1.禁止使用密码登录
设定SSH服务不能使用密码登录:
1)进入服务器配置文件:
[root@server ~]# cd /etc/ssh/
[root@server ssh]# ls
moduli ssh_host_ecdsa_key ssh_host_ed25519_key.pub
ssh_config ssh_host_ecdsa_key.pub ssh_host_rsa_key
sshd_config ssh_host_ed25519_key ssh_host_rsa_key.pub
# ssh_config客户端配置文件 sshd_config服务器配置文件
2)修改其中的密码认证:
[root@server ssh]# vim sshd_config
# 通过关键字查找到密码修改位置
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes
3)将密码认证选项改为no就可以了:
PasswordAuthentication no
:wq
4)重启服务:
[root@server ssh]# systemctl restart sshd
5)使用密码登录ssh服务器:
[kiosk@foundation5 ~]$ ssh root@172.25.5.10 -X
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
- 这时就已经不能使用密码登录了。
6)使用密钥对登录:
[root@client1 ~]# ssh root@172.25.5.10 -X
Last login: Tue Aug 18 16:14:02 2020 from 172.25.5.2
[root@server ~]#
- 这时依然可以使用密钥对登录。
这种方式更加安全,没有密钥对的连用户密码都不要想使用。
2.禁止远程登录root用户
root是超级管理员用户,使用root远程登录,万一用户误操作,不可恢复的结果。
我们可以sodu给他权限,但不能直接使用root。
所以仅允许普通用户远程登录。
1)在服务器的配置文件中设定root不能登录
[root@server ~]# vim /etc/ssh/sshd_config
#LoginGraceTime 2m
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
2)服务器保存文件、重启服务:
[root@server ~]# systemctl restart sshd
3)服务器创建一个普通用户:
[root@server ~]# useradd Liu # 新建用户
[root@server ~]# passwd Liu # 设定密码
Changing password for user Liu.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
4)客户端登录服务器root用户:
[root@client1 ~]# ssh root@172.25.5.2 -X
root@172.25.5.2's password: # 输入了三次密码,都没有登录成功
Permission denied, please try again.
root@172.25.5.2's password:
Permission denied, please try again.
root@172.25.5.2's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
5)客户端登录服务器Liu用户:
[root@client1 ~]# ssh Liu@172.25.5.2 -X
Liu@172.25.5.2's password:
/usr/bin/xauth: file /home/Liu/.Xauthority does not exis
[Liu@server ~]$
登录成功。
6)但是还是可以通过su命令切换到root用户:
[Liu@server ~]$ su -
Password:
Last login: Tue Aug 18 04:12:34 EDT 2020 from 172.25.5.250 on pts/0
Last failed login: Tue Aug 18 04:30:23 EDT 2020 from 172.25.5.250 on ssh:notty
There were 3 failed login attempts since the last successful login.
[root@server ~]$ whoami
root
- 内部用户切换与ssh无关。
3.修改默认端口
ssh不允许被很多人连接使用的,知道的人越少越好。
默认使用22端口,如果不修改很容易收到攻击,所以一般都会修改端口,也是tcp端口(1-65535)。
1000以内都是常见端口,由其100以内特别常见,尽量修改一个高位端口。
1)打开服务器ssh配置文件修改端口:
[root@server ~]# vim /etc/ssh/sshd_config
Port 59527
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
2)保存退出、重启服务:
[root@server ~]# systemctl restart sshd
- 这里我们已经关闭了防火墙和selinux所以不需要添加新端口直接使用就可以。
3)再次直接使用ssh连接:
[root@client1 ~]# ssh root@172.25.5.10
ssh: connect to host 172.25.5.10 port 22: Connection refused
# 22端口拒绝连接
4)加上端口号59527连接:
[kiosk@foundation5 ~]$ ssh root@172.25.5.10 -p 59527
root@172.25.5.10's password:
Last login: Tue Aug 18 16:58:28 2020
[root@server ~]#
使用新端口成功。
5)查看监听端口:
[root@server ~]# netstat -antupl
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:59527 0.0.0.0:* LISTEN 995/sshd
tcp 0 0 172.25.5.10:59527 172.25.5.250:34000 ESTABLISHED 2578/sshd: root@pts
键口连接成功。
4. 限制ssh监听IP
安全服务器最好不允许通过互联网连接,只能通过局域网进行连接。
建立一个中接服务器,用户在互联网中连接中接服务器,通过这台服务器连接要工作的安全服务器,是最安全的。
1)设定允许监听的网卡:
[root@server ~]# vim /etc/ssh/sshd_config
#Port 22
#AddressFamily any
ListenAddress 123.0.0.1
#ListenAddress ::
2)查看两张网卡的ip:
[root@server ~]# ip a
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:b2:1b:71 brd ff:ff:ff:ff:ff:ff
inet 172.25.5.10/24 brd 172.25.5.255 scope global ens3
valid_lft forever preferred_lft forever
inet6 fe80::4608:756c:3af9:e967/64 scope link
valid_lft forever preferred_lft forever
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:ff:50:ea brd ff:ff:ff:ff:ff:ff
inet 123.0.0.1/24 brd 123.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::2a6b:df0c:6f3e:aacb/64 scope link
valid_lft forever preferred_lft forever
3)重启ssh服务:
[root@server ~]# systemctl stop sshd
[root@server ~]# systemctl start sshd
4)通过指定监听网络连接:
[root@client1 ~]# ssh root@123.0.0.1
The authenticity of host '123.0.0.1 (123.0.0.1)' can't be established.
ECDSA key fingerprint is 46:14:77:1e:a0:f2:23:c3:66:24:23:76:ff:81:21:e5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '123.0.0.1' (ECDSA) to the list of known hosts.
Last login: Tue Aug 18 17:08:44 2020 from gateway
[root@server ~]#
- 之前设定过免密,连接成功。
5)通过其他网络连接:
[root@client1 ~]# ssh root@172.25.5.10
ssh: connect to host 172.25.5.10 port 22: Connection refused
- 拒绝被访问。
总结:
- 这里允许访问的是内网IP。
- 不允许访问的是外网IP。
- 我们使用外网连接到一个内网中的中接服务器。
- 通过中接服务器使用内网连接到ssh安全服务器。
5.白名单与黑名单
5.1 白名单
写入的用户允许被远程登录,其他用户都不允许远程登录。
这里是要写入配置文件中的,配置文件中默认没有出现。
1)服务端设定白名单:
[root@server ~]# vim /etc/ssh/sshd_config
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys
AllowUsers Liu # 允许Liu用户被登录
#AuthorizedPrincipalsFile none
2)服务端添加用户Liu:
[root@server ~]# useradd Liu # 添加新用户Liu
[root@server ~]# passwd Liu # 设定密码为123456
Changing password for user Liu.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
3)服务端重启服务:
[root@server ~]# systemctl restart sshd
4)登录root用户失败:
[root@client1 ~]# ssh root@172.25.5.10
root@172.25.5.10's password:
Permission denied, please try again.
root@172.25.5.10's password:
Permission denied, please try again.
root@172.25.5.10's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
5)登录Liu用户成功:
[root@client1 ~]# ssh Liu@172.25.5.10
Liu@172.25.5.10's password:
[Liu@server ~]$
5.2 黑名单
黑名单中相反,在名单中的用户不能被登录,其他用户都可以被登录。
添加语句:
DenyUsers 用户名
就可以添加黑名单了,这里就不演示了。