本文共 7507 字,大约阅读时间需要 25 分钟。
CentOS防火墙的配置
yum install iptables -y
service iptables status # 查看状态service iptables start # 开启service iptables stop # 关闭service iptables restart # 重启service iptables save # 保存通过命令行设置的规则到文件 iptables:将防火墙规则保存到 /etc/sysconfig/iptables: [确定]
chkconfig [--level 35] iptables on # iptables在3和5模式下开机自启动; chkconfig iptables off # 禁止iptables开机自启动;chkconfig --list|grep iptables # 查看状态;iptables 0:off 1:off 2:off 3:on 4:off 5:on 6:off
#允许所有入栈规则iptables -P INPUT ACCEPT#清空默认所有规则iptables -F#清空自定义的所有规则iptables -X#计数器置0iptables -Z
#允许本地回环接口(即运行本机访问本机)iptables -A INPUT -i lo -j ACCEPT# 允许已建立的或相关连的通行iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT#允许所有本机向外的访问iptables -A OUTPUT -j ACCEPT# 允许访问22端口iptables -A INPUT -p tcp --dport 22 -j ACCEPT#允许访问80端口iptables -A INPUT -p tcp --dport 80 -j ACCEPT#允许FTP服务的21和20端口iptables -A INPUT -p tcp --dport 21 -j ACCEPTiptables -A INPUT -p tcp --dport 20 -j ACCEPT#如果有其他端口的话,规则也类似,稍微修改上述语句就行#允许pingiptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT#禁止其他未允许的规则访问iptables -A INPUT -j REJECT #(注意:如果22端口未加入允许规则,SSH链接会直接断开。)iptables -A FORWARD -j REJECT#常用端口iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPTiptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPTiptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 23 -j ACCEPTiptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPTiptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPTiptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPTiptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
#如果只是想屏蔽IP的话 “3、开放指定的端口” 可以直接跳过。#屏蔽单个IP的命令是iptables -I INPUT -s 123.45.6.7 -j DROP#封整个段即从123.0.0.1到123.255.255.254的命令iptables -I INPUT -s 123.0.0.0/8 -j DROP#封IP段即从123.45.6.1到123.45.6.254的命令iptables -I INPUT -s 123.45.6.0/24 -j DROP
iptables -D INPUT -s **.**.**.** -j DROP
#允许对外请求的返回包iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT#允许icmp包通过iptables -A INPUT -p icmp --icmp-type any -j ACCEPT#允许来自于lo接口的数据包,如果没有此规则,将不能通过127.0.0.1访问本地服务iptables -A INPUT -i lo -j ACCEPT#过滤所有非以上规则的请求iptables -P INPUT DROP#添加内网ip信任(接受其所有TCP请求)#注:(**.**.**.**)为IP,下同iptables -A INPUT -p tcp -s **.**.**.** -j ACCEPT
[root@BAK_18 ~]# iptables -L -nChain INPUT (policy ACCEPT)target prot opt source destinationChain FORWARD (policy ACCEPT)target prot opt source destinationChain OUTPUT (policy ACCEPT)target prot opt source destination
将所有 iptables 以序号标记显示,执行:iptables -L -n --line-numbers比如要删除 INPUT 里序号为 8 的规则,执行:iptables -D INPUT 8
保存规则:
service iptables save
iptables 的配置文件为 /etc/sysconfig/iptables编辑配置文件:vi /etc/sysconfig/iptables文件中的配置规则与通过iptables命令配置,语法相似:如,通过iptables命令配置,允许访问80端口:iptables -A INPUT -p tcp --dport 80 -j ACCEPT那么,在文件中配置,只需要去掉句首的 iptables ,添加如下内容:-A INPUT -p tcp --dport 80 -j ACCEPT保存退出。重启iptables服务使其生效:service iptables restart
systemctl status firewalld.service # 查看状态systemctl start firewalld.service # 开启systemctl stop firewalld.service # 关闭systemctl restart firewalld.service # 重启firewall-cmd --version # 查看版本firewall-cmd --state # 查看状态firewall-cmd --get-active-zones # 获取启用的zone
[root@node1 ~]# systemctl start firewalld.service[root@node1 ~]# systemctl restart firewalld.service[root@node1 ~]# systemctl status firewalld.service● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: active (running) since 三 2018-05-23 09:06:50 CST; 2s ago Docs: man:firewalld(1) Main PID: 13981 (firewalld) CGroup: /system.slice/firewalld.service └─13981 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid5月 23 09:06:50 node1 systemd[1]: Starting firewalld - dynamic firewall daemon...5月 23 09:06:50 node1 systemd[1]: Started firewalld - dynamic firewall daemon.[root@node1 ~]# systemctl stop firewalld.service[root@node1 ~]# systemctl status firewalld.service● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:firewalld(1)5月 23 09:06:31 node1 systemd[1]: Starting firewalld - dynamic firewall daemon...5月 23 09:06:32 node1 systemd[1]: Started firewalld - dynamic firewall daemon.5月 23 09:06:39 node1 systemd[1]: Stopping firewalld - dynamic firewall daemon...5月 23 09:06:40 node1 systemd[1]: Stopped firewalld - dynamic firewall daemon.[root@node1 ~]# firewall-cmd --version # 查看版本0.4.4.4[root@node1 ~]# firewall-cmd --state # 查看状态running[root@node1 ~]# firewall-cmd --get-active-zones # 获取启用的zonepublic interfaces: enp0s25
systemctl enable firewalld # iptables在3和5模式下开机自启动; systemctl disable firewalld # 禁止iptables开机自启动;systemctl list-unit-files |grep firewalld.service # 查看状态;
[root@node1 java]# firewall-cmd --zone=public --list-allpublic (default, active) interfaces: eno16777984 sources: services: dhcpv6-client mdns ssh ports: masquerade: no forward-ports: icmp-blocks: rich rules:
# 列出已配置好可用的服务, 位于 /usr/lib/firewalld/services/ 下[root@node1 java]# firewall-cmd --get-servicesamanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns ftp high-availability http https imaps ipp ipp-client ipsec kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp open*** pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind samba samba-client smtp ssh telnet tftp tftp-client transmission-client vnc-server wbem-https# 强制列出包含用户设置在/etc/firewalld/services/, 但尚未loaded的服务[root@node1 java]# firewall-cmd --get-services --permanentamanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns ftp high-availability http https imaps ipp ipp-client ipsec kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp open*** pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind samba samba-client smtp ssh telnet tftp tftp-client transmission-client vnc-server wbem-https
# 不要忘记 --permanent [root@node1 java]# firewall-cmd --zone=public --add-port=8080/tcp --permanent# OR 添加一个地址段[root@node1 java]# firewall-cmd --zone=public --add-port=5060-5061/udp --permanentsuccess# 需要reload后才启用, 热加载[root@node1 java]# firewall-cmd --reload# OR 冷加载[root@node1 java]# firewall-cmd --complete-reloadsuccess# 能看到新端口已经添加[root@node1 java]# firewall-cmd --zone=public --list-allpublic (default, active) interfaces: eno16777984 sources: services: dhcpv6-client mdns ssh ports: 8080/tcp masquerade: no forward-ports: icmp-blocks: rich rules: # 删除一个端口firewall-cmd --permanent --zone=public --remove-port=8080/tcpfirewall-cmd --permanent --zone=public --remove-port=8080/udp[root@node1 ~]# firewall-cmd --version # 查看版本0.4.4.4
转载于:https://blog.51cto.com/moerjinrong/2351252