centos7版本中ssh相关的设置

1.设置SSH连接端口
1.1.关闭SELinux
--关闭系统当前selinux
# setenforce 0  
--关闭系统永久selinux
# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

1.2.编辑/etc/ssh/sshd_config,修改端口(22->26),并重启sshd
# vi /etc/ssh/sshd_config 
Port 26
# grep ^Port /etc/ssh/sshd_config 

# systemctl restart sshd

1.3.修改firewalld规则,允许tcp/26端口
# firewall-cmd --permanent --zone=public --add-port=26/tcp
# firewall-cmd --reload

#确认修改
# firewall-cmd --list-all |grep 26

这样,可以就通过26端口连接ssh。
上述过程中,如修改ssh端口后,重启sshd时报错,而注释掉修改又能正常重启,用getenforce检查selinux是否已关闭。

2、禁止root远程ssh(默认为禁止)
2.1.编辑sshd_config
# vi /etc/ssh/sshd_config
PermitRootLogin no 
--确认修改
# grep ^PermitRootLogin /etc/ssh/sshd_config 

2.2.重启sshd
# systemctl restart sshd

3.限制ssh远程访问的IP
--编辑/etc/hosts.allow,添加如下内容
# echo "sshd:192.168.1.111:allow" >>/etc/hosts.allow
--编辑hosts.deny,添加如下内容
# echo "sshd:ALL" >>/etc/hosts.deny
此外,还可通过修改sshd_config限制通过ssh远程访问的IP,还可使用firewalld对IP进行限制。

4.限制ssh访问的用户名
--修改sshd_config,允许ssh远程访问服务器的test用户(多个用户空格分隔,默认拒绝其他所有用户)
# echo "AllowUsers test" >>/etc/ssh/sshd_config

--重启sshd
# systemctl restart sshd

5.获取ssh的相关信息
5.1.获取sshd位置
# whereis sshd
5.2.获取ssh版本
# ssh -V

#5.3.查看sshd程序相关信息
# strings /usr/sbin/sshd |grep OpenSSH6.6

#5.4.sed更改版本信息
--先备份原有信息
# cp -p /usr/sbin/sshd ./sshd_bk
--更改版本信息
# sed -i 's/OpenSSH_6.6/OpenSSH_7.4/g' /usr/sbin/sshd

#5.5.重启sshd
[root@imzcy ~]# systemctl restart ssh

发布了169 篇原创文章 · 获赞 46 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/LHDZ_BJ/article/details/85124107