CentOS6.6 安全加固及性能優(yōu)化

CentOS Linux是被很多企業(yè)廣泛應(yīng)用的OS,本文總結(jié)了一些常見的加固方法。

基本原則:

最小的權(quán)限+最小的服務(wù)=最大的安全

操作之前先備份;

為避免配置錯(cuò)誤無法登錄主機(jī),請(qǐng)始終保持有一個(gè)終端已用root登錄并不退出,在另一個(gè)終端中做配置修改。這樣即使改錯(cuò),也不至于因系統(tǒng)無法登錄導(dǎo)致永遠(yuǎn)無法登錄或恢復(fù)配置。

操作項(xiàng):

一、注釋掉系統(tǒng)不需要的用戶和用戶組

注意:不建議直接刪除,當(dāng)你需要某個(gè)用戶時(shí),自己重新添加會(huì)很麻煩。

? ?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匿名賬號(hào)?

??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 檢查開機(jī)自啟動(dòng)的服務(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 ? ?#電源進(jìn)階設(shè)定,常用在 Laptop 上

??service cpuspeed stop;chkconfig??cpuspeed??off? ?#停用控制CPU速度主要用來省電

??service cups stop;chkconfig cups off? ? #停用 Common UNIX Printing System 使系統(tǒng)支援印表機(jī)

??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/*? ? #恢復(fù)默認(rèn)設(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)時(shí)間

??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)核不夠熟,加上注解大家謹(jǐn)慎選擇使用)

net.ipv4.tcp_syncookies = 1 ? ? ? ? ? #1是開啟SYN Cookies,當(dāng)出現(xiàn)SYN 等待隊(duì)列溢出時(shí),啟用Cookies來處理,可防范少量SYN攻擊,默認(rèn)是0關(guān)閉 ?net.ipv4.tcp_tw_reuse = 1 ? ? ? ? ? ? #1是開啟重用,允許將TIME_AIT sockets重新用于新的TCP連接,默認(rèn)是0關(guān)閉 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

net.ipv4.tcp_tw_recycle = 5 ? ? ? ? ? ?#TCP失敗重傳次數(shù),默認(rèn)是15,減 少次數(shù)可釋放內(nèi)核資源 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

net.ipv4.tcp_max_syn_backlog = 10240 ? ?#進(jìn)入SYN包的最大請(qǐng)求隊(duì)列,默認(rèn) 是1024 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

net.core.netdev_max_backlog = ?10240 ?#允許送到隊(duì)列的數(shù)據(jù)包最大設(shè)備隊(duì) 列,默認(rèn)300 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

net.core.somaxconn = 2048 ? ? ? ? ? ? ?#listen掛起請(qǐng)求的最大數(shù)量,默認(rèn) 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ù),默認(rèn)5 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

net.ipv4.tcp_syn_retries = 2 ? ? ? ? ? #向外SYN握手重試次數(shù),默認(rèn)4 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

net.ipv4.tcp_max_orphans = 3276800 ? ? #系統(tǒng)中最多有多少個(gè)TCP套接字不 被關(guān)聯(lián)到任何一個(gè)用戶文件句柄上,如果超出這個(gè)數(shù)字,孤兒連接將立即復(fù)位并打印警告信息 ? ? ? ? ? ? ? ??

net.ipv4.tcp_mem = 94500000 915000000 927000000 ??

#net.ipv4.tcp_mem[0]:低于此值,TCP沒有內(nèi)存壓力; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

#net.ipv4.tcp_mem[1]:在此值下,進(jìn)入內(nèi)存壓力階段; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

#net.ipv4.tcp_mem[2]:高于此值,TCP拒絕分配socket。內(nèi)存單位是頁(yè),可根據(jù) 物理內(nèi)存大小進(jìn)行調(diào)整,如果內(nèi)存足夠大的話,可適當(dāng)往上調(diào)。上述內(nèi)存單位是頁(yè),而不是字節(jié)。 ? ? ? ? ? ??

net.ipv4.ip_local_port_range = 20480 ?65535 ?#(表示用于向外連接的端口范圍。缺省情況下很?。?2768到61000 ?注意:這里不要將最低值設(shè)的太低,否則可能會(huì)占用掉正常的端口!?

八、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? ? #顯示當(dāng)前的各種用戶進(jìn)程限制

九、服務(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遠(yuǎn)程登錄?

? PermitRootLoginno

? PermitEmptyPasswords no #禁止空密碼登錄?

2.不要支持閑置會(huì)話,并配置 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 上有三個(gè)連接嘗試:

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 以上,以降低掃描到端口的機(jī)率。

? Port 18439

7.使用iptables允許特定的主機(jī)連接

作為TCP wrappers的一個(gè)代替品,你可以使用iptables來限制SSH訪問(但可以同時(shí)使用這個(gè)兩個(gè)的),這里有一個(gè)簡(jiǎn)單的例子,指出了如何允許一個(gè)特定的主機(jī)連接到你的SSH服務(wù):

iptables -A INPUT -p tcp -m state --state NEW --source 193.180.177.13 --dport 22 -j ACCEPT

? 保存后重啟ssh服務(wù)。

十二、添加普通用戶并進(jìn)行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?

十三、安裝安全補(bǔ)?。ń?jīng)常更新為好)

yum install yum-security

檢查安全更新

yum –security check-update

只安裝安全更新

yum update –security

檢查特定軟件有無安全更新

yum list-security software_name

列出更新的詳細(xì)信息

yum info-security software_name

十四、將 SSH 程序包和需要的庫(kù)保持為最新:

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 連接:(慎用:如果那臺(tái)主機(jī)掛了就慘了,這個(gè)真是比較安全的)

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 上有三個(gè)連接嘗試:(不是每個(gè)版本都可以,使用時(shí)需確認(rèn))

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容