安裝Linux系統最小化,即選包最小化,yum安裝軟件包也要最小化,無用的包不裝。開機自啟動服務最小化,即無用的服務不開啟。操作命令最小化,例如:能用"rm -f test.txt”就不用" rm -rf test.txt登錄Linux用戶最小化,平時沒有特殊需求用root登錄,用普通用戶登錄即可。普通用戶授權權限最小化,即只給用戶必需的管理系統的命令。Linux系統文件及目錄的權限設置最小化,禁止隨意創建、更改、刪除文件。
1.避免直接到root用戶下操作
在企業生產環境中應盡量避免直接到root用戶下操作,除非有超越普通用戶權限的系統維護需求。
在生產環境中,刪除多余的賬戶信息。
1)創建普通用戶并設置密碼
在交互式下添加普通用戶并設置密碼:
[root@ntp ~]# useradd noodles #添加用戶 [root@ntp ~]# passwd noodles #修改用戶密碼 Changing password for user noodles. New password: 111111 BAD PASSWORD: The password is a palindrome Retype new password: 111111 passwd: all authentication tokens updated successfully. [root@ntp ~]#
非交互式設置用戶密碼:
[root@ntp ~]# echo "12345678" | passwd --stdin noodles Changing password for user noodles. passwd: all authentication tokens updated successfully. [root@ntp ~]#
上面的設置方法有個問題,當用history命令查看時,能看到設置的密碼,所以需要清除歷史命令:
[root@ntp ~]# echo "12345678" | passwd --stdin noodles && history -c Changing password for user noodles. passwd: all authentication tokens updated successfully. [root@ntp ~]# history 1 history [root@ntp ~]#
2)切換用戶
[root@ntp ~]# whoami root [root@ntp ~]# su - noodles [noodles@ntp ~]$ su - root #注意這里-的區別是切換到用戶目錄,否則不切換,還是上次用戶的目錄 密碼: 上一次登錄:六 3月 26 10:26:25 CST 2022從 123.183.158.11pts/0 上 [root@ntp ~]#
3)更改Linux命令提示符
LINUX命令提示符由PS1環境變量控制,我們可以查看一下:
可以直接用set命令,但是太多。
[root@ntp ~]# set | wc -l 2768 [root@ntp ~]# set | grep PS1 PS1='[\u@\h \W]\$ ' #\u:user;\h:hostname;\W:pwd;\$:用戶符號 [root@ntp ~]# PS1='[\u@\h \W\t]\$ ' #增加\t時間變量 [root@ntp ~11:07:59]# PS1='[\u@\h \W\d]\$ ' #增加\d日期變量 [root@ntp ~六 3月 26]#
上面的修改只是臨時性生效,要永久生效,需修改配置文件.bashrc文件:
[root@ntp ~]# cp ~/.bashrc{,.ori.20220326} #先做備份
[root@ntp ~]#
[root@ntp ~]# pwd #當前所在目錄
/root
[root@ntp ~]# echo "PS1='[\u@\h \W\t]\$ '" >>/root/.bashrc #在最后一行添加語句
[root@ntp ~]# cat /root/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
PS1='[\u@\h \W\t]$ ' #這是在最后一行添加的結果
[root@ntp ~]#
這時并沒有起作用,需要source一下:
[root@ntp ~]# source /root/.bashrc [root@ntp ~11:20:31]$
4)rm命令的使用注意的問題
-f:強制刪除文件或目錄; -i:刪除已有文件或目錄之前先詢問用戶(默認) -r或-R:遞歸處理,將指定目錄下的所有文件與子目錄一并處理; -v:顯示指令的詳細執行過程。 \ : 不詢問直接操作!
工作中盡量不要用rm命令(粉碎文件),如果必須要刪除,可以用mv命令替代rm,建議將要刪除的文件,移動到/tmp臨時目錄中,類似Windows的回收站的功能。
5)關于重啟與關機
企業環境中,服務器要慎重重啟,必須重啟的時候,需要輸入以下命令:
[root@localhost ~11:42:59]$ shutdown -r now #重啟命令還有init 6;reboot。
如果要關機,先保存后關機:
[root@localhost ~11:43:59]$ shutdown -h now #關機命令除上面,還有init 0;halt;
上面的補充:
# shutdown -r now #立即重啟 # shutdown -r +5 #5分鐘后重啟 # shutdown -h now #即刻關機 # shutdown -h + 5 #5分鐘后關機
2.關閉SELinux功能
SELinux(Security-Enhanced Linux) 是美國國家安全局(NSA)對于強制訪問控制的實現,是 Linux歷史上最杰出的新安全子系統,實驗和生產環境基本做法還是關閉居多,安全問題由其它方式來解決。
[root@ntp ~11:27:12]$ getenforce #查看當前SELinux狀態 Enforcing [root@ntp ~11:27:57]$ cp /etc/selinux/config /etc/selinux/config.ori.20220326 #備份原文件 [root@ntp ~11:28:41]$ cat /etc/selinux/config #查看原文件 # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. #強制開啟 # permissive - SELinux prints warnings instead of enforcing. #不開啟 # disabled - No SELinux policy is loaded. #徹底關閉 SELINUX=enforcing # SELINUXTYPE= can take one of three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted [root@ntp ~11:28:56]$ vim /etc/selinux/config [root@ntp ~11:29:52]$ cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted
上面修改過多成后,進行比較,是否修改了:
[root@ntp ~11:30:01]$ diff /etc/selinux/config.ori.20220326 /etc/selinux/config 7c7 < SELINUX=enforcing --- > SELINUX=disabled #表明已修改 [root@ntp ~11:30:55]$
上面的修改只是下次啟動后才能生效,所以需要臨時處理:
[root@ntp ~11:30:55]$ getenforce Enforcing [root@ntp ~11:36:08]$ setenforce=0
重啟后:
[root@localhost ~11:42:49]$ getenforce Disabled [root@localhost ~11:42:56]$
3.精簡開機自啟動服務
CentOS7已不再使用chkconfig管理啟動項,使用 systemctl list-unit-files 可以查看啟動項
[root@localhost ~11:50:36]$ systemctl list-unit-files | grep enable cups.path enabled abrt-ccpp.service enabled #enabled abrt為auto bug report的縮寫 用于bug報告 關閉 abrt-oops.service enabled #關閉 abrt-vmcore.service enabled #關閉 abrt-xorg.service enabled #關閉 abrtd.service enabled #關閉 accounts-daemon.service enabled atd.service enabled auditd.service enabled #安全審計 保留 autovt@.service enabled #登陸相關 保留 avahi-daemon.service enabled bluetooth.service enabled chronyd.service enabled crond.service enabled #定時任務 保留 cups.service enabled dbus-org.bluez.service enabled dbus-org.fedoraproject.FirewallD1.service enabled dbus-org.freedesktop.Avahi.service enabled dbus-org.freedesktop.ModemManager1.service enabled dbus-org.freedesktop.nm-dispatcher.service enabled display-manager.service enabled dmraid-activation.service enabled firewalld.service enabled gdm.service enabled getty@.service enabled #tty控制臺相關 保留 initial-setup-reconfiguration.service enabled irqbalance.service enabled #優化系統中斷分配 保留 iscsi-onboot.service enabled iscsi.service enabled kdump.service enabled #內核崩潰信息捕獲 自定 ksm.service enabled ksmtuned.service enabled libstoragemgmt.service enabled libvirtd.service enabled lvm2-monitor.service enabled mcelog.service enabled mdmonitor.service enabled microcode.service enabled #處理器穩定性增強 保留 ModemManager.service enabled multipathd.service enabled NetworkManager-dispatcher.service enabled #網卡守護進程 關閉 NetworkManager-wait-online.service enabled #網卡守護進程 關閉 NetworkManager.service enabled ntpd.service enabled postfix.service enabled qemu-guest-agent.service enabled rhel-autorelabel-mark.service enabled rhel-autorelabel.service enabled rhel-configure.service enabled rhel-dmesg.service enabled rhel-domainname.service enabled rhel-import-state.service enabled rhel-loadmodules.service enabled rhel-readonly.service enabled rngd.service enabled rpcbind.service enabled rsyslog.service enabled #日志服務 保留 rtkit-daemon.service enabled smartd.service enabled sshd.service enabled #ssh登陸 保留 sysstat.service enabled systemd-readahead-collect.service enabled #內核調用--預讀取 保留 systemd-readahead-drop.service enabled #內核調用--預讀取 保留 systemd-readahead-replay.service enabled #內核調用--預讀取 保留 tuned.service enabled #保留 udisks2.service enabled vdo.service enabled vgauthd.service enabled vmtoolsd-init.service enabled vmtoolsd.service enabled avahi-daemon.socket enabled cups.socket enabled dm-event.socket enabled iscsid.socket enabled iscsiuio.socket enabled lvm2-lvmetad.socket enabled lvm2-lvmpolld.socket enabled rpcbind.socket enabled virtlockd.socket enabled virtlogd.socket enabled default.target enabled graphical.target enabled nfs-client.target enabled remote-fs.target enabled runlevel5.target enabled unbound-anchor.timer enabled [root@localhost ~11:52:37]$
4.根據需要關閉防火墻
內網環境可以關閉,真正的企業環境是不能關閉的,只開啟必須開放的服務端口。
下面查看防火墻的狀態:
[root@localhost ~13:24:26]$ systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running) since 六 2022-03-26 11:42:26 CST; 1h 42min ago Docs: man:firewalld(1) Main PID: 811 (firewalld) Tasks: 2 CGroup: /system.slice/firewalld.service └─811 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid 3月 26 11:42:25 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon... 3月 26 11:42:26 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon. [root@localhost ~13:24:36]$
下面進行關閉:
[root@localhost ~13:24:36]$ systemctl stop firewalld [root@localhost ~13:25:41]$ systemctl disable firewalld Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@localhost ~13:25:48]$
為了進一步學習,先開啟防火墻:
[root@localhost ~13:27:07]$ systemctl start firewalld [root@localhost ~13:27:15]$
企業環境開放必要的端口,比如ssh、應用(如ODOO服務8069)、數據庫(postgresql端口5432)等。
先查看當前狀態:
[root@localhost ~13:33:29]$ firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 sources: services: dhcpv6-client ntp ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@localhost ~13:33:35]$
下面進行配置
[root@localhost ~13:35:07]$ firewall-cmd --zone=public --add-port=22/tcp --permanent success [root@localhost ~13:35:48]$ firewall-cmd --zone=public --add-port=8069/tcp --permanent success [root@localhost ~13:36:33]$ firewall-cmd --zone=public --add-port=5432/tcp --permanent success [root@localhost ~13:37:06]$
重載防火墻,查看開放的端口:
[root@localhost ~13:37:38]$ firewall-cmd --reload success [root@localhost ~13:37:54]$ firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: eth0 sources: services: dhcpv6-client ntp ssh ports: 22/tcp 8069/tcp 5432/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules: [root@localhost ~13:38:01]$
5.更改SSH服務器端口
先對sshd_config備份:
[root@ntp ~]# cp /etc/ssh/sshd_config{,.ori.20220326}
[root@ntp ~]#
[root@ntp ~]# ll /etc/ssh/sshd_config*
-rw-------. 1 root root 3907 8月 9 2019 /etc/ssh/sshd_config
-rw------- 1 root root 3907 3月 26 13:57 /etc/ssh/sshd_config.ori.20220326
[root@ntp ~]#
下面進行修改:
Port 2222 #更改連接端口 PermitRootLogin no #不允許root用戶登錄 PermitEmptyPasswords no #不允許空密碼登錄 GSSAPIAuthentication no #加快連接速度 UseDNS no #禁止反向解析,加快連接速度
重啟服務后生效,這里不做演示。
6.提權命令sudo
su - 致命的缺點是,必須知道root密碼,權限最大。sudo 支持授權管理,但是不需告知root密碼。通過sudo命令,我們可以把某些用戶權限分類,且有針對性(精細)授權給指定的普通用戶,被授權的用戶無需知道root密碼,只有管理員才是真正的root用戶,
sudo命令執行的流程或工作原理:
1) sudo可以針對單個命令授予臨時權限,sudo僅在需要時授予用戶權限,減少了用戶因為錯誤執行命令損壞系統的可能性。 2) sudo也可以用來以其它用戶身份執行命令。 3) sudo也可以記錄用戶執行的命令,以及失敗的特權獲取。
將上面建立的用戶noodles加入輪子組wheel:
[root@ntp ~]# id noodles02 uid=1003(noodles02) gid=1003(noodles02) 組=1003(noodles02),10(wheel) [root@ntp ~]# id noodles uid=1002(noodles) gid=1002(noodles) 組=1002(noodles) [root@ntp ~]# vim /etc/sudoers [root@ntp ~]# ## Same thing without a password %wheel ALL=(ALL) NOPASSWD: ALL noodles ALL=(ALL) NOPASSWD: ALL
或者
[root@ntp ~]# usermod noodles -a -G wheel [root@ntp ~]# su - noodles 上一次登錄:六 3月 26 16:50:11 CST 2022pts/0 上
7.調整SSH客戶端CRT的字符集
[root@ntp ~]# echo 'LANG="zh_CN.UTF-8"' > /etc/sysconfig/i18n [root@ntp ~]# source /etc/sysconfig/i18n [root@ntp ~]#
8.同步系統時間
每2份鐘從58.220.207.226同步一次時間:
[root@ntp ~]# crontab -l no crontab for root [root@ntp ~]# crontab -e no crontab for root - using an empty one crontab: installing new crontab [root@ntp ~]# crontab -l */2 * * * * /usr/sbin/ntpdate 58.220.207.226 &> /dev/null [root@ntp ~]#
9.更改歷史命令和超時登錄時間
查看一下當前的歷史記錄
[root@ntp ~]# history | wc -l 85
設置只顯示5條命令:
[root@ntp ~]# export HISTSIZE=5 [root@ntp ~]# history 82 crontab -l 83 history 84 history | wc -l 85 export HISTSIZE=5 86 history [root@ntp ~]#
但是,上面的命令只是臨時生效,要永久生效,需要下面設置:
[root@ntp ~]# cp /etc/profile{,.ori.20220326}
[root@ntp ~]# vim /etc/profile
在最后加入:
export TMOUT=300
export HISTSIZE=5
export HISTFILESIZE=5
進行比較:
[root@ntp ~]# diff /etc/profile.ori.20220326 /etc/profile 76a77,80 > export TMOUT=300 > export HISTSIZE=5 > export HISTFILESIZE=5 > [root@ntp ~]#
10.隱藏Linux版本信息
先查看當前版本信息,當前系統為CentOS7.8。
[root@ntp ~]# cat /etc/redhat-release CentOS Linux release 7.8.2003 (Core) [root@ntp ~]# cat /etc/issue \S Kernel \r on an \m [root@ntp ~]# cat /etc/issue.net \S Kernel \r on an \m
備份原始文件:
[root@ntp ~]# cat /etc/redhat-release > /etc/redhat-release.ori.20220326 [root@ntp ~]# cat /etc/issue | tee /etc/issue.ori.20220326 \S Kernel \r on an \m [root@ntp ~]# cat /etc/issue.net | tee /etc/issue.net.ori.20220326 \S Kernel \r on an \m
清空原文件內容:
[root@ntp ~]# >/etc/issue [root@ntp ~]# >/etc/redhat-release [root@ntp ~]# >/etc/issue.net
再次查看版本信息,無任何顯示:
[root@ntp ~]# cat /etc/redhat-release [root@ntp ~]#
11.定時清理垃圾郵件
寫一個定時任務來定期進行清理。
[root@ntp ~]# df -i 文件系統 Inode 已用(I) 可用(I) 已用(I)% 掛載點 devtmpfs 480748 408 480340 1% /dev tmpfs 484999 1 484998 1% /dev/shm tmpfs 484999 665 484334 1% /run tmpfs 484999 16 484983 1% /sys/fs/cgroup /dev/mapper/centos-root 26214400 131254 26083146 1% / /dev/vda1 524288 341 523947 1% /boot /dev/mapper/centos-home 21557248 210 21557038 1% /home tmpfs 484999 9 484990 1% /run/user/42 tmpfs 484999 1 484998 1% /run/user/0 [root@ntp ~]#
做定時任務,每天晚上0點執行一次:
[root@ntp ~]# mkdir -p /myscripts [root@ntp ~]# echo 'find /var/spool/postfix/maildrop/ -type f | xargs rm -f' >/server/scripts/del_file.sh -bash: /server/scripts/del_file.sh: 沒有那個文件或目錄 [root@ntp ~]# echo 'find /var/spool/postfix/maildrop/ -type f | xargs rm -f' >/myscripts/del_file.sh [root@ntp ~]# cat /myscripts/del_file.sh find /var/spool/postfix/maildrop/ -type f | xargs rm -f [root@ntp ~]# crontab -l */2 * * * * /usr/sbin/ntpdate 58.220.207.226 &> /dev/null [root@ntp ~]# echo '00 00 * * * /bin/sh /myscripts/del_file.sh >/dev/null 2>&1' >>/var/spool/cron/root [root@ntp ~]# crontab -l */2 * * * * /usr/sbin/ntpdate 58.220.207.226 &> /dev/null 00 00 * * * /bin/sh /myscripts/del_file.sh >/dev/null 2>&1 [root@ntp ~]#
12.禁止Linux系統被ping
對于要求很高的中小型企業,設置禁止ping也是可以的,從安全的角度說,禁止ping會增加系統的安全性。
[root@ntp ~]# vim /etc/sysctl.conf [root@ntp ~]# cat /etc/sysctl.conf # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). net.ipv4.icmp_echo_ignore_all = 1 [root@ntp ~]# sysctl -p #刷新生效。
上面要開放ping功能,只需將1改成0,即:
net.ipv4.icmp_echo_ignore_all = 0
安全圈
一顆小胡椒
LemonSec
看雪學苑
黑客技術與網絡安全
安全圈
系統安全運維
LemonSec
嘶吼專業版
安全圈
ChaMd5安全團隊
HACK之道