1. 程式人生 > >ssh安全優化-更改SSH服務器端遠程登錄的配置

ssh安全優化-更改SSH服務器端遠程登錄的配置

居住 token 限制 option logo config 更改 vim sed

1、備份原始文件

[root@szxjdw01-a-pro-14 ~]# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.ori
[root@szxjdw01-a-pro-14 ~]# vim /etc/ssh/sshd_config
#Port 2213行修改為Port 52113
PermitRootLogin yes 131行修改為PermitRootLogin no
#PermitEmptyPasswords no 60行修改為PermitEmptyPasswords no
GSSAPIAuthentication yes  75行修改為GSSAPIAuthentication no
UseDNS no 129行默認不需要修改


快捷方式:

直接把這5項目加入到配置文件/etc/ssh/sshd_config的第13行,方便,不需要一個一個配置找了。

切勿把這4項放到配置文件的最後面,此方法是錯誤的,因為配置文件中有重復的,默認是最前面的那個生效的,所以一定不能直接把這5項目加入/etc/ssh/sshd_config的最後一行。

#####by jeremy at 2018-08-06#####
Port 52113
PermitRootLogin no
PermitEmptyPasswords no
UseDNS no
GSSAPIAuthentication no
#####by jeremy at 2018-08-06#####

添加普通用戶和密碼

[root@szxjdw01-a-pro-14 ~]# useradd sysadm
[root@szxjdw01-a-pro-14 ~]# echo jayae86|passwd --stdin sysadm
Changing password for user oldboy.
passwd: all authentication tokens updated successfully.

重啟生效:restart和reload都可以

[root@szxjdw01-a-pro-14 ~]# /etc/init.d/sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]
[root@szxjdw01-a-pro-14 ~]# logout

註銷,然後切換sysadm,端口52113登錄即可。

[oldboy@szxjdw01-a-pro-14 ~]$ whoami
sysadm

更加增強安全:拒絕所有網段登錄,只開放安全網段登錄

su - 
#切換到root賬號下面修改


#拒絕所有ssh連接,修改完成後立即生效,不需要重啟,不需要重啟ssh服務

[root@shz-b-web-11 ~]# vim /etc/hosts.deny  
#
# hosts.deny    This file contains access rules which are used to
#               deny connections to network services that either use
#               the tcp_wrappers library or that have been
#               started through a tcp_wrappers-enabled xinetd.
#
#               The rules in this file can also be set up in
#               /etc/hosts.allow with a 'deny' option instead.
#
#               See 'man 5 hosts_options' and 'man 5 hosts_access'
#               for information on rule syntax.
#               See 'man tcpd' for information on tcp_wrappers
#
sshd:ALL


#只允許如下網段和IP地址連接,修改完成後立即生效,不需要重啟ssh服務

[root@shz-b-web-11 ~]# vim /etc/hosts.allow 
#
# hosts.allow   This file contains access rules which are used to
#               allow or deny connections to network services that
#               either use the tcp_wrappers library or that have been
#               started through a tcp_wrappers-enabled xinetd.
#
#               See 'man 5 hosts_options' and 'man 5 hosts_access'
#               for information on rule syntax.
#               See 'man tcpd' for information on tcp_wrappers
#
sshd:223.204.67.158/255.255.255.255    #公司內網IP
sshd:10.1.1.0/255.255.255.0    #服務器內網IP網段
sshd:10.1.2.0/255.255.255.0    #服務器內網IP網段


除了前面介紹的安全知識以外,還有更高級的SSH安全策略,具體如下。

1) 更改SSH監聽的IP,使其僅監聽內網IP。命令如下:

[root@szxjdw01-a-back-13 ~]# sed -n '13,20p' /etc/ssh/sshd_config
#####by jeremy#2018-08-06#####
Port 52113
ListenAddress 10.0.0.7:52113#企業僅指定監聽本機內網IP地址
PermitRootLogin no
PermitEmptyPasswords no
UseDNS no
GSSAPIAuthentication no
#####by jeremy#2018-08-06#####

2)通過防火墻限制僅能使用內網IP連接此服務器。限制命令如下:

iptables -I INPUT -p tcp --dport 52113 -s 10.0.0.0/24 -j ACCEPT
#默認規則為DROP時的限制SSH命令

3)通過撥號到×××服務器,然後從局域網訪問這些服務器,提升安全性。其實Linux主機安全就像我們居住的房子一樣安全。

4)工作中的系統安全重點就是互聯網TCP/IP連接、對外的Web服務器斷開http 80和https 443的安全控制。


ssh安全優化-更改SSH服務器端遠程登錄的配置