Linux-ssh安全配置

Linux-ssh安全配置:

注:本服务器为centos7系列
1.修改ssh默认连接端口
1)查看selinux处于disabled状态
[root@VM_0_3_centos /]# getenforce
Disabled
注:阿里云ECS和腾讯云CVM云服务器的selinux默认处于disabled状态。
2)修改端口
[root@VM_0_3_centos /]# vim /etc/ssh/sshd_config
#”打开port注释,将默认端口22修改自定义端口即可“
[root@VM_0_3_centos /]# grep ^Port /etc/ssh/sshd_config
Port 2222
3)重启sshd服务
[root@VM_0_3_centos /]# systemctl restart sshd
4)配置防火墙规则
[root@VM_0_3_centos ~]# firewall-cmd --permanent --zone=public --add-port=2222/tcp
success
[root@VM_0_3_centos ~]# firewall-cmd --reload
success
[root@VM_0_3_centos ~]# firewall-cmd --list-all |grep 2222
ports: 2222/tcp
注:腾讯云默认安全组开放了所有的端口,而阿里云默认安全组开启了几个常用端口,所以使用阿里云服务器需要登录控制台,自行在安全组开放此自定义的ssh端口。
5)验证(退出当前登录)
在这里插入图片描述
2.禁止root用户ssh服务器
1)创建一个普通权限的用户,并设置密码
[root@VM_0_3_centos ~]# useradd luke
[root@VM_0_3_centos ~]# passwd luke
Changing password for user luke.
New password:
BAD PASSWORD: The password contains the user name in some form
Retype new password:
passwd: all authentication tokens updated successfully.
注:修改之前创建远程ssh登录的普通账户,不然无法远程ssh,不便于管理。
2)禁止root远程ssh登录
[root@VM_0_3_centos ~]# vim /etc/ssh/sshd_config
#将39行的注释打开,修改yes为no
[root@VM_0_3_centos ~]# grep ^PermitRootLogin /etc/ssh/sshd_config
PermitRootLogin no
3)重启sshd服务,进行验证
在这里插入图片描述
#使用普通用户登录
[c:~]$ ssh 175.24.110.75 2222

Connecting to 175.24.110.75:2222…
Connection established.
To escape to local shell, press ‘Ctrl+Alt+]’.

[luke@VM_0_3_centos ~]$
[luke@VM_0_3_centos ~]$ w
23:54:02 up 21 min, 1 user, load average: 0.00, 0.04, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
luke pts/0 223.104.64.142 23:52 2.00s 0.03s 0.00s w
[luke@VM_0_3_centos ~]$ su
Password:
[root@VM_0_3_centos luke]# w
23:54:15 up 21 min, 1 user, load average: 0.00, 0.04, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
luke pts/0 223.104.64.142 23:52 7.00s 0.05s 0.00s sshd: luke [priv]

3.设置tcp_wrappers限制ssh访问服务器的源IP:
[root@VM_0_3_centos luke]# cat /etc/hosts.allow
#在该文件末尾添加以下内容
sshd:112.74.12*.4:allow
[root@VM_0_3_centos luke]# cat /etc/hosts.deny
#在该文件末尾添加以下内容
sshd:all:deny
注:通过修改sshd_config配置文件也可以限制源IP,也可以使用firewalld来限制源IP。

发布了24 篇原创文章 · 获赞 46 · 访问量 674

猜你喜欢

转载自blog.csdn.net/qq_36913644/article/details/104566625