Ubuntu配置iptables规则

Ubuntu配置防火墙,并且开机iptables自启动规则;适用于CentOS
1、登录root账号
# 切换到root账号
super@super:~$ sudo passwd root  # 设置root密码
super@super:~$ su - root         # 切换到root账号

以下操作可以在root下也可以在普通账号,但是ubuntu保存配置必须在root下.
每次添加或者删除规则,务必保存配置,以免丢失.

1、清空iptables所有配置(新系统操作)
sudo iptables -F
sudo iptables -X
sudo iptables -Z
2、放行SSH远程22端口及lookback规则:
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A OUTPUT -o lo -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
3、配置默认动作为拒绝,只允许流出数据:
sudo iptables --policy OUTPUT ACCEPT
sudo iptables --policy FORWARD DROP
sudo iptables -P INPUT DROP
# 操作这部之前,请确保放行了ssh或者其他远程端口.
4、配置放行的关联规则包
sudo iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A OUTPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
# 如果启用nat,规则需要添加
iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o ens32 -j MASQUERADE
5、Ubuntu保存iptables配置与开机启动(root账号下)
# 如果是普通用户,请切换到root
super@super:~$ su - root
Password: 
root@super:~# iptables-save > /etc/iptables-rules  # 保存配置
5.1、ubuntu配置规则开机自启动(可以普通账号下)
super@super:~$ more /etc/rc.local   # 请先配置rc.local服务自启动
#!/bin/bash
#rc.local启动

# iptables配置恢复,默认重启系统iptables配置丢失.
iptables-restore < /etc/iptables-rules

exit 0
5.2 Redhat保存iptables配置
# 方式一
/etc/init.d/iptables save
# 方式二
service iptables save 

延伸其他规则配置(规则保存在root账号下)

1、查看规则
super@super:~$ sudo iptables -L -n
super@super:~$ sudo iptables -nL --line-number # 查看详细规则条目
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED

Chain FORWARD (policy DROP)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
2、添加规则 (这个规则适用于第一次新规则增加,未保存之前.)
super@super:~$ sudo iptables -A INPUT -p tcp –dport 80 -j ACCEPT 
3、插入规则 (适用于保存了规则之后新增规则)
super@super:~$ sudo iptables -I INPUT 2 -p tcp --dport 3306 -j ACCEPT  # 第2条规则之前插入此规则
4、放行多个端口规则(如果插入规则,把-A换成-I)
super@super:~$ sudo iptables -A INPUT -s 172.21.16.0/24 -p tcp -m multiport –dport 8080,8081,8082 -j ACCEPT
5、对指定ip放行snmp协议
super@super:~$ sudo iptables -A INPUT -s 172.21.16.0/24 -p udp –dport 161 -j ACCEPT
6、禁止ping
super@super:~$ sudo iptables -A INPUT -p icmp –icmp-type 8 -s 0/0 -j DROP  # 禁止所有网段ping
super@super:~$ sudo iptables -A INPUT -p icmp --icmp-type 8 -s 172.21.16.18 -j ACCEPT # 禁止某个或者某网段ping的方式
7、开放ping协议
super@super:~$ sudo iptables -A INPUT -p icmp -m icmp –icmp-type any -j ACCEPT
8、删除规则
# 方式一删除规则
super@super:~$ sudo iptables -D INPUT 2   # 删除规则2
 # 方式二删除规则
super@super:~$ sudo iptables -D INPUT -p icmp –icmp-type 8 -s 172.21.16.18  -j DROP 

所有规则改动都必须保存,不管是Redhat还是Ubuntu

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值