CentOS Linux是被很多企業(yè)廣泛應用的OS,本文總結(jié)了一些常見的加固方法贞盯。
基本原則:
最小的權(quán)限+最小的服務(wù)=最大的安全
操作之前先備份;
為避免配置錯誤無法登錄主機沪饺,請始終保持有一個終端已用root登錄并不退出躏敢,在另一個終端中做配置修改。這樣即使改錯整葡,也不至于因系統(tǒng)無法登錄導致永遠無法登錄或恢復配置件余。
操作項:
一、注釋掉系統(tǒng)不需要的用戶和用戶組
注意:不建議直接刪除遭居,當你需要某個用戶時啼器,自己重新添加會很麻煩。
? ?cp??/etc/passwd??/etc/passwdbak? ?#修改之前先備份
? ?vi /etc/passwd??#編輯用戶俱萍,在前面加上#注釋掉此行?
? ?#adm:x:3:4:adm:/var/adm:/sbin/nologin
? ?#lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
? ?#sync:x:5:0:sync:/sbin:/bin/sync
? ?#shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
? ?#halt:x:7:0:halt:/sbin:/sbin/halt
? ?#uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
? ?#operator:x:11:0
perator:/root:/sbin/nologin
? ?#games:x:12:100:games:/usr/games:/sbin/nologin
? ?#gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
? ?#ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin? ? #注釋掉ftp匿名賬號?
??cp /etc/group? ?/etc/groupbak? ?#修改之前先備份
??vi /etc/group??#編輯用戶組端壳,在前面加上#注釋掉此行
??#adm:x:4:root,adm,daemon
??#lp:x:7:daemon,lp
??#uucp:x:14:uucp
??#games:x:20:
??#dip:x:40:
二、關(guān)閉系統(tǒng)不需要的服務(wù)
使用命令?chkconfig|grep on 檢查開機自啟動的服務(wù)枪蘑;
abrt-ccpp ? ? ? 0:off ? 1:off ? 2:off ? 3:on ? ?4:off ? 5:on ? ?6:off
abrtd ? ? ? ? ? 0:off ? 1:off ? 2:off ? 3:off ? 4:off ? 5:on ? ?6:off
acpid ? ? ? ? ? 0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
auditd ? ? ? ? ?0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
autofs ? ? ? ? ?0:off ? 1:off ? 2:off ? 3:on ? ?4:on ? ?5:on ? ?6:off
blk-availability ? ? ? ?0:off ? 1:on ? ?2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
certmonger ? ? ?0:off ? 1:off ? 2:off ? 3:on ? ?4:on ? ?5:on ? ?6:off
cgconfig ? ? ? ?0:off ? 1:off ? 2:off ? 3:off ? 4:off ? 5:off ? 6:off
cpuspeed ? ? ? ?0:off ? 1:on ? ?2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
crond ? ? ? ? ? 0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
cups ? ? ? ? ? ?0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
haldaemon ? ? ? 0:off ? 1:off ? 2:off ? 3:on ? ?4:on ? ?5:on ? ?6:off
ip6tables ? ? ? 0:off ? 1:off ? 2:on ? ?3:off ? 4:on ? ?5:on ? ?6:off
iptables ? ? ? ?0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
irqbalance ? ? ?0:off ? 1:off ? 2:off ? 3:on ? ?4:on ? ?5:on ? ?6:off
kdump ? ? ? ? ? 0:off ? 1:off ? 2:off ? 3:off ? 4:on ? ?5:on ? ?6:off
lvm2-monitor ? ?0:off ? 1:on ? ?2:on ? ?3:off ? 4:on ? ?5:on ? ?6:off
mcelogd ? ? ? ? 0:off ? 1:off ? 2:off ? 3:on ? ?4:off ? 5:on ? ?6:off
mdmonitor ? ? ? 0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
messagebus ? ? ?0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
netconsole ? ? ?0:off ? 1:off ? 2:off ? 3:off ? 4:off ? 5:off ? 6:off
netfs ? ? ? ? ? 0:off ? 1:off ? 2:off ? 3:on ? ?4:on ? ?5:on ? ?6:off
network ? ? ? ? 0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
nfslock ? ? ? ? 0:off ? 1:off ? 2:off ? 3:on ? ?4:on ? ?5:on ? ?6:off
portreserve ? ? 0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
postfix ? ? ? ? 0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
restorecond ? ? 0:off ? 1:off ? 2:off ? 3:off ? 4:off ? 5:off ? 6:off
rpcbind ? ? ? ? 0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
rpcgssd ? ? ? ? 0:off ? 1:off ? 2:off ? 3:on ? ?4:on ? ?5:on ? ?6:off
rsyslog ? ? ? ? 0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
sshd ? ? ? ? ? ?0:off ? 1:off ? 2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
sysstat ? ? ? ? 0:off ? 1:on ? ?2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
udev-post ? ? ? 0:off ? 1:on ? ?2:on ? ?3:on ? ?4:on ? ?5:on ? ?6:off
服務(wù)說明:
auditdThe audit package contains the user space utilities for storing and searching the audit records generate by the audit subsystem in the Linux 2.6 kernel.
blk-availabilityControls availability of LVM2 block devices.
cgconfigStart cgconfigparser.
cgredThis is a daemon for automatically classifying processes into cgroups based on UID/GID.
cpuspeedThis program monitors the system's idle percentage and reduces or raises the CPUs' clock speeds and voltages accordingly to minimize power consumption when idle and maximize perfor-mance when needed.
crondThe daemon to execute scheduled commands.
ip6tablesIt's IPv6 packet filter administration.
iptablesIt's administration tool for IPv4 packet filtering and NAT.
lvm2-monitorMonitor LVM2 with using dmeventd.
netconsoleIt sends printk message from kernel on UDP.
netfsNFS clinet.
networkNetwork system.
postfixSMTP service daemon.
rdiscDetect routers in local subnets.
restorecondIt's the daemon that watches for file creation and then sets the default SELinux file context.
rsyslogIt reads and/or clears kernel message ring buffer; set console_loglevel.
saslauthdsaslauthd is a daemon process that handles plaintext authentication requests on behalf of the SASL library.
sshdOpenSSH sshd daemon.
udev-postMoves the generated persistent udev rules to /etc/udev/rules.d.
(見http://www.server-world.info/en/note?os=CentOS_6&p=initial_conf&f=4)
關(guān)閉不必要的服務(wù)
??service acpid stop;chkconfig acpid off ? ?#電源進階設(shè)定损谦,常用在 Laptop 上
??service cpuspeed stop;chkconfig??cpuspeed??off? ?#停用控制CPU速度主要用來省電
??service cups stop;chkconfig cups off? ? #停用 Common UNIX Printing System 使系統(tǒng)支援印表機
??service ip6tables stop;chkconfig ip6tables off? ?#禁止IPv6
三、禁止非root用戶執(zhí)行/etc/rc.d/init.d/下的系統(tǒng)命令
??chmod -R 700 /etc/rc.d/init.d/*?(chmod -R 755 /etc/rc.d/init.d/*? ? #恢復默認設(shè)置)
四岳颇、修改history命令記錄
? ?cp /etc/profile? ?/etc/profilebak
? ?vi /etc/profile
? ?找到 HISTSIZE=1000 改為 HISTSIZE=50
五照捡、調(diào)整文件描述符大小
? ?echo "ulimit -SHn 65535" >> /etc/rc.local
? ?/etc/security/limits.conf
? ?* ? ? soft ? ?nofile ? ? ? ? ?65535?
* ? ? hard ? ?nofile ? ? ? ? ?65535
* ? ? soft ? ?stack ? ? ? ? ? unlimited
* ? ? hard ? ?stack ? ? ? ? ? unlimited
六、同步系統(tǒng)時間
??cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
crontab -e
? ? ? 0-59/10 * * * * /usr/sbin/ntpdate us.pool.ntp.org | logger -t NTP
? ? ??chkconfig --list | grep ntp?#看下服務(wù)情況
? ? ? chkconifg ntpd on
? ? ? service ntpd start?
七话侧、內(nèi)核參數(shù)優(yōu)化
vi /etc/sysctl.conf ? ?#末尾添加如下參數(shù) ?(備注:內(nèi)核不夠熟麻敌,加上注解大家謹慎選擇使用)
net.ipv4.tcp_syncookies = 1 ? ? ? ? ? #1是開啟SYN Cookies,當出現(xiàn)SYN 等待隊列溢出時掂摔,啟用Cookies來處理术羔,可防范少量SYN攻擊赢赊,默認是0關(guān)閉 ?net.ipv4.tcp_tw_reuse = 1 ? ? ? ? ? ? #1是開啟重用,允許將TIME_AIT sockets重新用于新的TCP連接级历,默認是0關(guān)閉 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
net.ipv4.tcp_tw_recycle = 5 ? ? ? ? ? ?#TCP失敗重傳次數(shù)释移,默認是15,減 少次數(shù)可釋放內(nèi)核資源 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
net.ipv4.tcp_max_syn_backlog = 10240 ? ?#進入SYN包的最大請求隊列寥殖,默認 是1024 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
net.core.netdev_max_backlog = ?10240 ?#允許送到隊列的數(shù)據(jù)包最大設(shè)備隊 列玩讳,默認300 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
net.core.somaxconn = 2048 ? ? ? ? ? ? ?#listen掛起請求的最大數(shù)量,默認 128 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
net.core.wmem_default = 8388608 ? ? ? ?#發(fā)送緩存區(qū)大小的缺省值 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
net.core.rmem_default = 8388608 ? ? ? ?#接受套接字緩沖區(qū)大小的缺省值 (以字節(jié)為單位) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
net.core.rmem_max = 16777216 ? ? ? ? ? #最大接收緩沖區(qū)大小的最大值 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
net.core.wmem_max = 16777216 ? ? ? ? ? #發(fā)送緩沖區(qū)大小的最大值 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
net.ipv4.tcp_synack_retries = 2 ? ? ? ?#SYN-ACK握手狀態(tài)重試次數(shù)嚼贡,默認5 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
net.ipv4.tcp_syn_retries = 2 ? ? ? ? ? #向外SYN握手重試次數(shù)熏纯,默認4 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
net.ipv4.tcp_max_orphans = 3276800 ? ? #系統(tǒng)中最多有多少個TCP套接字不 被關(guān)聯(lián)到任何一個用戶文件句柄上,如果超出這個數(shù)字粤策,孤兒連接將立即復位并打印警告信息 ? ? ? ? ? ? ? ??
net.ipv4.tcp_mem = 94500000 915000000 927000000 ??
#net.ipv4.tcp_mem[0]:低于此值樟澜,TCP沒有內(nèi)存壓力; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
#net.ipv4.tcp_mem[1]:在此值下叮盘,進入內(nèi)存壓力階段秩贰; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
#net.ipv4.tcp_mem[2]:高于此值,TCP拒絕分配socket柔吼。內(nèi)存單位是頁毒费,可根據(jù) 物理內(nèi)存大小進行調(diào)整,如果內(nèi)存足夠大的話愈魏,可適當往上調(diào)觅玻。上述內(nèi)存單位是頁,而不是字節(jié)培漏。 ? ? ? ? ? ??
net.ipv4.ip_local_port_range = 20480 ?65535 ?#(表示用于向外連接的端口范圍串塑。缺省情況下很小:32768到61000 ?注意:這里不要將最低值設(shè)的太低北苟,否則可能會占用掉正常的端口!?
八打瘪、CentOS 系統(tǒng)優(yōu)化??
? ? ? cp??/etc/profile??/etc/profilebak2
? ?? ?vi /etc/profile? ?? ?#在文件末尾添加以下內(nèi)容
? ?? ?ulimit -c unlimited
? ?? ?ulimit -s unlimited
? ?? ?ulimit -SHn 65535? ?
? ?? ?source??/etc/profile? ? #使配置立即生效
? ?? ?ulimit -a? ? #顯示當前的各種用戶進程限制
九友鼻、服務(wù)器禁止PING
? ? ? cp??/etc/rc.d/rc.local??/etc/rc.d/rc.localbak? ???
? ? ? vi??/etc/rc.d/rc.local? ?? ???#在文件末尾增加下面這一行
? ? ? echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all ? ???
? ? ? 參數(shù)0表示允許? ?1表示禁止
十、去除系統(tǒng)相關(guān)信息?
? 事先備份到安全的地方哈:)?
? echo "Welcome to Server" >/etc/issue?
? echo "Welcome to Server" >/etc/issue.net
? echo "Welcome to Server" >/etc/redhat-release
十一闺骚、SSH服務(wù)安全配置
? ? ?vi?/etc/ssh/sshd_config ??
? ? ?1. 禁用root遠程登錄?
? PermitRootLoginno
? PermitEmptyPasswords no #禁止空密碼登錄?
2.不要支持閑置會話彩扔,并配置 Idle Log Out Timeout 間隔:
ClientAliveInterval 600?# (Set to 600 seconds = 10 minutes)
ClientAliveCountMax 0
? 3.禁用用戶的 .rhosts 文件:
? IgnoreRhosts yes
? 4.PasswordAuthentication no
5.配置 iptables,以便在 30 秒內(nèi)僅允許在端口 2022 上有三個連接嘗試:
Redhat iptables example (Update /etc/sysconfig/iptables):
-I INPUT -p tcp --dport 2022 -i eth0 -m state --state NEW -m recent --set
? -I INPUT -p tcp --dport 2022 -i eth0 -m state --state NEW -m recent --update --seconds 30 --hitcount 3 -j DR
6.ssh安全技巧之更改 SSH 端口
建議改成 10000 以上僻爽,以降低掃描到端口的機率虫碉。
? Port 18439
7.使用iptables允許特定的主機連接
作為TCP wrappers的一個代替品,你可以使用iptables來限制SSH訪問(但可以同時使用這個兩個的)胸梆,這里有一個簡單的例子敦捧,指出了如何允許一個特定的主機連接到你的SSH服務(wù):
iptables -A INPUT -p tcp -m state --state NEW --source 193.180.177.13 --dport 22 -j ACCEPT
? 保存后重啟ssh服務(wù)须板。
十二、添加普通用戶并進行sudo授權(quán)管理?
??useradd -d /home/goal user (指定home目錄與用戶名不同)
? passwd user ?#設(shè)置密碼 ?
? vi /etc/sudoers ?#或visudo打開兢卵,添加user用戶所有權(quán) 限 ?
? root ? ?ALL=(ALL) ? ? ? ALL
? user ? ?ALL=(ALL) ? ? ? ALL?
十三习瑰、安裝安全補丁(經(jīng)常更新為好)
yum install yum-security
檢查安全更新
yum –security check-update
只安裝安全更新
yum update –security
檢查特定軟件有無安全更新
yum list-security software_name
列出更新的詳細信息
yum info-security software_name
十四秽荤、將 SSH 程序包和需要的庫保持為最新:
yum update openssh-server openssh openssh-clients -y
十五甜奄、清空防火墻并設(shè)置規(guī)則
? ? iptables -F ? #清楚防火墻規(guī)則
iptables -L ? #查看防火墻規(guī)則
iptables -A INPUT -p tcp --dport 80 -j ACCEPT ?
iptables -A INPUT -p tcp --dport 22 -j ACCEPT ?
iptables -A INPUT -p tcp --dport 53 -j ACCEPT ?
iptables -A INPUT -p udp --dport 53 -j ACCEPT ?
iptables -A INPUT -p udp --dport 123 -j ACCEPT ?
iptables -A INPUT -p icmp -j ACCEPT ?
iptables -P INPUT DROP ?
/etc/init.d/iptables save
十六、配置防火墻以接受僅來自已知網(wǎng)段的 SSH 連接:(慎用:如果那臺主機掛了就慘了窃款,這個真是比較安全的)
Update /etc/sysconfig/iptables (Redhat specific file) to accept connection only
from 192.168.100.0/24 and 209.64.100.5/27, enter:
-A RH-FW-1-INPUT -s 192.168.100.0/24 -m state --state NEW -p tcp --dport 22 -j ACCEPT
-A RH-FW-1-INPUT -s 209.64.100.5/27 -m state --state NEW -p tcp --dport 22 -j ACCEPT?
十七课兄、配置 iptables,以便在 30 秒內(nèi)僅允許在端口 2022 上有三個連接嘗試:(不是每個版本都可以晨继,使用時需確認)
Redhat iptables example (Update /etc/sysconfig/iptables):
-I INPUT -p tcp --dport 2022 -i eth0 -m state --state NEW -m recent --set
-I INPUT -p tcp --dport 2022 -i eth0 -m state --state NEW -m recent --update?--seconds 30 --hitcount 3 -j DR
十八烟阐、鎖定關(guān)鍵文件系統(tǒng)
chattr +i /etc/passwd
chattr +i /etc/inittab
chattr +i /etc/group
chattr +i /etc/shadow
chattr +i /etc/gshadow
【參考資料】
http://blog.chinaunix.net/uid-26430172-id-3387855.html
http://wenku.baidu.com/view/beb4161158fb770bf68a552a.html
http://www.ibm.com/developerworks/cn/aix/library/au-sshsecurity/
http://os.51cto.com/art/200803/68174_all.htm
http://www.cnblogs.com/xiaoluo501395377/archive/2013/05/26/3100444.html
http://www.111cn.net/sys/linux/86318.htm
http://www.centoscn.com/CentOS/config/2013/1102/2034.html