歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux管理 >> Linux安全 >> linux(CentOS)服務器安全配置詳解

linux(CentOS)服務器安全配置詳解

日期:2017/2/27 17:10:52   编辑:Linux安全

1. 關閉不必要的服務

chkconfig --list | awk '{print "chkconfig " $1 " off"}' > /tmp/chkconfiglist.sh;/bin/sh /tmp/chkconfiglist.sh;rm -rf /tmp/chkconfiglist.sh
chkconfig  crond on
chkconfig  irqbalance on
chkconfig  network on
chkconfig  sshd on
chkconfig  syslog on
chkconfig  iptables on
setenforce 0
sed -i 's/^SELINUX=.*$/SELINUX=disabled/g' /etc/sysconfig/selinux

2. 刪除不必要rpm包

yum -y groupremove "FTP Server" "Text-based Internet" "Windows File Server" "PostgreSQL Database" "News Server" "DNS Name Server" "Web Server" "Dialup Networking Support" "Mail Server" "Office/Productivity" "Ruby" "Office/Productivity" "Sound and Video" "X Window System" "X Software Development" "Printing Support" "OpenFabrics Enterprise Distribution"

3. 刪除系統特殊的的用戶帳號

禁止所有默認的被操作系統本身啟動的且不需要的帳號,當你第一次裝上系統時就應該做此檢查,Linux提供了各種帳號,你可能不需要,如果你不需要這個帳號,就移走它,你有的帳號越多,就越容易受到攻擊
for i in adm lp sync shutdown halt mail news uucp operator games gopher ftp
do
    userdel $i
done

for i in adm lp mail news uucp games dip
do
    groupdel $i
done

4. 對用戶和口令文件進行權限控制,並加上不可更改屬性

chmod 600 /etc/passwd
chmod 600 /etc/shadow
chmod 600 /etc/group
chmod 600 /etc/gshadow  
chattr +i /etc/passwd
chattr +i /etc/shadow
chattr +i /etc/group
chattr +i /etc/gshadow
chattr +i /etc/services

5. 禁止ping

echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all #臨時生效
echo 'net.ipv4.icmp_echo_ignore_all=1>>/etc/sysctl.conf' #重啟永久生效


6. 修改ssh服務的root登錄權限

前提不行建立另一個一般用戶,修改ssh服務配置文件,使的ssh服務不允許直接使用root用戶來登錄。
vi /etct/ssh/sshd_config
PermitRootLogin yes
將這行前的#去掉後,修改為:PermitRootLogin no


7. 密碼輸錯5次鎖定180s

sed -i '4a auth        required      pam_tally2.so deny=5 unlock_time=180' /etc/pam.d/system-auth

8. 修改shell命令的history記錄個數

sed -i 's/HISTSIZE=.*$/HISTSIZE=100/g' /etc/profile
source /etc/profile

9. 修改自動注銷帳號時間

自動注銷帳號的登錄,在Linux系統中root賬戶是具有最高特權的。如果系統管理員在離開系統之前忘記注銷root賬戶,那將會帶來很大的安全隱患,應該讓系統會自動注銷。通過修改賬戶中“TMOUT”參數,可以實現此功能。TMOUT按秒計算。編輯你的profile文件(vi /etc/profile),在"HISTSIZE="後面加入下面這行:
TMOUT=300
300,表示300秒,也就是表示5分鐘。這樣,如果系統中登陸的用戶在5分鐘內都沒有動作,那麼系統會自動注銷這個賬戶。

10. 記錄每個在服務器上操作的命令

mkdir /root/logs  #記錄日志位置自己更改,最好隱藏
echo "export PROMPT_COMMAND='{ msg=\$(history 1 | { read x y; echo \$y; });user=\$(whoami); echo \$(date \"+%Y-%m-%d %H:%M:%S\"):\$user:\`pwd\`/:\$msg ---- \$(who am i); } >> \$HOME/logs/\`hostname\`.\`whoami\`.history-timestamp'" >> /root/.bash_profile


11. 優化網絡參數

sed -i 's/net.ipv4.tcp_syncookies.*$/net.ipv4.tcp_syncookies = 1/g' /etc/sysctl.conf  #TCP SYN Cookie保護生效
echo 'net.ipv4.tcp_tw_reuse = 1' >> /etc/sysctl.conf
echo 'net.ipv4.tcp_tw_recycle = 1' >> /etc/sysctl.conf
echo 'net.ipv4.ip_local_port_range = 1024 65000' >> /etc/sysctl.conf
sysctl -p


12. iptables配置

sed -i 's/IPTABLES_MODULES="ip_conntrack_netbios_ns"/#IPTABLES_MODULES="ip_conntrack_netbios_ns"/g' /etc/sysconfig/iptables-config
cat > /etc/sysconfig/iptables << EOF
# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
-A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
# -A INPUT -p udp -m udp --dport 20 -j ACCEPT
COMMIT
EOF
/sbin/service atd start
echo "/sbin/service iptables stop" | at now+3minutes
/sbin/service iptables restart
iptables根據實際情況設置
Copyright © Linux教程網 All Rights Reserved