1、關閉不必要的系統服務
Centos 6方法:如 chkconfig xinetd off 關閉telnet服務
chkconfig vsftpd off 關閉ftp服務
關閉后,使用命令chkconfig –list 來查看是否關閉
Centos 7 方法:
關閉25端口,一般為postfix,這是一個郵件服務端口,如果用不到郵件服務,可以關閉
1、 [root@host-172-21-185-13 ~]# netstat -lntup | grep master
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2066/master
tcp6 0 0 ::1:25 :::* LISTEN 2066/master
2、現在知道它的端口對應程序為master.但是它具體是什么程序呢?
locate master | grep '/master$' 發現路徑是/usr/libexec/postfix/master
3、systemctl status postfix 查看postfix服務的狀態
systemctl stop postfix 關閉postfix服務
systemctl disable postfix 開機關閉postfix服務
2、更改SSH默認端口
方法:打開sshd_config文件,找到這行:#Port 22 把22修改為其他沒有占用的端口,並重啟sshd服務
注意:修改ssh端口,最好是提前把telnet打開並連接上,防止修改后遠程連接不上服務器就悲劇了,修改后沒問題了,再關閉telnet服務
3、禁止root用戶遠程ssh登錄
方法:建普通賬戶,useradd username
如:1、創建賬號
useradd guest
passwd guest
2、給賬號權限
修改 /etc/sudoers 文件,找到下面一行,在root下面添加一行,如下所示:
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
guest ALL=(ALL) ALL
3、將guest添加到wheel組中
編輯 su 文件(vi /etc/pam.d/su),在開頭添加下面兩行:
auth sufficient pam_rootok.so 和
auth required pam_wheel.so group=wheel
這表明只有 wheel 組的成員可以使用 su 命令成為 root 用戶
添加方法為:
usermod -G wheel guest
查看是否添加成功:
groups guest
普通賬戶添加成功后,通過普通賬戶來登錄服務器,把root賬戶登錄禁止
打開 sshd_config ,找到下面這行:
#PermitRootLogin yes
更改為:
PermitRootLogin no
然后保存文件,重啟sshd守護進程使改動生效。執行下面命令即可:
sudo /etc/init.d/sshd restart
注意:一定要先建立普通賬戶,測試普通賬戶能切換到root賬戶后,再禁止root賬戶登錄,否則普通賬戶沒登錄,root也禁止了,那就悲劇了
4、限制用戶使用SU命令切換root
編輯 su 文件(vi /etc/pam.d/su),在開頭添加下面兩行:
auth sufficient pam_rootok.so 和
auth required pam_wheel.so group=wheel
這表明只有 wheel 組的成員可以使用 su 命令成為 root 用戶
添加方法為:
usermod -G wheel username
查看是否添加成功:
groups username
5、密碼復雜度策略
/etc/pam.d/system-auth文件中,增加內容
password requisite pam_cracklib.so ucredit=-1 lcredit=-1 dcredit=-1 ocredit=-1
(注:ucredit:大寫字母個數;lcredit:小寫字母個數;dcredit:數字個數;ocredit:特殊字符個數 )
6、檢查是否配置賬戶認證失敗次數限制(最大值:6)
編輯/etc/pam.d/system-auth文件
配置:
auth required pam_tally2.so deny=5 unlock_time=600
account required pam_tally2.so
注意pam_tally2.so模塊的名字,以前用的是pam_tally.so,現在的系統中沒有pam_tally.so模塊,千萬不要配置在文件中,這樣會造成普通用戶sudo -I 切不到root下
查看系統中是不是有這個模塊,模塊名可以在目錄/lib/security/或/lib64/security/中找到
如果鎖定,需要登錄到root賬戶下,pam_tally2 -u zxadmin 查看zxadmin用戶的鎖定次數 ;pam_tally2 -u zxadmin -r 來解鎖
7、檢查密碼重復使用次數限制
編輯/etc/pam.d/system-auth文件,
修改設置如下
password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok remember=5
補充操作說明
只需在password sufficient這一行加上remember=5即可
8、密碼相關配置(下面的這幾項都是針對普通密碼的?)
vi /etc/login.defs (修改如下)
PASS_MAX_DAYS 90 #密碼最長過期天數
PASS_MIN_DAYS 0 #密碼最小過期天數
PASS_MIN_LEN 8 #密碼最小長度
PASS_WARN_AGE 7 #密碼過期警告天數
9、檢查是否存在空口令賬號
cat /etc/shadow | awk 'BEGIN{FS=":";ORS=","}{if($2=="")print$1};'|more
如果命令能過濾出來空口令賬號,一定要刪除空口令賬號
10、檢查歷史命令設置
編輯文件/etc/profile,
修改HISTSIZE配置為100
HISTSIZE=100
11、檢查是否設置命令行界面超時退出
vi /etc/profile (增加如下)
export TMOUT=600 (單位:秒)
12、配置時間同步ntp
客戶端安裝ntpdate
yum -y install ntpdate
但這樣的同步,只是強制性的將系統時間設置為ntp服務器時間。如果CPU Tick有問題,只是治標不治本。所以,一般配合cron命令,來進行定期同步設置。比如,在crontab中添加:
crontab -e
0 5 * * * /usr/sbin/ntpdate ntp1.aliyun.com
13、關閉selinux
編輯vim /etc/sysconfig/selinux
設置SELINUX=disabled
14、禁止同時按下ctrl+alt+del 重啟
編輯/etc/init/control-alt-delete.conf
找到
start on control-alt-delete
更改為
#start on control-alt-delete
15、umask設置,涉及四個地方修改
在文件/etc/csh.login中設置 umask 077或UMASK 077
在最后添加一行 umask=077
在文件/etc/profile中設置umask 077或UMASK 077
在文件/etc/csh.cshrc中設置 umask 077或UMASK 077
檢查文件/etc/bashrc(或/etc/bash.bashrc)中設置 umask 077或UMASK 077
16、ssh登錄警告設置
1、執行如下命令創建ssh banner信息文件:
#touch /etc/ssh_banner
#chown bin:bin /etc/ssh_banner
#chmod 644 /etc/ssh_banner
#echo " Authorized only. All activity will be monitored and reported " > /etc/ssh_banner
可根據實際需要修改該文件的內容。
2. 修改/etc/ssh/sshd_config文件,添加如下行:
Banner /etc/ssh_banner
3.重啟sshd服務:
#/etc/init.d/sshd restart
17、禁止root遠程telnet登錄。
編輯 /etc/pam.d/login文件,配置auth required pam_securetty.so
18、禁用telnet服務
利用命令rpm -qa |grep telnet查看是否安裝telnet 和telnet server 如果安裝的話
1、編輯/etc/xinetd.d/telnet, 修改 disable = yes。
2.激活xinetd服務。命令如下:
# service xinetd restart
如果沒安裝則說明禁用telnet服務