Linux之Redhat中火墙的高级配置------firewalld和iptables
1.火墙基本结构:
2.进入火墙配置界面firewall-config
监控命令:watch -n 1 firewall-cmd --list-all
火墙的9种类型:
【1】在图形化界面配置火墙类型
确定后
【2】在图形化界面配置火墙支持的服务
【3】在图形化界面配置允许使用的端口,端口需要更改相应配置文件的端口并重启服务。
注:runtime表示临时更改,permanent表示永久更改,需要reload和重启火墙,测试在浏览器,安装httpd
3.firewall常用指令
【1】firewall-cmd --state 火墙状态
【2】firewall-cmd --get-active-zones 查看有效区域IP和网卡
【3】firewall-cmd --get-default-zone 查看默认区域同上
【4】firewall-cmd --get-zones 查看所有的火墙类型
【5】firewall-cmd --zone=public --list-all 查看public类型的区域
【6】firewall-cmd --get-services 查看火墙支持的服务
【7】firewall-cmd --list-all-zones 列出所有火墙类型的详细信息
【8】firewall-cmd --set-default-zone=dmz 设置火墙类型为dmz
【9】firewall-cmd --add-port=8080/tcp 添加端口/etc/firewalld/zones/public.xml
【10】firewall-cmd --remove-port=8080/tcp 移除端口也可以更改上一步的文件然后重起火墙
4.更改服务名称
在/etc/firewalld/services/中将文件重命名然后重起服务即可
5.firewall基本配置
【1】firewall-cmd --complete-reload 可以中断已经连接的设备
测试:
【2】firewall-cmd --permanent --add-source=IP --zone=block 设置指定IP的火墙策略
测试用IP连接主机,连不上
【3】firewall-cmd --permanent --remove-source=IP --zone=block 移除策略
【4】firewall-cmd --permanent --zone=internal --add-interface=eth0 添加网卡
【5】firewall-cmd --permanent --zone=internal --change-interface=eth0 更改网卡
【6】firewall-cmd --permanent --zone=internal --remove-interface=eth0 移除网卡
【7】屏蔽指定IP访问http
firewall -cmd --direct --add-rule ipv4 filter INPUT 1(加一条策略) -s(指定源) IP(屏蔽主机的IP) -p(类型) tcp --dport(指定端口) 22 -j(动作) REJECT
测试在浏览器进行
【7】地址转换(当你链接主机1时将转接到主机2)
注:删除主机2的文件:/root/.ssh/known_hosts。
firewall-cmd --add-forward-port=port=22:proto=tcp:toport=22:toaddr=IP(想转换的IP址)
测试:
注:输入的密码为转接的主机密码
【8】IP转接
开启策略:firewall-cmd --add-masquerade
配置策略一:
firewall-cmd --permanent --zone=<ZONE> --add-rich-rule=’rule family=ipv4 source address=172.25.37.137(可以指定网段/24) forward-port=port=80 protocol=tcp
to-port=8080’
配置策略二:
firewall-cmd--permanent--zone=<ZONE>--add-forward-port=port=80:proto=tcp:toport=8080:toaddr=172.25.37.137
注:网关两个配置文件/etc/sysconfig/network /etc/sysconfig/network-script/ifcfg-eth0 添加GATEWAY=IP(网关IP)
测试:
转接主机:
要链接的客户端:
6.iptables配置火墙
安装软件yum install iptables
启用iptables
【1】systemctl stop firewalld
【2】systemctl disable firewalld
【3】systemctl mask firewalld
【4】systemctl start iptables
【5】systemctl enable iptables
7.iptables常用指令
【1】iptables -t filter -L 查看策略
【2】iptables -F 清除策略
【3】service iptables save 保存策略
【4】策略保存的文件:/etc/sysconfig/iptables
8.在iptables中添加策略
【1】iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT -I默认添加位置为第一个
【2】iptables -A INPUT 1 -p tcp --dport 80 -j ACCEPT 允许80端口链接
【3】iptables -A INPUT -i lo -j ACCEPT i表示添加,给lo添加策略ACCEPT
【4】iptables -A INPUT -s 172.25.254.92 -p tcp --dport 22 -j ACCEPT 允许92通过22端口链接
9.在iptables中删除策略
【1】iptables -D INPUT 8 删除第8个策略
【2】iptables -D INPUT +策略
【3】过滤策略,方便查看策略在表中的位置
iptables -nl | grep -E "INPUT|target" -v |cat -b
【4】iptables -R INPUT 2(要改的策略) -p tcp ...(更改后的策略)
10.策略表格
【1】iptables -N westos 添加表
【2】iptables -E westos yuchen 改表的名称
【3】iptables -X yuchen 删除表
11.iptables火墙策略补充
【1】netstat --antlpe 查看所有端口信息
【2】iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT 访问过直接通过
【3】iptables -A INPUT -m state --state NEW -i lo -j ACCEPT 自回环第一次通过
【4】iptables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT 22端口通过
【5】iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT 80端口通过
【6】iptables -A INPUT -j REJECT 其他的拒绝
12.ssh访问地址转换
【1】iptables -t nat -nL 查看nat表
【2】sysctl -a | grep ip_forward 查看ip_forward的状态
【3】ip_forward的状态设为1
vim /etc/sysctl.conf
【4】sysctl -p 加载
【5】iptables -t nat -A POSTROUTING -o eth0 -j SANT --to-source 172.25.254.92 添加策略将访问eth0的源地址转为172.25.254.92
【6】将客户端网关设置为服务器同网段的IP
13.IP转接
【1】iptables -t nat -nL 查看nat表
【2】iptables -t nat -A PREROUTING -i eth0 -d 172.25.254.137 -j DNAT --to-dest 172.25.37.137 添加策略
【3】测试: