Linux服務器安全配置

1.建立其他用戶

useradd userid
passwd userid
移動klarkzh到wheel分組后刪除userid分組
usermod -g wheel userid

2.注釋掉系統(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:operator:/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:
#news:x:9:13:news:/etc/news:

3.關閉系統(tǒng)不需要的服務

service acpid stop chkconfig acpid off #停止服務,取消開機啟動 #電源進階設定,常用在 Laptop 上
service autofs stop chkconfig autofs off #停用自動掛載檔桉系統(tǒng)與週邊裝置
service bluetooth stop chkconfig bluetooth off #停用Bluetooth藍芽
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

如果要恢復某一個服務,可以執(zhí)行下面操作
service acpid start chkconfig acpid on

4.禁止非root用戶執(zhí)行/etc/rc.d/init.d/下的系統(tǒng)命令

chmod -R 700 /etc/rc.d/init.d/*
chmod -R 777 /etc/rc.d/init.d/* #恢復默認設置

5.給下面的文件加上不可更改屬性,從而防止非授權用戶獲得權限

chattr +i /etc/passwd
chattr +i /etc/shadow
chattr +i /etc/group
chattr +i /etc/gshadow
chattr +i /etc/services #給系統(tǒng)服務端口列表文件加鎖,防止未經(jīng)許可的刪除或添加服務
lsattr /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/services #顯示文件的屬性

注意:執(zhí)行以上權限修改之后,就無法添加刪除用戶了。果再要添加刪除用戶,需要先取消上面的設置,等用戶添加刪除完成之后,再執(zhí)行上面的操作。

chattr -i /etc/passwd #取消權限鎖定設置
chattr -i /etc/shadow
chattr -i /etc/group
chattr -i /etc/gshadow
chattr -i /etc/services #取消系統(tǒng)服務端口列表文件加鎖

現(xiàn)在可以進行添加刪除用戶了,操作完之后再鎖定目錄文件

6.限制不同文件的權限

chattr +a .bash_history #避免刪除.bash_history或者重定向到/dev/null
chattr +i .bash_history
chmod 700 /usr/bin #恢復 chmod 555 /usr/bin
chmod 700 /bin/ping #恢復 chmod 4755 /bin/ping
chmod 700 /usr/bin/vim #恢復 chmod 755 /usr/bin/vim
chmod 700 /bin/netstat #恢復 chmod 755 /bin/netstat
chmod 700 /usr/bin/tail #恢復 chmod 755 /usr/bin/tail
chmod 700 /usr/bin/less #恢復 chmod 755 /usr/bin/less
chmod 700 /usr/bin/head#恢復 chmod 755 /usr/bin/head
chmod 700 /bin/cat #恢復 chmod 755 /bin/cat
chmod 700 /bin/uname #恢復 chmod 755 /bin/uname
chmod 500 /bin/ps #恢復 chmod 755 /bin/ps

7.禁止使用Ctrl+Alt+Del快捷鍵重啟服務器

cp /etc/inittab /etc/inittabbak
vi /etc/inittab #注釋掉下面這一行
#ca::ctrlaltdel:/sbin/shutdown -t3 -r now

8.使用yum update更新系統(tǒng)時不升級內(nèi)核,只更新軟件包

由于系統(tǒng)與硬件的兼容性問題,有可能升級內(nèi)核后導致服務器不能正常啟動,這是非常可怕的,沒有特別的需要,建議不要隨意升級內(nèi)核。

cp /etc/yum.conf /etc/yum.confbak

  • 修改yum的配置文件 vi /etc/yum.conf 在[main]的最后添加 exclude=kernel*
  • 直接在yum的命令后面加上如下的參數(shù):yum --exclude=kernel* update
    查看系統(tǒng)版本cat /etc/issue
    查看內(nèi)核版本uname -a

9.關閉Centos自動更新

chkconfig --list yum-updatesd #顯示當前系統(tǒng)狀態(tài)
yum-updatesd   #0:關閉 1:關閉 2:啟用 3:啟用 4:啟用 5:啟用 6:關閉
service yum-updatesd stop #關閉 開啟參數(shù)為start
# 停止 yum-updatesd: [確定]
service yum-updatesd status #查看是否關閉
yum-updatesd # 已停
chkconfig --level 35 yum-updatesd off #禁止開啟啟動(系統(tǒng)模式為3、5)
chkconfig yum-updatesd off #禁止開啟啟動(所有啟動模式全部禁止)
chkconfig --list yum-updatesd #顯示當前系統(tǒng)狀態(tài)
yum-updatesd # 0:關閉 1:關閉 2:啟用 3:關閉 4:啟用 5:關閉 6:關閉

10.關閉多余的虛擬控制臺

我們知道從控制臺切換到 X 窗口,一般采用 Alt-F7 ,為什么呢?因為系統(tǒng)默認定義了 6 個虛擬控制臺,
所以 X 就成了第7個。實際上,很多人一般不會需要這么多虛擬控制臺的,修改/etc/inittab ,注釋掉那些你不需要的。
cp /etc/inittab /etc/inittabbak
vi /etc/inittab

# Run gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1
#2:2345:respawn:/sbin/mingetty tty2
#3:2345:respawn:/sbin/mingetty tty3
#4:2345:respawn:/sbin/mingetty tty4
#5:2345:respawn:/sbin/mingetty tty5
#6:2345:respawn:/sbin/mingetty tty6

11.刪除[MySQL]歷史記錄

用戶登陸[數(shù)據(jù)庫]后執(zhí)行的SQL命令也會被MySQL記錄在用戶目錄的.mysql_history文件里。如果數(shù)據(jù)庫用戶用SQL語句修改了數(shù)據(jù)庫密碼,也會因.mysql_history文件而泄漏。所以我們在shell登陸及備份的時候不要在-p后直接加密碼,而是在提示后再輸入數(shù)據(jù)庫密碼。另外這兩個文件我們也應該不讓它記錄我們的操作,以防萬一。

cd
cp .bash_history .bash_historybak #備份
cp .mysql_history .mysql_historybak
rm .bash_history .mysql_history
ln -s /dev/null .bash_history
ln -s /dev/null .mysql_history

11. 修改history命令記錄

cp /etc/profile /etc/profilebak
vi /etc/profile
#找到 HISTSIZE=1000 改為 HISTSIZE=50

13.隱藏服務器系統(tǒng)信息

在缺省情況下,當你登陸到[Linux]系統(tǒng),它會告訴你該linux發(fā)行版的名稱、版本、內(nèi)核版本、服務器的名稱。為了不讓這些默認的信息泄露出來,我們要進行下面的操作,讓它只顯示一個"login:"提示符。刪除/etc/issue和/etc/issue[.NET]這兩個文件,或者把這2個文件改名,效果是一樣的。

mv /etc/issue /etc/issuebak
mv /etc/issue.net /etc/issue.netbak

14.優(yōu)化Linux內(nèi)核參數(shù)

然后編輯 /etc/sysctl.conf
vim /etc/sysctl.conf

fs.file-max = 51200

net.core.rmem_max = 67108864

net.core.wmem_max = 67108864

net.core.netdev_max_backlog = 250000

net.core.somaxconn = 4096

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 0

net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_keepalive_time = 1200

net.ipv4.ip_local_port_range = 10000 65000

net.ipv4.tcp_max_syn_backlog = 8192

net.ipv4.tcp_max_tw_buckets = 5000

net.ipv4.tcp_fastopen = 3

net.ipv4.tcp_rmem = 4096 87380 67108864

net.ipv4.tcp_wmem = 4096 65536 67108864

net.ipv4.tcp_mtu_probing = 1

15.CentOS 系統(tǒng)優(yōu)化

cp /etc/profile /etc/profilebak2
vim /etc/profile #在文件末尾添加以下內(nèi)容

ulimit -c unlimited
ulimit -s unlimited
ulimit -SHn 65535
source /etc/profile #使配置立即生效

ulimit -a #顯示當前的各種用戶進程限制

16.服務器禁止ping

cp /etc/rc.d/rc.local /etc/rc.d/rc.localbak
vim /etc/rc.d/rc.local #在文件末尾增加下面這一行
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
參數(shù)0表示允許 1表示禁止

17.防止IP欺騙

編輯host.conf文件并增加如下幾行來防止IP欺騙攻擊。
vim /etc/host.conf

order bind,hosts
multi off
nospoof on

18.防止DoS攻擊

對系統(tǒng)所有的用戶設置資源限制可以防止DoS類型攻擊。如最大進程數(shù)和內(nèi)存使用數(shù)量等。例如,可以在/etc/security/limits.conf中添加如下幾行:

* hard core 0
* hard rss 5000  #( 本行或許沒用,man limits.conf 顯示 maximum resident set size (KB) (Ignored in Linux 2.4.30 and higher)
* hard nproc 50

然后必須編輯/etc/pam.d/login文件檢查下面一行是否存在。
session required /lib/security/pam_limits.so
上面的命令禁止調(diào)試文件,限制進程數(shù)為50并且限制內(nèi)存使用為5MB。

18.自動注銷帳號的登錄

vim /etc/profile
修改"HISTSIZE="
后面加入下面這行:
TMOUT = 300
300,表示5分。這樣,如果系統(tǒng)中登陸的用戶在5分鐘內(nèi)都沒有動作,那么系統(tǒng)會自動注銷這個賬戶。

19.限制su切換

vim /etc/pam.d/su
文件中找到#auth required pam_wheel.so use_uid,去掉前面的注釋#。

20.修改sshd

修改端口Port和
PermitRootLogin no

20.修改iptables

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

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

  • linux資料總章2.1 1.0寫的不好抱歉 但是2.0已經(jīng)改了很多 但是錯誤還是無法避免 以后資料會慢慢更新 大...
    數(shù)據(jù)革命閱讀 13,375評論 2 33
  • 1.Linux下如何用命令查看實時日志(完整命令) tail -f 路徑.log查看前多少行 tai-200f 路...
    qianyewhy閱讀 2,562評論 0 11
  • 下面,就給大家介紹這些CentOS常用命令。 一:使用CentOS常用命令查看cpu more/proc/cpui...
    guiwuzhe閱讀 2,742評論 1 25
  • 一、用戶帳號和環(huán)境……………………………………………………………. 2 二、系統(tǒng)訪問認證和授權…………………………...
    大福技術閱讀 6,140評論 0 5
  • 每個人都背負著一個沉重的十字架,在緩慢而艱難地朝著目的地前進。 途中,有一個人忽然停了下來。他心想:這個十字架實在...
    顏夢sky閱讀 477評論 1 2

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