準備環境:
1.兩臺虛擬機,系統是centos7,IP1:192.168.1.103,IP2:192.168.1.106
2.IP1的SSH的默認端口是22,這裏修改爲2222
3.只允許IP2通過SSH登錄IP1
4.不允許root賬戶直接ssh登錄
第一步:檢查防火牆設置
a) 檢查防火牆是否開啓
systemctl status firewalld.service
b) 如果防火牆開啓,檢查端口是否放行
firewall-cmd --zone=public --list-port
c) 如果端口不放行,則放行此端口
firewall-cmd --zone=public --add-port=2222/tcp --permanent
d) 重載防火牆設置
firewall-cmd --reload
第二步:檢查SELinux(訪問控制系統)
a) 檢查SELinux是否開啓,enabled表示開啓
sestatus -v | grep "SELinux status"
b) 通過semanage(默認系統不自帶,需要安裝)設置SELinux,允許新的SSH端口
yum install policycoreutils-python
semanage port -a -t ssh_port_t -p tcp 2222
semanage port -l | grep ssh
vim /etc/ssh/sshd_config
a) 修改端口,默認是22
Port 2222
b) 禁止root賬戶通過SSH直接登錄,默認是yes
PermitRootLogin no
第四步:限制SSH登錄的IP
a) 設置禁止所有ip連接服務器的SSH
vim /etc/hosts.deny
sshd:all:deny
b) 設置允許指定ip連接服務器的SSH
vim /etc/hosts.allow
sshd:192.168.1.106:allow
第四步:重啓SSH服務,並通過登錄進行驗證
systemctl restart sshd.service
systemctl status sshd.service
原文出自:http://blog.csdn.net/daiyudong2020/article/details/54782758