学习啦>学习电脑>操作系统>Linux教程>

LINUX防火墙常用操作

时间: 春健736 分享

  学习啦小编来给大家介绍下LINUX防火墙常用操作,对于自己学习使用Linux更有帮助。下面跟着学习啦小编一起来了解一下吧。

  LINUX防火墙常用操作

  (1) 重启后永久性生效:

  开启:chkconfig iptables on

  关闭:chkconfig iptables off

  (2) 即时生效,重启后失效:

  开启:service iptables start

学习啦在线学习网   关闭:service iptables stop

  (3)Linux 防火墙开放特定端口

学习啦在线学习网   iptables是linux下的防火墙,同时也是服务名称。

  service iptables status 查看防火墙状态

学习啦在线学习网   service iptables start 开启防火墙

  service iptables stop 关闭防火墙

学习啦在线学习网   service iptables restart 重启防火墙

学习啦在线学习网   防火墙开放特定端口:

  ①文件/etc/sysconfig/iptables

  ②添加:

  -A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT

学习啦在线学习网   ★具体参照/etc/sysconfig/iptables中的配置,一般默认会有一个22端口开放★

  ★数字3306代表开放3306端口,也可以改成其他的端口.★

  ③重启防火墙

学习啦在线学习网   service iptables restart

学习啦在线学习网   保存对防火墙的设置

  serivce iptables save

学习啦在线学习网   =====================================

学习啦在线学习网   查看iptables规则及编号

学习啦在线学习网   iptables -nL –line-number

  查看iptables规则及编号

学习啦在线学习网   iptables -nL –line-number

  关闭所有的INPUT FORWARD(转发) OUTPUT的所有端口

学习啦在线学习网   iptables -P INPUT DROP

  iptables -P FORWARD DROP

学习啦在线学习网   iptables -P OUTPUT DROP

  只打开22端口

  iptables -A INPUT -p tcp –dport 22 -j ACCEPT

学习啦在线学习网   iptables -A OUTPUT -p tcp –sport 22 -j ACCEPT

  参数讲解:

  –A 参数就看成是添加一条规则

  –p 指定是什么协议,我们常用的tcp 协议,当然也有udp,例如53端口的DNS

  –dport 就是目标端口,当数据从外部进入服务器为目标端口

  –sport 数据从服务器出去,则为数据源端口使用

  –j 就是指定是 ACCEPT -接收 或者 DROP 不接收

学习啦在线学习网   禁止某个IP访问

  iptables -A INPUT -p tcp -s 192.168.1.2 -j DROP

学习啦在线学习网   –s 参数是来源(即192.168.1.2)

学习啦在线学习网   后面拒绝就是DROP

  删除规则

  iptables -D INPUT 2

  删除INPUT链编号为2的规则

  1、允许通过某一端口

学习啦在线学习网   vi /etc/sysconfig/iptables

学习啦在线学习网   -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT(允许80端口通过防火墙)

  /etc/init.d/iptables restart

学习啦在线学习网   #最后重启防火墙使配置生效

  只允许特定ip访问某端口?参考下面命令,只允许46.166.150.22访问本机的80端口。如果要设置其他ip或端口,改改即可。

  iptables -I INPUT -p TCP --dport 80 -j DROP

学习啦在线学习网   iptables -I INPUT -s 46.166.150.22 -p TCP --dport 80 -j ACCEPT

  在root用户下执行上面2行命令后,重启iptables, service iptables restart

  查看iptables是否生效:

  [root@www.ctohome.com]# iptables -L

  Chain INPUT (policy ACCEPT)

学习啦在线学习网   target prot opt source destination

  ACCEPT tcp -- 46.166.150.22 anywhere tcp dpt:http

学习啦在线学习网   DROP tcp -- anywhere anywhere tcp dpt:http

  Chain FORWARD (policy ACCEPT)

学习啦在线学习网   target prot opt source destination

  Chain OUTPUT (policy ACCEPT)

学习啦在线学习网   target prot opt source destination

  上面命令是针对整个服务器(全部ip)禁止80端口,如果只是需要禁止服务器上某个ip地址的80端口,怎么办?

  下面的命令是只允许来自174.140.3.190的ip访问服务器上216.99.1.216的80端口

学习啦在线学习网   iptables -A FORWARD -s 174.140.3.190 -d 216.99.1.216 -p tcp -m tcp --dport 80 -j ACCEPT

  iptables -A FORWARD -d 216.99.1.216 -p tcp -m tcp --dport 80 -j DROP

  如果您不熟悉linux的ssh命令,那么可以在webmin/virtualmin面板中设置,达到相同效果。参考:webmin面板怎样设置允许特定ip访问80端口,禁止80端口

  更多iptables参考命令如下:

学习啦在线学习网   1.先备份iptables

学习啦在线学习网   # cp /etc/sysconfig/iptables /var/tmp

  需要开80端口,指定IP和局域网

学习啦在线学习网   下面三行的意思:

  先关闭所有的80端口

  开启ip段192.168.1.0/24端的80口

  开启ip段211.123.16.123/24端ip段的80口

  # iptables -I INPUT -p tcp --dport 80 -j DROP

  # iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 80 -j ACCEPT

  # iptables -I INPUT -s 211.123.16.123/24 -p tcp --dport 80 -j ACCEPT

  以上是临时设置。

学习啦在线学习网   2.然后保存iptables

  # service iptables save

  3.重启防火墙

学习啦在线学习网   #service iptables restart

  ===============以下是转载================================================

  以下是端口,先全部封再开某些的IP

学习啦在线学习网   iptables -I INPUT -p tcp --dport 9889 -j DROP

学习啦在线学习网   iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 9889 -j ACCEPT

  如果用了NAT转发记得配合以下才能生效

学习啦在线学习网   iptables -I FORWARD -p tcp --dport 80 -j DROP

学习啦在线学习网   iptables -I FORWARD -s 192.168.1.0/24 -p tcp --dport 80 -j ACCEPT

学习啦在线学习网   常用的IPTABLES规则如下:

  只能收发邮件,别的都关闭

学习啦在线学习网   iptables -I Filter -m mac --mac-source 00:0F:EA:25:51:37 -j DROP

  iptables -I Filter -m mac --mac-source 00:0F:EA:25:51:37 -p udp --dport 53 -j ACCEPT

  iptables -I Filter -m mac --mac-source 00:0F:EA:25:51:37 -p tcp --dport 25 -j ACCEPT

  iptables -I Filter -m mac --mac-source 00:0F:EA:25:51:37 -p tcp --dport 110 -j ACCEPT

  IPSEC NAT 策略

学习啦在线学习网   iptables -I PFWanPriv -d 192.168.100.2 -j ACCEPT

  iptables -t nat -A PREROUTING -p tcp --dport 80 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.2:80

  iptables -t nat -A PREROUTING -p tcp --dport 1723 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.2:1723

学习啦在线学习网   iptables -t nat -A PREROUTING -p udp --dport 1723 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.2:1723

学习啦在线学习网   iptables -t nat -A PREROUTING -p udp --dport 500 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.2:500

学习啦在线学习网   iptables -t nat -A PREROUTING -p udp --dport 4500 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.2:4500

学习啦在线学习网   FTP服务器的NAT

  iptables -I PFWanPriv -p tcp --dport 21 -d 192.168.1.22 -j ACCEPT

学习啦在线学习网   iptables -t nat -A PREROUTING -p tcp --dport 21 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.1.22:21

  只允许访问指定网址

学习啦在线学习网   iptables -A Filter -p udp --dport 53 -j ACCEPT

  iptables -A Filter -p tcp --dport 53 -j ACCEPT

  iptables -A Filter -d www.ctohome.com -j ACCEPT

  iptables -A Filter -d www.guowaivps.com -j ACCEPT

学习啦在线学习网   iptables -A Filter -j DROP

  开放一个IP的一些端口,其它都封闭

  iptables -A Filter -p tcp --dport 80 -s 192.168.1.22 -d www.pconline.com.cn -j ACCEPT

  iptables -A Filter -p tcp --dport 25 -s 192.168.1.22 -j ACCEPT

  iptables -A Filter -p tcp --dport 109 -s 192.168.1.22 -j ACCEPT

学习啦在线学习网   iptables -A Filter -p tcp --dport 110 -s 192.168.1.22 -j ACCEPT

  iptables -A Filter -p tcp --dport 53 -j ACCEPT

  iptables -A Filter -p udp --dport 53 -j ACCEPT

  iptables -A Filter -j DROP

  多个端口

  iptables -A Filter -p tcp -m multiport --destination-port 22,53,80,110 -s 192.168.20.3 -j REJECT

  连续端口

  iptables -A Filter -p tcp -m multiport --source-port 22,53,80,110 -s 192.168.20.3 -j REJECT iptables -A Filter -p tcp --source-port 2:80 -s 192.168.20.3 -j REJECT

  指定时间上网

  iptables -A Filter -s 10.10.10.253 -m time --timestart 6:00 --timestop 11:00 --days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j DROP

  iptables -A Filter -m time --timestart 12:00 --timestop 13:00 --days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j ACCEPT

  iptables -A Filter -m time --timestart 17:30 --timestop 8:30 --days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j ACCEPT

学习啦在线学习网   禁止多个端口服务

  iptables -A Filter -m multiport -p tcp --dport 21,23,80 -j ACCEPT

  将WAN 口NAT到PC

学习啦在线学习网   iptables -t nat -A PREROUTING -i $INTERNET_IF -d $INTERNET_ADDR -j DNAT --to-destination 192.168.0.1

  将WAN口8000端口NAT到192。168。100。200的80端口

学习啦在线学习网   iptables -t nat -A PREROUTING -p tcp --dport 8000 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.1.22:80

  MAIL服务器要转的端口

学习啦在线学习网   iptables -t nat -A PREROUTING -p tcp --dport 110 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.1.22:110

  iptables -t nat -A PREROUTING -p tcp --dport 25 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.1.22:25

学习啦在线学习网   只允许PING 202。96。134。133,别的服务都禁止

  iptables -A Filter -p icmp -s 192.168.1.22 -d 202.96.134.133 -j ACCEPT

  iptables -A Filter -j DROP

  禁用BT配置

  iptables –A Filter –p tcp –dport 6000:20000 –j DROP

  禁用QQ防火墙配置

  iptables -A Filter -p udp --dport ! 53 -j DROP

  iptables -A Filter -d 218.17.209.0/24 -j DROP

  iptables -A Filter -d 218.18.95.0/24 -j DROP

  iptables -A Filter -d 219.133.40.177 -j DROP

  基于MAC,只能收发邮件,其它都拒绝

学习啦在线学习网   iptables -I Filter -m mac --mac-source 00:0A:EB:97:79:A1 -j DROP

  iptables -I Filter -m mac --mac-source 00:0A:EB:97:79:A1 -p tcp --dport 25 -j ACCEPT

学习啦在线学习网   iptables -I Filter -m mac --mac-source 00:0A:EB:97:79:A1 -p tcp --dport 110 -j ACCEPT

  禁用MSN配置

  iptables -A Filter -p udp --dport 9 -j DROP

  iptables -A Filter -p tcp --dport 1863 -j DROP

  iptables -A Filter -p tcp --dport 80 -d 207.68.178.238 -j DROP

  iptables -A Filter -p tcp --dport 80 -d 207.46.110.0/24 -j DROP

  只允许PING 202。96。134。133 其它公网IP都不许PING

学习啦在线学习网   iptables -A Filter -p icmp -s 192.168.1.22 -d 202.96.134.133 -j ACCEPT

  iptables -A Filter -p icmp -j DROP

  禁止某个MAC地址访问internet:

  iptables -I Filter -m mac --mac-source 00:20:18:8F:72:F8 -j DROP

  禁止某个IP地址的PING:

  iptables –A Filter –p icmp –s 192.168.0.1 –j DROP

学习啦在线学习网   禁止某个IP地址服务:

学习啦在线学习网   iptables –A Filter -p tcp -s 192.168.0.1 --dport 80 -j DROP

  iptables –A Filter -p udp -s 192.168.0.1 --dport 53 -j DROP

学习啦在线学习网   只允许某些服务,其他都拒绝(2条规则)

学习啦在线学习网   iptables -A Filter -p tcp -s 192.168.0.1 --dport 1000 -j ACCEPT

  iptables -A Filter -j DROP

学习啦在线学习网   禁止某个IP地址的某个端口服务

学习啦在线学习网   iptables -A Filter -p tcp -s 10.10.10.253 --dport 80 -j ACCEPT

学习啦在线学习网   iptables -A Filter -p tcp -s 10.10.10.253 --dport 80 -j DROP

  禁止某个MAC地址的某个端口服务

学习啦在线学习网   iptables -I Filter -p tcp -m mac --mac-source 00:20:18:8F:72:F8 --dport 80 -j DROP

  禁止某个MAC地址访问internet:

  iptables -I Filter -m mac --mac-source 00:11:22:33:44:55 -j DROP

学习啦在线学习网   禁止某个IP地址的PING:

  iptables –A Filter –p icmp –s 192.168.0.1 –j DROP

  启动iptables

学习啦在线学习网   service iptables start

学习啦在线学习网   iptables –list //*查看iptables规则集*//

学习啦在线学习网   下面是没有定义规划时iptables的样子:

学习啦在线学习网   Chain INPUT (policy ACCEPT)

学习啦在线学习网   target prot opt source destination

  Chain FORWARD (policy ACCEPT)

  target prot opt source destination

  Chain OUTPUT (policy ACCEPT)

学习啦在线学习网   target prot opt source destination

学习啦在线学习网   如何开启/关闭指定端口

  例如:

  开启81端口:

学习啦在线学习网   iptables -I INPUT -i eth0 -p tcp –dport 81 -j ACCEPT

学习啦在线学习网   iptables -I OUTPUT -o eth0 -p tcp –sport 81 -j ACCEPT

  关闭81端口:

学习啦在线学习网   iptables -I INPUT -i eth0 -p tcp –dport 81 -j DROP

  iptables -I OUTPUT -o eth0 -p tcp –sport 81 -j DROP

  然后保存

学习啦在线学习网   /etc/rc.d/init.d/iptables save

  eth0为网卡名称,可以输入ifconfig来查看网卡信息,注意填写正确的网卡名称。

  可以使用lsof命令来查看某一端口是否开放.查看端口可以这样来使用.

  我就以81端口为例:

  lsof -i:81

  如果有显示说明已经开放了,如果没有显示说明没有开放。

LINUX防火墙常用操作

学习啦小编来给大家介绍下LINUX防火墙常用操作,对于自己学习使用Linux更有帮助。下面跟着学习啦小编一起来了解一下吧。 LINUX防火墙常用操作 (1) 重启后永久性生效: 开启:chkconfig iptables on 关闭:chkconfig iptables off (2) 即
推荐度:
点击下载文档文档为doc格式

精选文章

  • Linux防火墙怎么开放特定端口
    Linux防火墙怎么开放特定端口

    学习啦在线学习网学习啦小编来给大家介绍下Linux防火墙怎么开放特定端口,也就是只允许某个特定端口才可以通过,其余的不行。下面跟着学习啦小编一起来了解一下吧。

  • Linux怎么配置Web服务器
    Linux怎么配置Web服务器

    从1998年Linux开始在中国市场受到关注,时间已经过去5年,从最初蜂拥而至的桌面版本,到后来悄悄进入企业应用的Linux服务器,Linux逐渐得到人们的认可。

  • linux中怎么添加ftp用户
    linux中怎么添加ftp用户

    学习啦在线学习网Linux下创建用户是很easy的事情了,只不过不经常去做这些操作,时间久了就容易忘记。那么linux中怎么添加ftp用户,下面跟着学习啦小编一起来了解一下吧

  • Linux如何给文件夹设置密码
    Linux如何给文件夹设置密码

    电脑里经常会存储着重要文件,这些文件需要进行加密,有许多方法来实现。但是你知道在Linux里面怎么给文件夹加密码?下面跟着学习啦小编一起来了解一

646735