Linux服務器安全登陸設置記錄

 

在平常運維工做中,對加固服務器的安全設置是一個機器重要的環境。比較推薦的作法是:
1)嚴格限制ssh登錄(參考:Linux系統下的ssh使用(依據我的經驗總結)):
     修改ssh默認監聽端口
     禁用root登錄,單獨設置用於ssh登錄的帳號或組;
     禁用密碼登錄,採用證書登錄;
     ListenAddress綁定本機內網ip,即只能ssh鏈接本機的內網ip進行登錄;
2)對登錄的ip作白名單限制(iptables、/etc/hosts.allow、/etc/hosts.deny)
3)能夠專門找兩臺機器做爲堡壘機,其餘機器作白名單後只能經過堡壘機登錄,將機房服務器的登錄進去的口子收緊;
     另外,將上面限制ssh的作法用在堡壘機上,而且最好設置登錄後的二次驗證環境(Google-Authenticator身份驗證)
4)嚴格的sudo權限控制參考:linux系統下的權限知識梳理
5)使用chattr命令鎖定服務器上重要信息文件,如/etc/passwd、/etc/group、/etc/shadow、/etc/sudoers、/etc/sysconfig/iptables、/var/spool/cron/root等
6)禁ping(echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all)html

今天這裏主要說下服務器安全登錄的白名單設置,經過下面兩種方法:
1)iptables對ssh端口作限制;
2)/etc/hosts.allow和/etc/hosts.deny限制;這兩個文件是控制遠程訪問設置的,經過他能夠容許或者拒絕某個ip或者ip段的客戶訪問linux的某項服務。
若是當iptables、hosts.allow和hosts.deny三者都設置時或設置出現衝突時,遵循的優先級是hosts.allow > hosts.deny >iptableslinux

下面來看一下幾個限制本地服務器登錄的設置:
1)iptables和hosts.allow設置一致,hosts.deny不設置。若是出現衝突,以hosts.allow設置爲主。
[root@localhost ~]# cat /etc/sysconfig/iptables
.....
-A INPUT -s 192.168.1.0/24 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 114.165.77.144 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 133.110.186.130 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPTcentos

[root@localhost ~]# cat /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
#                                                                                                      //切記:這裏的192.168.1.*網段設置不能改成192.168.1.0/24;多個ip之間用逗號隔開
sshd:192.168.1.*,114.165.77.144,133.110.186.130,133.110.186.139:allow     //最後的allow能夠省略安全

[root@localhost ~]# cat /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
#bash

如上的設置,133.110.186.139雖然沒有出如今iptables的白名單設置裏,可是出如今hosts.allow設置裏,那麼它是容許登錄本地服務器的;
也就是說hosts.allow裏設置的ip均可以登錄本地服務器,hosts.allow裏沒有設置而iptables裏設置的ip不能登錄本地服務器;
因此,只要hosts.allow裏設置了,iptables其實就沒有必要再對ssh進行限制了;服務器

2)hosts.allow不設置,iptables和hosts.deny設置(兩者出現衝突,以hosts.deny爲主)
[root@localhost ~]# cat /etc/sysconfig/iptables
.....
-A INPUT -s 192.168.1.0/24 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 114.165.77.144 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 133.110.186.130 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPTapp

[root@localhost ~]# cat /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
#運維

[root@localhost ~]# cat /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:133.110.186.130:deny                                               //最後的deny能夠省略ssh

以上雖然133.110.186.130在iptables裏設置了,可是在hosts.deny裏也設置了,這時要遵循hosts.deny的設置,即133.110.186.130這個ip不能登錄本地服務器;
也就是說上面只有192.168.1.0網段和114.165.77.144能登錄本地服務器;tcp

3)當iptables、hosts.allow、hosts.deny三者都設置時,遵循的hosts.allow!
[root@localhost ~]# cat /etc/sysconfig/iptables
.....
-A INPUT -s 192.168.1.0/24 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 114.165.77.144 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 133.110.186.130 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 133.110.186.133 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 133.110.186.137 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

[root@localhost ~]# cat /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:192.168.1.*,114.165.77.144,133.110.186.130,133.110.186.139:allow                 //最後的allow能夠省略

[root@localhost ~]# cat /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:deny                                  //最後的deny能夠省略

上面設置以後,只有hosts.allow裏面設置的192.168.1.*,114.165.77.144,133.110.186.130,133.110.186.139這些ip能登錄本地服務器

4)還有一種設置,hosts.deny不動,在hosts.allow裏面設置deny
[root@localhost ~]# cat /etc/sysconfig/iptables
.....
-A INPUT -s 192.168.1.0/24 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 114.165.77.144 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -s 133.110.186.130 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

[root@localhost ~]# cat /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:192.168.1.*,114.165.77.144,133.110.186.130,133.110.186.139:allow             //最後的allow能夠省略
sshd:all:deny                                            //這個原本是在hosts.deny裏的設置,也能夠放在這,表示出了上面的ip以外都被限制登錄了。

[root@localhost ~]# cat /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
#

5)iptables關閉,則hosts.allow和hosts.deny文件同時設置纔有效。

==========================================================
/etc/hosts.allow和/etc/hosts.deny文件配置後不生效問題:

若是在/etc/hosts.allow和/etc/hosts.deny文件裏配置了相關服務(如sshd、ftp)的ip限制後,發現不生效!
緣由可能以下:
1)/etc/hosts.allow 與 /etc/hosts.deny 只對ssh應用調用了tcp_wrappers的服務器才起做用;
2)查看服務器的ssh是否支持tcp_wrappers。使用下面兩個命令:
   # strings /usr/sbin/sshd|grep hosts_access
   # ldd `which sshd` | grep libwrap
3)若是上面的兩個查看命令都沒有結果,說明本機的ssh不支持tcp_wrappers
4)通常centos6默認的ssh都是支持tcp_wrappers的。但要是將服務器的ssh升級到openssh6.7以後,則就不支持了!
   由於從openssh6.7開始,ssh官方就移除了對tcp wrappers的支持!!!!
5)也就是說,centos6系統下默認的ssh版本(OpenSSH_5.3p1)若是升級到了openssh6.7以後,ssh應用就不支持tcp wrappers了。
   這樣/etc/hosts.allow和/etc/hosts.deny文件裏的限制設置也就無效了!
6)可是centos7默認的ssh版本是OpenSSH_7.4p1,centos7下默認的ssh版本是支持tcp wrappers的!

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 

[root@localhost ~]# ssh -V
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017

[root@localhost ~]# ldd `which sshd` | grep libwrap
        libwrap.so.0 => /lib64/libwrap.so.0 (0x00007fd302fc9000)
        
[root@localhost ~]# strings /usr/sbin/sshd|grep hosts_access
hosts_access
相關文章
相關標籤/搜索