新闻公告

文章详情

了解最新动态和产品更新
获取帮助文档和使用指南

返回新闻列表

Linux系统加固配置

2026/2/27 14:58:13 帮助中心
一、内核参数加固:
vim /etc/sysctl.conf
net.ipv4.ip_forward=1 //路由转发功能
net.ipv4.conf.all.log_martians=1 //启用日志记录导演的IP地址数据包
net.ipv4.conf.all.send.redirects=0 //禁用所有接口发icmp重定向
net.ipv4.conf.all.accept_source_route=0  //禁用源路由数据包
net.ipv4.conf.all.accept_redirects=0 //禁用接受icmp重定向
net.ipv4.conf.all.echo.ignore_broadcasts=1 //禁用对ping广播响应
net.ipv4.conf.all.rp_filter=1 //从同一个接口响应查询,而不是另一个接口,防IP欺骗
net.ipv4.tcp_syncookies=1 //防syn的dos攻击 
sysctl -p /etc/sysctl.conf //立即生效

二、授权认证加固
1、登陆提醒
vim /etc/issue.net
所有认证登陆已全程记录,不要做环事

2、防止按错到热启动重启系统
rm -f /etc/systemd/system/ctrl-alt-del.trage
rm -rf /usr/lib/systemd/system/ctrl-alt-del.target
vim /etc/systemd/system.conf
ctrlAltDelBurstAction=none
systemctl daemon-reexec

3、设置终端超时退出
vim /etc/profile
export TMOUT=300

4、禁止交互式启动
vim /etc/sysconfig/init
PRMOMPT=no

5、单用户模式root进入设备密码
vim /etc/sysconfig/init
SINGLE=/sbin/sulogin //增加一行

6、修改grub2的默认密码
grub2-mkpasswd-pbkdf2
vim /etc/grub2.cfg  //追加如下,最后一行是上面命令生成的密码哈希,直接复制过来
set superusers='root'  //超级管理员用户
password_pbkdf2 root 
grup.pbkdf2.sha512.XXXXXXXXXX

7、限制用户登陆系统
usermod -L -s /sbin/nologin   admin

8、禁止一些用户使用su命令切换root
vim /etc/pam.d/su  //只允许root和wheel群组账户使用su命令
auth   reuired pam_wheel.so  user_uid

9、防止su切换把当前用户环境变量带入其它环境
vim /etc/login.defs
ALWAYS_SET_PATH=yes

10、设置口令有效期
PASS_MAX_DAYS   90
PASS_MIN_DAYS    0
PASS_MIN_LEN       5
PASS_WARN_AGE   7

11、设置密码的复杂度(两个文件前面添加两行)
vim /etc/pam.d/password-auth
password     requisite    pam_pwquality.so minlen=8 minclass=3 enforce_for_root try_first pass  local_users_only retry=3 dcredit=0 ucredit=0 lcredit=0 ocredit=0
password     requisite    pam_pwhistory.so use_authtok remember=5 enforce_for_root
vim /etc/pam.d/system-auth
password     requisite    pam_pwquality.so minlen=8 minclass=3 enforce_for_root try_first pass  local_users_only retry=3 dcredit=0 ucredit=0 lcredit=0 ocredit=0
password     requisite    pam_pwhistory.so use_authtok remember=5 enforce_for_root

12、密码错误3次锁定300秒(两个文件都加入三行)
vim /etc/pam.d/password-auth
vim /etc/pam.d/system-auth
auth   required            pam_faillock.so preauth audit deny=3 even_deny_root unlock_time=300
auth   [default=die]     pam_faillock.so authfail audit deny=3 even_deny_root unlock_time=300
auth   sufficient           pam_faillock.so authsucc audit deny=3 even_deny_root unlock_time=300

13、删除无主文件
find / -nouser
rm -f filename  //删除查到的文件

14、删除群组ID不存在文件
find / -nogroup
rm -r filename

15、限制at命令使用权限只能是root
rm -f /etc/at.deny
touch /etc/at.allow
chown root:root /etc/at.allow
chmod og-rwx /etc/at.allow

16、限制cron命令使用权限只能是root
rm -f /etc/cron.deny
touch /etc/cron.allow
chown root:root /etc/cron.allow
chmod og-rwx /etc/cron.allow

17、禁用其它用户sudo提权
vim /etc/sudoers
#%wheel ALL=(ALL)  ALL //注释掉

18、ssh服务加固 
vim /etc/ssh/sshd_config
MaxAuthTries 3  //错三次锁60秒
PermitEmptyPasswords no  //禁止使用空密码连接
lgnoreRhosts yes //禁止rhosts信任主机无密码登录
PrintLastLog  yes  //开户登陆提示上一次的最后登陆时间
ClientAliveInterval 900
ClientAliveCountMax 0  //900秒无操作自动断
ListenAddress 192168.1.10 //指定本机哪个IP监听
DenyUsers $admin  //禁止admin用ssh连接
PermitRootLogin no //禁止root远程登陆,也不能普通用户切root

三、防火墙配置安全
1、iptables安装
dnf install iptables policycoreutils 
service iptables start 
systemctl enable --now iptables

iptables -F   //清规则 

步骤2允许所有来源的IP访问TCP22端口(C)penSSH
iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -m conntrack --ctstate ESTABLISHED -j ACCEPT

步骤3允许本机访问内网(192.168.0.0/24)所有主机的TCP22端口(OpenSSH)
iptables -A OUTPUT -p tcp -s 192.168.0.0/24 --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -p tcp --sport 22 -m conntrack --ctstate ESTABLISHED -j ACCEPT

4、禁止主动访问其它,防御反弹攻击 
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP

5、步骤5允许本机ping其他主机
iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT

步骤6允许本机访问远程主机的UDP53端口(DNS)
iptables -A INPUT -p udp -m udp --sport 53 -j ACCEPT
iptables -A OUTPUT -p udp -m udp --dport 53-j ACCEPT

步骤7允许环回连接
iptables -A INPUT -i lo -jACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

步骤8允许已建立和相关传入连接
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

步骤9允许已建立的传出连接
iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED -j ACCEPT

保存配置service iptables save 
 
2、iptables当路由器用
开启ip路由转发功能
sysctl -w net.ipv4.ip_forward=1

配置SNAT并查看
iptables -t filter -A FORWARD -j ACCEPT
iptables -t nat -A POSTROUTING -o ens3 -s 192.168.0.0/24 -j SNAT --to 192.168.0.48
iptables -t nat -nvL --line-numbers

配置DNAT对外映射80
iptables -t nat -A PREROUTING -d 192.168.0.48 -p tcp -m tcp --dport 80 -j DNAT --to-destination192.168.0.49:80 
iptables -t nat -nvL --line-numbers //查看目的nat

service iptables save 
记住要添加路由

3、firewalld使用
dnf -y install firewalld firewall-config
systemctl unmask --now firewalld.service 
systemctl enable --now firewalld.service

开源NAT转换
步骤1开启路由转发功能
[root@Jumpserver ~]# sysctl -w net.ipv4.ip_forward=1net.ipv4.ip_forward = 1

步骤2开启地址伪装功能
[root@Jumpserver ~]# firewall-cmd --add-masqueradeSuccess

配置目的nat
firewall-cmd --zone=public --add-forward-port=port=80:proto=tcp:toport=80:toaddr=192.168.0.86
查看
[root@Jumpserver ~]# firewall-cmd --list-all

4、添加服务到防火墙
firewall-cmd --add-service=http
firewall-cmd --list-services

5、禁止ping本机
firewall-cmd --add-rich-rule='rule family=ipv4 protocol value=icmp reject'

6、当192.168.0.209访问本机5555端口时,将请求转发到本机22端口。
[root@Server ~]# firewall-cmd --add-rich-rule='rule family=ipv4 source address=192.168.0.209 forward-port port=5555 protocol=tcp to-port=22'
[root@Server ~]# firewall-cmd --add-masquerade
firewall-cmd --runtime-to-permanent 永久保存配置
firewall-cmd --reload 重新加载

三、SElinux安全
SELinux的三种工作模式为 强制模式(Enforcing) 、 宽容模式(Permissive) 和 禁用模式(Disabled) ,具体区别如下
强制模式(Enforcing) 
特点 :严格遵循安全策略,违规访问会被立即拒绝并记录日志,是生产环境的首选模式1。
适用场景 :日常运行、高安全性需求场景,确保资源访问的严格管控。

宽容模式(Permissive)
特点 :允许所有访问操作,但会记录违规行为(如拒绝访问的详细信息),不阻止程序运行,常用于问题排查和调试1。
适用场景 :系统维护、新服务部署前的测试阶段,避免因策略问题导致服务中断。

禁用模式(Disabled) 
特点 :完全关闭SELinux,系统恢复为传统自主访问控制(DAC),不执行任何安全检查1。
适用场景 :需临时禁用SELinux的场景(如已知策略冲突需修复时),但 强烈不建议长期使用 ,存在安全风险。

getenforce
dnf install selinux-policy-targeted, policycoreutils-python-utils
ps -eZ | grep httpd  //查看httpd进程上下文
semanage port -l | grep http   //查看允许httpd侦听的端口
ausearch -m avc -c httpd //查看selinux日志
semanage port -a -t http_port_t -p tcp 82 //给http加允许端口


工单