SSH应用实战——安全防护(fail2ban)

ssh 安全配置

  • 端口

   ssh随机端口范围在 27000-30000,可以手动修改也要改在这个范围内,建议定时修改端口。

  • 密码

  登陆密码应包含大小写、数字、特殊字符等 10 位以上,建议定期修改密码。

  • root 登录

  默认允许 root 登录 vps,也可以新建普通用户,禁止 root 登录。

  vim /etc/ssh/sshd_config

1 # add below lines
2 PermitRootLogin no
  • 密钥登录 

  如果觉得还不够安全或者非 root 登录不方便也可以设置密钥登录,然后再禁止密码登录。不过这样只能在自己电脑上登录。

  客户端生成密钥对,然后把公钥传输到服务端

  [客户端 ~]# ssh-keygen

  [客户端 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub -p 端口 用户名@服务器地址

 

  • 用户限制 

  可以设置允许登录的用户以及禁止登陆的用户

  vim /etc/ssh/sshd_config 

1 # add below lines
2 AllowUsers xxx
3 DenyUsers xxx
  • ip 限制 

  相比 iptables ,通过修改配置文件限制 ip ssh 登录更简便。

  vim /etc/hosts.allow

1 # add below lines
2 # only allow x.x.x.x login
3 sshd: x.x.x.x
4 # allow all ip login
5 sshd:all

  vim /etc/hosts.deny

1 # add below lines
2 # deny x.x.x.x login
3 sshd: x.x.x.x

Centos7配置fail2ban防止ssh被暴力破解

  • 配置epel源

  wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

  或者     yum -y install epel-release

  • 安装fail2ban

  yum install fail2ban

  • 查看当前版本

  fail2ban-server -V

  • 配置

  cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

  vi /etc/fail2ban/jail.local

  末尾添加以下内容

1 [ssh-iptables]
2 enabled = true
3 filter = sshd
4 action = iptables[name=SSH, port=22, protocol=tcp]
5 #sendmail-whois[name=SSH, dest=your@email.com, sender=fail2ban@email.com]
6 logpath = /var/log/secure
7 maxretry = 3
8 bantime = 300

  maxretry 表示最大尝试次数达到3次,bantime 客户端300秒内禁止连接

  • 启动fail2ban服务

  systemctl start fail2ban

  • 验证

  fail2ban-client ping

  Server replied: pong                  //表示正常

  • 测试

  连接当前主机,连续输错三次密码则会拒绝连接

  查看被禁止的IP

  fail2ban-client status ssh-iptables

  此处ssh-iptables为jail.local的[ssh-iptables]名称

  可以在/var/log/fail2ban.log查看屏蔽日志

 

下一篇姊妹篇之——fail2ban 触发邮件告警

 

 努力吧少年,不努力学习,你将一无所有。。。。。。。。。。。。。。。

 

转载于:https://www.cnblogs.com/iceblues/p/10203423.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值