实验环境:
需要两台主机,一台单网卡(node2),一台双网卡(node1)
node1:
[root@westos_student50 Desktop]# vim /etc/sysconfig/network-scripts/ifcfg-ens160
[root@westos_student50 Desktop]# vim /etc/sysconfig/network-scripts/ifcfg-ens192
node2:
[root@westos_student50 Desktop]# vim /etc/sysconfig/network-scripts/ifcfg-ens160
- 火墙管理工具切换
打开iptables:
[root@node1 Desktop]# dnf install iptables-services -y下载iptables
[root@node1 Desktop]# systemctl disable --now firewalld关闭火墙
[root@node1 Desktop]# systemctl mask firewalld锁定
[root@node1 Desktop]# systemctl enable --now iptables.service 开启iptables
[root@node1 Desktop]# systemctl status iptables.service 查看状态
打开firewalld:
[root@node1 Desktop]# systemctl disable --now iptables.service 关闭iptables
[root@node1 Desktop]# systemctl unmask firewalld.service 解锁火墙
[root@node1 Desktop]# systemctl mask iptables.service 锁定
[root@node1 Desktop]# systemctl enable --now firewalld.service 开启火墙
[root@node1 Desktop]# systemctl status firewalld.service 查看状态
2.iptables的使用
火墙策略的永久保存
[root@node1 Desktop]# iptables -F刷掉原有火墙策略
[root@node1 Desktop]# iptables -nL查看火墙策略
[root@node1 Desktop]# service iptables save
[root@node1 Desktop]# iptales-save > /etc/sysconfig/iptables
[root@node1 Desktop]# systemctl restart iptables.service
[root@node1 Desktop]# iptables -nL
3.火墙默认策略
input 输入
output 输出
forward 转发
postrouting 路由之后
prerouting 路由之前
1).默认的3张表:
filter 经过本机内核的数据(input output forward)
nat 不经过内核的数据(postrouting,prerouting,input,output)
mangle 当filter和nat表不够用时使用(input output forward postrouting,prerouting,)
2).iptables命令 :
iptables -t ##指定表名称
-n #不做解析
-L ##查看
-A #添加策略
-p ##协议
--dport ##目的地端口
-s #来源
-j 动作
ACCEPT ##允许
DROP ##丢弃
REJECT ##拒绝
SNAT ##源地址转换
DNAT ##目的地地址转换-N ##新建链
-E ##更改链名称
-X ##删除链
-D ##删除规则
-I 插入规则
-R ##更改规则
-P ##更改默认规则
[root@node1 Desktop]# iptables -t filter -L查看指定的filter表
[root@node1 Desktop]# iptables -t filter -A INPUT -j ACCEPT 添加策略
[root@node1 Desktop]# iptables -t filter -L显示带名称的
[root@node1 Desktop]# iptables -t filter -A INPUT -s 192.168.0.70 -j REJECT 插入策略拒绝
[root@node1 Desktop]# iptables -nL(火墙从上到下读取,前边有一条符合,后边的就不再读取)
[root@node1 Desktop]# iptables -t filter -I INPUT 3 -s 192.168.0.71 -j REJECT 插入策略到第三行
[root@node1 Desktop]# iptables -nL
[root@node1 Desktop]# iptables -t filter -I INPUT -s 192.168.0.72 -j REJECT 插入策略到第一行
[root@node1 Desktop]# iptables -nL
[root@node1 Desktop]# iptables -t filter -I INPUT -p tcp --dport 22 -s 192.168.0.73 -j REJECT 插入策略
[root@node1 Desktop]# iptables -nL
[root@node1 Desktop]# iptables -N redhat新建redhat链
[root@node1 Desktop]# iptables -nL
[root@node1 Desktop]# iptables -E redhat westos把redhat链名字改为westos
[root@node1 Desktop]# iptables -nL
[root@node1 Desktop]# iptables -D INPUT 3删除input第三行
[root@node1 Desktop]# iptables -nL
[root@node1 Desktop]# iptables -X westos删除westos链
[root@node1 Desktop]# iptables -nL
[root@node1 Desktop]# iptables -R INPUT 1 -p tcp --dport 22 -j ACCEPT
[root@node1 Desktop]# iptables -nL
[root@node1 Desktop]# iptables -P INPUT DROP默认规则改为DROP
[root@node1 Desktop]# iptables -nL
[root@node1 Desktop]# iptables -P INPUT ACCEPT
[root@node1 Desktop]# iptables -nL
数据包状态
RELATED #建立过连接的
ESTABLISHED ##正在连接的
NEW ##新的
[root@node1 Desktop]# iptables -F
[root@node1 Desktop]# service iptables save
[root@node1 Desktop]# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@node1 Desktop]# iptables -A INPUT -i lo -m state --state NEW -j ACCEPT
[root@node1 Desktop]# iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT
[root@node1 Desktop]# iptables -A INPUT -p tcp --dport 53 -m state --state NEW -j ACCEPT
[root@node1 Desktop]# iptables -A INPUT -j REJECT
[root@node1 Desktop]# iptables -nL
nat表中的dnat snat
1.1.1.70主机中:
[root@node2 Desktop]# vim /etc/sysconfig/network
[root@node2 Desktop]# nmcli connection reload
[root@node2 Desktop]# nmcli connection up ens160
1.1.1.60或者192.168.0.60主机中:
[root@node1 Desktop]# iptables -t nat -A POSTROUTING -o ens160 -j SNAT --to-source 192.168.0.60
[root@node1 Desktop]# sysctl -a | grep ip_forward
[root@node1 Desktop]# vim /etc/sysctl.conf
[root@node1 Desktop]# iptables -t nat -A PREROUTING -i ens160 -j DNAT --to-dest 1.1.1.70
[root@node1 Desktop]# iptables -t nat -nL
在1.1.1.70主机中:
[root@node2 Desktop]# route -n
[root@node2 Desktop]# ping 192.168.0.60
1 firewalld的开启
[root@node1 Desktop]# systemctl disable iptables
[root@node1 Desktop]# systemctl unmask firewalld.service
[root@node1 Desktop]# systemctl mask iptables
[root@node1 Desktop]# systemctl enable --now firewalld
[root@node1 Desktop]# systemctl status firewalld.service
2.关于firewalld的域
[root@node1 Desktop]# firewall-cmd --permanent --add-service=http
[root@node1 Desktop]# firewall-cmd --reload 重启火墙
[root@node1 Desktop]# firewall-cmd --state 查案火墙状态
[root@node1 Desktop]# firewall-cmd --get-active-zones查看活跃的域
[root@node1 Desktop]# firewall-cmd --list-all列出当前默认域
[root@node1 Desktop]# firewall-cmd --list-all --zone=trusted查询指定域
[root@node1 Desktop]# firewall-cmd --get-default-zone查询默认域
[root@node1 Desktop]# firewall-cmd --set-default-zone=trusted设定默认域
[root@node1 Desktop]# firewall-cmd --get-services查询所有支持的服务
[root@node1 Desktop]# firewall-cmd --permanent --remove-service=http移除http服务
[root@node1 Desktop]# firewall-cmd --add-source 192.168.0.70 --zone=trustedzhiding指定数据来源访问指定域
[root@node1 Desktop]# firewall-cmd --remove-source 192.168.0.70 --zone=trusted删除自定域中的数据来源
[root@node1 Desktop]# firewall-cmd --set-default-zone=public指定为public域
[root@node1 Desktop]# firewall-cmd --remove-interface=ens160 --zone=public删除指定域的网络接口
[root@node1 Desktop]# firewall-cmd --add-interface=ens160 --zone=trusted添加指定域的网络接口
[root@node1 Desktop]# firewall-cmd --list-all --zone=trusted
[root@node1 Desktop]# firewall-cmd --change-interface=ens160 --zone=public更改网络接口到指定域
[root@node1 Desktop]# firewall-cmd --list-all --zone=trusted
[root@node1 Desktop]# firewall-cmd --list-all --zone=public
3. firewalld的高级规则
[root@node1 Desktop]# firewall-cmd --direct --get-all-rules查看高级规则
[root@node1 Desktop]# firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -s 192.168.0.70 -p tcp --dport 80 -j REJECT设置不让192.168.0.70访问
4.firewalld中的NAT
SNAT:
[root@node1 Desktop]# firewall-cmd --permanent --add-masquerade
[root@node1 Desktop]# firewall-cmd --reload
DNAT:
[root@node1 Desktop]# firewall-cmd --add-forward-port=port=22:proto=tcp:toaddr=1.1.1.70
[root@node1 Desktop]# firewall-cmd --reload
暑期编程PK赛 得CSDN机械键盘等精美礼品!



