CentOS 7下Firewald防火墙使用手册

1、添加黑名单

firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="192.168.0.23" drop' 

然后reload一下firewall

firewall-cmd --reload

在这里插入图片描述
2、添加 端口白名单

查看下防火墙的状态:systemctl status firewalld
需要开启防火墙 systemctl start firewalld.service  

firewall-cmd --zone=public --list-ports       ##查看已开放的端口

2.添加8484端口到白名单 执行
 firewall-cmd --permanent --zone=public --add-port=8484/tcp

重启防火墙:添加成功之后需要重启防火墙
firewall-cmd --reload

命令含义

–zone #作用域
–add-port=8484/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效

扩展

在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service
查看已启动的服务列表:systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl --failed
查看所有打开的端口: firewall-cmd --zone=public --list-ports

更新防火墙规则: firewall-cmd --reload

查看区域信息: firewall-cmd --get-active-zones

查看指定接口所属区域: firewall-cmd --get-zone-of-interface=eth0

拒绝所有包:firewall-cmd --panic-on

取消拒绝状态: firewall-cmd --panic-off

查看是否拒绝: firewall-cmd --query-panic

firewall-cmd --state                          ##查看防火墙状态,是否是running
firewall-cmd --reload                          ##重新载入配置,比如添加规则之后,需要执行此命令
firewall-cmd --get-zones                      ##列出支持的zone
firewall-cmd --get-services                    ##列出支持的服务,在列表中的服务是放行的
firewall-cmd --query-service ftp              ##查看ftp服务是否支持,返回yes或者no
firewall-cmd --add-service=ftp                ##临时开放ftp服务
firewall-cmd --add-service=ftp --permanent    ##永久开放ftp服务
firewall-cmd --remove-service=ftp --permanent  ##永久移除ftp服务
firewall-cmd --add-port=80/tcp --permanent    ##永久添加80端口 
firewall-cmd --remove-port=80/tcp --permanent    ##永久添加80端口 
firewall-cmd --zone=public --list-ports       ##查看已开放的端口

3、

Centos7的安全加固

1.查找系统中是否存在空密码帐户。使用如下命令可以直接查看。

#awk -F: ‘($2==""){print $1}/etc/shadow

2.查找uid值为0的用户。(如果passwd文件中uid值为0的话,那么这个用户就拥有root权限。)

使用可以看到当前系统只有root帐户拥有root权限。

#awk -F: ‘($3==0){print $1}‘ /etc/passwd

3.查看密码参数的含义。

PASS_MAX_DAYS        密码有效期
 
PASS_MIN_DAYS         修改密码的最短期限

PASS_MIN_LEN            密码最短长度

PASS_WARN_AGE        密码过期提醒    

4.设置会话超时。

修改 vi/etc/profile文件。在最后添加参数export TMOUT=600.
5.禁止root通过ssh远程登录。

修改vi /etc/ssh/sshd_config文件,将配置参数#PermitRootLogin yes改成PermitRootLogin no.

Centos7的基础优化

DNS
网络yum源
epel源
同步时间
安装vim
设置最大打开文件描述符数
禁用selinux
关闭防火墙
优化ssh连接速度
内核参数优化
设置vim退格键删除最后一个字符类型
#!/bin/bash
#author yundd by 
#this script is only for CentOS 7.x
#check the OS

platform=`uname -i`
if [ $platform != "x86_64" ];then 
echo "this script is only for 64bit Operating System !"
exit 1
fi
echo "the platform is ok"
cat << EOF
 your system is CentOS 7 x86_64  
EOF

#添加公网DNS地址
cat >> /etc/resolv.conf << EOF
nameserver 114.114.114.114
EOF
#Yum源更换为国内阿里源
yum install wget telnet -y
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

#添加阿里的epel源
#add the epel
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# rpm -ivh http://dl.Fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm

#yum重新建立缓存
yum clean all
yum makecache
#同步时间
 ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
yum -y install ntp
/usr/sbin/ntpdate cn.pool.ntp.org
echo "* 4 * * * /usr/sbin/ntpdate cn.pool.ntp.org > /dev/null 2>&1" >> /var/spool/cron/root
systemctl  restart crond.service

#安装vim
yum -y install vim
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#安装lrzsz实现win与linux之间的文件互相下载上传
yum -y install lrzsz

#设置最大打开文件描述符数
echo "ulimit -SHn 102400" >> /etc/rc.local
cat >> /etc/security/limits.conf << EOF
*           soft   nofile       655350
*           hard   nofile       655350
EOF


#禁用selinux
sed -i ‘s/SELINUX=enforcing/SELINUX=disabled//etc/selinux/config
setenforce 0

#关闭防火墙
systemctl disable firewalld.service 
systemctl stop firewalld.service 

#set ssh
sed -i ‘s/^GSSAPIAuthentication yes$/GSSAPIAuthentication no//etc/ssh/sshd_config
sed -i ‘s/#UseDNS yes/UseDNS no//etc/ssh/sshd_config
systemctl  restart sshd.service


#内核参数优化
cat >> /etc/sysctl.conf << EOF
vm.overcommit_memory = 1
net.ipv4.ip_local_port_range = 1024 65536
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_syncookies=1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout=120
net.ipv4.tcp_keepalive_time = 2400
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000 
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_abort_on_overflow = 0
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.ipv4.netfilter.ip_conntrack_max = 2097152
net.nf_conntrack_max = 655360
net.netfilter.nf_conntrack_tcp_timeout_established = 1200
EOF
/sbin/sysctl -p

#vim定义退格键可删除最后一个字符类型
echo ‘alias vi=vim‘ >> /etc/profile
echo ‘stty erase ^H‘ >> /etc/profile
cat >> /root/.vimrc << EOF
set tabstop=4
set shiftwidth=4
set expandtab
syntax on
"set number
EOF

# update soft
# yum -y update

参考链接 :
对于linux的进站白名单ip和端口的配置(iptables) : https://blog.csdn.net/qq_28198181/article/details/82349402

https://blog.csdn.net/chenzi5358/article/details/100630513

https://blog.csdn.net/terence_csdn/article/details/86042263

发布了313 篇原创文章 · 获赞 57 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_40907977/article/details/104229674