Linux系统下的iptables
iptables概述
iptables也是火墙,在企业7之前使用firewall,企业7以及企业7之后iptables和firewalld均可以使用,并且iptables相对于firewalld而言,精度更高,它能完成firewalld无法完成的精度较高的状态设定。
iptables和firewalld之间最本质的不同是
iptales service 在
/etc/sysconfig/iptables中储存配置
firewalld 将配置存储在
/usr/lib/firewalld和
/etc/firewalld/中的各种xml文件中。
从firewalld转换到iptables(二者选一)
(1)下载iptables服务的软件包
yum install iptables-service ##下载iptables的服务 yum list iptables-service ##查看其是否安装完成
(2)关闭firewalld
systemctl stop firewalld systemctl disable firewalld systemctl mask firewalld ##锁住irewalld
(3)启用iptables
systemctl start iptables systemctl enable iptables
iptables参数
参数 | 说明 |
---|---|
-t | 指定表 |
-A | 添加某条链中的策略(默认添加到最后一条 |
-D | 删除某条链中的策略 |
-I | 插入某条链的策略(默认添加到第一条,也可指定位置 |
-S | 列出具体的策略 |
-nL | 查看添加策略后最终生成的表 |
-F | 刷新(清空)当前的策略,保存到配置文件中的虽然清空,但是依然存在 |
-Z | 将系统的记录的包全部清空 |
-N | 新建一条链 |
-X | 删除链 |
-P | 改变策略的动作(只能修改为ACCEPT或DROP,不能修改为REJECT |
-E | 重命名链的名称 |
iptables-save 或者service iptables sage | 保存到配置文件/etc/sysconfig/iptables |
iptables -nL
iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT ##在INPUT链中添加策略,默认在末尾添加
iptables -t filter -I INPUT 1 -s 172.25.254.204 -p tcp --dport 22 -j ACCEPT ##在INPUT链中添加策略,默认在前
iptables -D INPUT 2 ##删除链INPUT的第2条策略
iptables -t filter - R INPUT 1 -s 172.25.254.-p tcp --dport 22 -j DROP ##对链INPUT进行策略的修改
iptables -S INPUT ##查看详细策略
iptables -N redhat ##新建一条链为redhat
iptables -E redhat WESTOS ##把redhat重命名为WESTOS
iptables -X WESTOS ##链WESTOS删除
iptables -P INPUT DROP ##改变INPUT的策略为DROP,不能改为REJECT,会报错
详细信息可以
man iptables查看
火墙的策略优化
当其他主机对该主机进行服务访问时,该主机上的火墙会对其进行判断是不是新的服务,然后再进行服务允许查看,这样每访问一次服务,就会对火墙里的策略进行读取,这样降低了火墙的效率。
所以为了减少对火墙包的读取,提高火墙效率,对Iptables进行火墙策略的优化。
优化步骤
iptables -F ##先清空之前的火墙策略 iptables -nL ##查看刷新是否成功
1.火墙对访问的服务进行校验,如果是以前访问过的服务,则直接ACCEPT,不再做下一步检验(如果服务已经建立或者处于运行状态,登录过的服务直接进入,如果是新的,进入下一条)
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCPET
2.如果发现该服务是第一次到达
iptables -A INPUT -m state NEW -i lo -j ACCEPT ##该服务是法第一次到达的话,执行这一条命令
3.新服务进入校验
iptables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT ##20端口是sshd服务 ##如果是sshd服务的话则允许 iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT ##80端口是httpd服务 ,如果httpd服务的话则允许 其他的服务依次按照这个进行策略的书写
4.查看策略是否加入成功并保存到配置文件中,防止被刷新
iptables -nL ##查看火墙策略是否添加上 iptables-save > /ec/sysconfig/iptables ##保存到配置文件中,防止被刷新 systemctl restart iptables
iptables的地址伪装
在客户机192.168.0.204这个主机上ping172.25.254.4物理机Ping不通
让192.168.0.204ping通172.25.254.65
操作步骤:
1.在双网卡主机上,打开内核路由功能
sysctl -a | grep ip_forward 如果 net ip_forward = 0,则进行以下操作 vim /etc/sysconfig/sysctl net ip_forward = 1 systemctl restart network sysctl -a | grep ip_forward ##查看内核路由功能是否真的开启
2.在双网卡主机上,添加火墙策略
iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 172.25.254.104
3.在客户机(192.168.0.204)这台主机上,添加网关
cd /etc/sysconfg/network-scripts/ vim ifcfg-eth0 添加网关 GATEWAY=192.168.0.104 route -n ##查看
测试
ping 172.25.254.4 ##能Ping通
iptables的端口转发
iptables的端口转发原理和firewalld类似
操作方法:
在双网卡主机上
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 22 -j DNAT --to-dest 192.168.0.204:22 systemctl restart iptables.service
测试
在真机(172.25.254.4)上进行测试
ssh root@172.25.254.104 ifconfig ##查看这时连接的主机是 192.168.0.204
管理selinux端口标签
在
selinux=enforcing的情况下
以httpd服务端口为例
systemctl start httpd netstat -antulpe | grep httpd vim /etc/httpd/conf/httpd.conf ##打开httpd配置文件 改变监听端口 改为8080端口
测试
重启服务成功
但是改变端口为8888重启服务发现重启失败
原因是
semanage port -l | grep httpd ##查看是因为8080端口符合,但是8888端口并不在名单里
semanage port -a -t http_port_t -p tcp 8888 ##进行8888端口的添加 semange port -l | grep http ##查看8888端口是否添加成功
测试
systemctl restart httpd ##重启成功
- 关于linux系统下iptables下的管理的8种命令
- linux系统中的防火墙(iptables与firewalld)——firewalld
- Linux系统Iptables的常用防火墙配置方法
- Linux系统下使用Iptables构建静态防火墙
- linux实用资料(iptables,系统参数,NFS)
- Linux系统iptables服务
- 配置linux-Fedora系统下iptables防火墙
- linux系统上的防火墙iptables
- Linux系统Iptables防火墙使用手册
- 把iptables移植到嵌入式Linux系统
- 通过Linux系统下iptables防火墙开启/关闭指定端口方法
- Linux -- 系统安全之Iptables防火墙(1)
- Linux系统非ROOT用户80端口不能启动tomcat问题的变通办法——通过Iptables端口转发
- linux系统之iptables其二命令注解
- Linux系统Iptables端方执行按次过细讲解
- 20151208linux系统日常管理第三部分(linux下使用iptables实现内网的ftp端口映射 )扩展3
- Linux系统管理初步(四)系统防火墙控制程序iptables
- 自制上网管理系统PHP+Mysql+iptables on linux
- Linux系统学习笔记网络相关 ,firewalld和netfilter,netfilter5表5链介绍,iptables语法
- linux系统设置防火墙,熟悉iptables框架