Openssh安全性配置

来自linux中国网wiki
跳到导航 跳到搜索

起因

刚刚被ddos 有人想用root 去登录我的机器 ,于是 抽空作一下笔记,也加固一下vps

SSH 安全性相关配置

这些同时也适合于vps 

1.通过控制用户访问限制 SSH 访问: 
# vi /etc/ssh/sshd_config 
AllowUsers fsmythe bnice swilson
DenyUsers jhacker joebadguy jripper

2.将 root 账户仅限制为控制台访问:

PermitRootLogin no

3.仅使用 SSH Protocol 2:

Protocol 2

4.
LoginGraceTime 30  #LoginGraceTime 允许一次登录花费 30 秒;如果用户花费的时间超过 30 秒,就不允许他访问,必须重新登录。
MaxAuthTries 2  #MaxAuthTries 把错误尝试的次数限制为 3 次,3 次之后拒绝登录尝试

5.禁用用户的 .rhosts 文件:
# vi /etc/ssh/sshd_config
IgnoreRhosts yes

6.禁用空密码:
# vi /etc/ssh/sshd_config
PermitEmptyPasswords no



7.不要支持闲置会话,并配置 Idle Log Out Timeout 间隔:
#当客户端连上服务器端后,若没有任何操作则,服务器端默认会
#每隔一定时间发送一个alive消息给客户端寻求客户端应答,
#默认一共发三次.若都没有回应,则断开连其中           
#ClientAliveInterval设置每隔多少秒发送一次alive消息
#ClientAliveCountMax 设置一共发多少次.


ClientAliveInterval 600		# (Set to 600 seconds = 10 minutes)
ClientAliveCountMax 0


下面是非交互操作命令
#use sed   no Interaction

#deny root ssh   PermitRootLogin
mkdir -p /data/back_cnf 
cp -a /etc/ssh/sshd_config /data/back_cnf
sed -i 's/^PermitRootLogin yes$/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/^#PermitRootLogin yes$/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/^PasswordAuthentication yes$/PasswordAuthentication no/' /etc/ssh/sshd_config
sed -i 's/^ChallengeResponseAuthentication yes$/ChallengeResponseAuthentication no/' /etc/ssh/sshd_config 
sed -i 's/^PermitEmptyPasswords yes$/PermitEmptyPasswords no/' /etc/ssh/sshd_config
sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config
sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
systemctl restart  sshd
#service sshd restart

#打开pubk
sed -i "s/#PubkeyAuthentication yes/PubkeyAuthentication yes/g" /etc/ssh/sshd_config
#禁止密码
sed -i "s/^PasswordAuthentication yes/PasswordAuthentication no/g" /etc/ssh/sshd_config



8.配置 iptables,以便在 30 秒内仅允许在端口 2022 上有三个连接尝试:
Redhat iptables example (Update /etc/sysconfig/iptables): 
-I INPUT -p tcp --dport 2022 -i eth0 -m state --state NEW -m recent --set

-I INPUT -p tcp --dport 2022 -i eth0 -m state --state NEW -m recent --update 
--seconds 30 --hitcount 3 -j DR


9.从系统上删除 rlogin 和 rsh 二进制程序,并将它们替代为 SSH 的一个 symlink:
# find /usr -name rsh
/usr/bin/rsh
# rm -f /usr/bin/rsh
# ln -s /usr/bin/ssh /usr/bin/rsh




#下面这些比较安全但是比较麻烦 适合生产环境 
10.为私有密钥使用一个强大的口令和密码保护来创建公私密钥对(绝不要生成一个无密码的密钥对或一个无密码口令无密钥的登录):
(Use a higher bit rate for the encryption for more security)
ssh-keygen -t rsa -b 4096

11.配置 TCP 包装程序,以便仅允许选定的远程主机并拒绝不合意的主机:
# vi /etc/hosts.deny
ALL: 192.168.200.09		# IP Address of badguy





以减少ssh连接超时等待的时间:
方法:ssh -o ConnectTimeout=3 192.168.0.10
或修改sshd_config文件里面的UseDNS 选项,改为UseDNS no。

TCP Wrappers

TCP Wrappers是优先查找/etc/host.allow,再查找/etc/host.deny 如果两个文件信息有冲突,同一个主机出现在两个文件中,则/etc/allow的生效,如果一个主机都没有出现在2个文件中,则默认允许访问

sudo sed  -i '$a\sshd:61.140.169.212,138.68.5.0'  /etc/hosts.allow
 
 vi /etc/hosts.deny
 sshd:ALL
 
 sudo sed  -i '$a\sshd:ALL' /etc/hosts.deny

#在非 61.140.169.212,138.68.5.0 上ssh  
ssh  root@youid
ssh_exchange_identification: read: Connection reset by peer

Linux简单防火墙TCP Warppers设置

其它

配置和使用 ssh-agent
对于拒绝创建无密码 SSH 公私密钥对的真正的偏执狂来说,有一个 ssh-agent 实用程序。简言之,您使用 ssh-agent 实用程序来暂时在无口令集的公私密钥对配置上授予无密码的 SSH 访问,但仅针对当前 shell 会话。在运用 ssh-agent 实用程序之前,像往常一样输入口令:

参考

(转载)SSH暴力攻击的几种防范方法

保护 SSH 的三把锁 https://www.ibm.com/developerworks/cn/aix/library/au-sshlocks/

SSH 安全性和配置入门 不错哦 SSH 架构 https://www.ibm.com/developerworks/cn/aix/library/au-sshsecurity/index.html#ibm-pcon

http://www.jianshu.com/p/37464cea469e http://forlinux.blog.51cto.com/8001278/1352900

Linux/Centos服务器ssh安全设置 https://www.haiyun.me/archives/linux-ssh-secure-config.html