1、centos7
1.1、开启防火墙
systemctl start firewalld.service
1.2、关闭防火墙
systemctl stop firewalld.service
先用:systemctl unmask firewalld.service
然后:systemctl start firewalld.service
1.3、开启防火墙开机启动
systemctl enable firewalld.service
1.4、关闭防火墙开机启动
systemctl disable firewalld.service
1.5、查看防火墙状态
systemctl status firewalld
firewall-cmd --state
1.6、开启端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
1.7、移除端口
firewall-cmd --zone=public --remove-port=80/tcp --permanent
firewall-cmd --permanent --remove-port=123/tcp
1.8、重启防火墙
firewall-cmd --reload
1.9、查看某个端口是否开启
firewall-cmd --query-port=80/tcp
1.10、查询开放列表
firewall-cmd --list-port
1.11、systemctl常用命令
systemctl是CentOS7的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。
systemctl start firewalld.service
systemctl stop firewalld.service
systemctl restart firewalld.service
systemctl status firewalld.service
systemctl enable firewalld.service
systemctl disable firewalld.service
systemctl is-enabled firewalld.service
systemctl list-unit-files|grep enabled
systemctl --failed
1.12、firewalld-cmd配置
firewall-cmd --version
firewall-cmd --help
firewall-cmd --state
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
2、centos6
2.1、防火墙配置文件
vim /etc/sysconfig/iptables
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8000:20000 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
2.2、service方式
chkconfig iptables on
chkconfig iptables off
service iptables status
service iptables start
service iptables stop
service iptables restart
保存配置:service iptables save
2.2、iptables方式
cd /etc/init.d/
/etc/init.d/iptables status
/etc/init.d/iptables start
/etc/init.d/iptables stop
/etc/init.d/iptables restart
2.3、添加端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8000:20000 -j ACCEPT
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3307 -j ACCEPT
iptables -D INPUT -m state --state NEW -m tcp -p tcp --dport 3307 -j ACCEPT
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 8000:20000 -j ACCEPT
2.4、设置端口转发
iptables -t nat -A PREROUTING -p tcp --dport [端口号]
-j DNAT --to-destination [目标IP]
iptables -t nat -A PREROUTING -p udp --dport [端口号]
-j DNAT --to-destination [目标IP]
iptables -t nat -A POSTROUTING -p tcp -d [目标IP] --dport [端口号]
-j SNAT --to-source [本地服务器IP]
iptables -t nat -A POSTROUTING -p udp -d [目标IP] --dport [端口号]
-j SNAT --to-source [本地服务器IP]
iptables -t nat -A PREROUTING -p tcp --dport 8001 -j DNAT --to-destination [目标服务器IP]:9001
iptables -t nat -A PREROUTING -p udp --dport 8001 -j DNAT --to-destination [目标服务器IP]:9001
iptables -t nat -A POSTROUTING -d [目标服务器IP]/32 -p tcp -m tcp --dport 9001 -j SNAT --to-source [本地服务器IP]
iptables -t nat -A POSTROUTING -d [目标服务器IP]/32 -p udp -m udp --dport 9001 -j SNAT --to-source [本地服务器IP]
iptables -t nat -A PREROUTING -p tcp --dport 8000:9000 -j DNAT --to-destination [目标服务器IP]
iptables -t nat -A PREROUTING -p udp --dport 8000:9000 -j DNAT --to-destination [目标服务器IP]
iptables -t nat -A POSTROUTING -d [目标服务器IP] -p tcp --dport 8000:9000 -j SNAT --to-source [本地服务器IP]
iptables -t nat -A POSTROUTING -d [目标服务器IP] -p udp --dport 8000:9000 -j SNAT --to-source [本地服务器IP]
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
3、Ubuntu
3.1、安装UFW防火墙
sudo apt-get install ufw
安装防火墙:sudo apt-get install ufw
开启防火墙:sudo ufw enable
sudo ufw default deny
3.2、开启防火墙
sudo ufw enable
sudo ufw default deny
3.3、防火墙常用命令
sudo ufw allow smtp
sudo ufw allow 22/tcp
sudo ufw allow 53
sudo ufw allow from 192.168.1.100
sudo ufw allow proto udp 192.168.0.1 port 53 to 192.168.0.2 port 53
sudo ufw deny smtp
sudo ufw delete allow smtp
sudo ufw status
ufw enable|disable
ufw logging on|off
ufw default allow|deny