OPS Notes By 枯木

Ssh的一些安全设定

| Comments

ssh是我们日常工作中必不可少的工具,所以它的安全性也是异常重要了,这里笔者简单介绍了一些ssh配置中需要注意的几个点。

首先,作为root用户登录并备份原始文件,输入下面的命令:

1
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

1、调整认证时限(默认单位为s),即当用户登录ssh之后,要求输入密码的时间限制,规定时间没有输入则自动断线:

1
LoginGraceTime 60

2、最好限制root用户远程登录,修改如下:

1
PermitRootLogin no

3、关闭X11转发,防止额外的信息泄露

1
X11Forwarding no

4、取消以下两行的注释,使得登录后显示一些信息

1
2
PrintMotd yes
PrintLastLog yes

5、编辑/etc/motd文件,添加一些警告信息,如下

1
2
3
4
This computer system is for authorized users only. All activity
is logged and regularly checked. Individuals using this system
without authority or in excess of their authority are subject to
having all their services revoked...

6、关闭ssh闲置会话

1
2
ClientAliveInterval 60
ClientAliveCountMax 5
  • ClientAliveInterval设置表示如果超过这么长时间没有收到客户端的任何数据,将通过安全通道向客户端发送一个”alive”消息,并等候应答,单位默认为s。
  • ClientAliveCountMax表示在未收到任何客户端回应前最多允许发送多少个”alive”消息,这是设置5次

以上也就是客户端如果60*5=300s,即5分钟如果没有任何操作,则空闲连接会被强制断开,关闭时提示如下

Connection to x.x.x.x closed by remote host.
Connection to x.x.x.x closed.

7、开启TCPKeepAlive

1
TCPKeepAlive yes

TCPKeepAlive指定系统是否向客户端发送TCP keepalive消息,这种消息可以检测到死连接、连接不当关闭、客户端崩溃等异常,避免僵尸进程产生,推荐开启。

8、修改ssh默认端口22

1
Port xxxx    #自定义端口

9、设置允许登录的ssh用户

1
AllowUsers test1 test2 test3

也可以设置允许的组

1
AllowGroups admin

10、关闭不必要的认证

如果可以,只允许公钥认证,关闭其它认证方式

1
2
PasswordAuthentication no
ChallengeResponseAuthentication no

基本的一个设置就是以上的部分了内容了,还可以结合fail2ban更好的保护ssh的安全。关于fail2ban下次介绍,其实很多网友已经写了很多相关的博文了。

参考文章

–EOF–

Comments