linux开启端口命令 iptables 打开 查看

来源:互联网 发布:cmd运行java步骤 编辑:程序博客网 时间:2024/06/01 07:25

打开指定端口:/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT #80为指定端口
将更改进行保存:/etc/rc.d/init.d/iptables save

或直接在/etc/sysconfig/iptables中增加一行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT


关闭端口

iptables -A INPUT -p tcp --dport 111 -j DROP


开发不连续端口(包括单个):

iptables -A INPUT -p tcp -m multiport --dport 21,20 -j ACCEPT

其他都关闭:

iptables -A INPUT -p tcp -j REJECT --reject-with tcp-reset

开放连续端口:

iptables -A RH-Firewall-1-INPUT -m state --state NEW -m udp -p udp --dport 8001:8140 -j ACCEPT

还可以直接修改/etc/sysconfig/iptables,如何写,参考已开端口,或者按照上面的命令填写即可

重新启动iptables

service iptables restart


查看端口是否已经开放
/etc/init.d/iptables status

what is iptables?

Iptables is used to set up, maintain, and inspect the tables of IPv4 packet filter rules in the Linux kernel.  Several different tables may be defined.  Each table contains a number of built-in chains and may also contain user-defined chains.

以上是itables man手册上的说明。


iptables包含4个表:filter,nat,mangle,raw。我们最常用的就是filter这个表。filte表有三个内建的chain:INPUT、OUTPUT和FORWORD。

INPUT:用来处理发给本机的数据包;如主机A发给本机的数据,数据通信路径会像这:主机A------>本机localhost

OUTPUT:用来处理本机发送出去的数据包;如本机访问百度服务器,数据通信路径会像这样,如本机localhost------>百度服务器

FORWORD:用来处理流经本机,但又不是发给本机的数据包;如主机A要给主机B发送数据包,经过了本机,路径会像这样:主机A--->本机localhost--->主机B。

前面两条chain好理解,但FORWORD怎么理解呢?

一般主机就一张网卡,什么时候会用到呢?

在做ARP中间人攻击时,我们需欺骗网关和被攻击的主机,使其两者的通信数据经过本机,这个时候就可需设置FORWORD 这个chain下的规则了,因为默认情况下,FORWORD这个CHAIN下有一条策略是拒绝所有的数据的。


接下来看下如何查看filter表的配置:

1、filter表查看

[plain] view plain copy
  1. [root@localhost ~]# iptables -L -v  
[plain] view plain copy
  1. 建议查看表时,带上-v参数,会显示in out 方向等信息,显示的信息更完整、详细。  



2、filter表规则添加。

有两种方式添加规则

iptables -A 和iptables -I

iptables -A 添加的规则是添加在最后面。如针对INPUT链增加一条规则,接收从eth0口进入且源地址为192.168.0.0/16网段发往本机的数据。

[plain] view plain copy
  1. [root@localhost ~]# iptables -A INPUT -i eth0 -s 192.168.0.0/16 -j ACCEPT  


iptables -I 添加的规则默认添加至第一条。


如果要指定插入规则的位置,则使用iptables -I 时指定位置序号即可。



3、删除规则

如果删除指定则,使用iptables -D命令,命令后可接序号。效果请对比上图。


或iptables -D 接详细定义;


如果想把所有规则都清除掉,可使用iptables -F。


4、备份iptabes rules

使用iptables-save命令,如:

[plain] view plain copy
  1. [root@localhost ~]# iptables-save >/dd/iptables.bak  

5、恢复iptables rules

使用iptables命令,如:

[plain] view plain copy
  1. [root@localhost ~]# iptables-restore </dd/iptables.bak  


6、iptables 配置保存

以上做的配置修改,在设备重启后,配置将丢失。可使用service iptables save进行保存。

[plain] view plain copy
  1. [root@localhost ~]# service iptables save 

======================================================================================

1、关闭所有的 INPUT FORWARD OUTPUT 只对某些端口开放。
下面是命令实现:

iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP

再用命令 iptables -L -n 查看 是否设置好, 好看到全部 DROP 了
这样的设置好了,我们只是临时的, 重启服务器还是会恢复原来没有设置的状态
还要使用 service iptables save 进行保存
看到信息 firewall rules 防火墙的规则 其实就是保存在 /etc/sysconfig/iptables
可以打开文件查看 vi /etc/sysconfig/iptables
2、
下面我只打开22端口,看我是如何操作的,就是下面2个语句

iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT

再查看下 iptables -L -n 是否添加上去, 看到添加了

Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:22

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp spt:22

现在Linux服务器只打开了22端口,用putty.exe测试一下是否可以链接上去。
可以链接上去了,说明没有问题。

最后别忘记了保存 对防火墙的设置
通过命令:service iptables save 进行保存
重启iptables 
service iptables save && service iptables restart
关闭防火墙
chkconfig iptables off && service iptables stop


iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
针对这2条命令进行一些讲解吧
-A 参数就看成是添加一条 INPUT 的规则
-p 指定是什么协议 我们常用的tcp 协议,当然也有udp 例如53端口的DNS
到时我们要配置DNS用到53端口 大家就会发现使用udp协议的

而 --dport 就是目标端口 当数据从外部进入服务器为目标端口
反之 数据从服务器出去 则为数据源端口 使用 --sport

-j 就是指定是 ACCEPT 接收 或者 DROP 不接收
3、禁止某个IP访问
1台Linux服务器,2台windows xp 操作系统进行访问
Linux服务器ip 192.168.1.99
xp1 ip: 192.168.1.2
xp2 ip: 192.168.1.8

下面看看我2台xp 都可以访问的

192.168.1.2 这是 xp1 可以访问的,
192.168.1.8 xp2 也是可以正常访问的。

那么现在我要禁止 192.168.1.2 xp1 访问, xp2 正常访问, 
下面看看演示

通过命令 iptables -A INPUT -p tcp -s 192.168.1.2 -j DROP
这里意思就是 -A 就是添加新的规则, 怎样的规则呢? 由于我们访问网站使用tcp的,
我们就用 -p tcp , 如果是 udp 就写udp,这里就用tcp了, -s就是 来源的意思,
ip来源于 192.168.1.2 ,-j 怎么做 我们拒绝它 这里应该是 DROP

好,看看效果。好添加成功。下面进行验证 一下是否生效

一直出现等待状态 最后 该页无法显示 ,这是 192.168.1.2 xp1 的访问被拒绝了。

再看看另外一台 xp 是否可以访问, 是可以正常访问的 192.168.1.8 是可以正常访问的
4、如何删除规则
首先我们要知道 这条规则的编号,每条规则都有一个编号

通过 iptables -L -n --line-number 可以显示规则和相对应的编号
num target     prot opt source               destination
1    DROP       tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:3306
2    DROP       tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:21
3    DROP       tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:80
多了 num 这一列, 这样我们就可以 看到刚才的规则对应的是 编号2

那么我们就可以进行删除了
iptables -D INPUT 2
删除INPUT链编号为2的规则。

再 iptables -L -n 查看一下 已经被清除了。
5、过滤无效的数据包
假设有人进入了服务器,或者有病毒木马程序,它可以通过22,80端口像服务器外传送数据。
它的这种方式就和我们正常访问22,80端口区别。它发向外发的数据不是我们通过访问网页请求
而回应的数据包。

下面我们要禁止这些没有通过请求回应的数据包,统统把它们堵住掉。

iptables 提供了一个参数 是检查状态的,下面我们来配置下 22 和 80 端口,防止无效的数据包。

iptables -A OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

可以看到和我们以前使用的:
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
多了一个状态判断。

同样80端口也一样, 现在删掉原来的2条规则,
iptables -L -n --line-number    这个是查看规则而且带上编号。我们看到编号就可以
删除对应的规则了。

iptables -D OUTPUT 1     这里的1表示第一条规则。

当你删除了前面的规则, 编号也会随之改变。看到了吧。

好,我们删除了前面2个规则,22端口还可以正常使用,说明没问题了

下面进行保存,别忘记了,不然的话重启就会还原到原来的样子。

service iptables save    进行保存。

Saving firewall rules to /etc/sysconfig/iptables:          [ OK ]
其实就是把刚才设置的规则写入到 /etc/sysconfig/iptables 文件中。
6、DNS端口53设置
下面我们来看看如何设置iptables来打开DNS端口,DNS端口对应的是53

大家看到我现在的情况了吧,只开放22和80端口, 我现在看看能不能解析域名。

host www.google.com    输入这个命令后,一直等待,说明DNS不通

出现下面提示 :
;; connection timed out; no servers could be reached

ping 一下域名也是不通
[root@localhost ~ping www.google.com
ping: unknown host www.google.com

我这里的原因就是 iptables 限制了53端口。

有些服务器,特别是Web服务器减慢,DNS其实也有关系的,无法发送包到DNS服务器导致的。

下面演示下如何使用 iptables 来设置DNS 53这个端口,如果你不知道 域名服务端口号,你

可以用命令 : grep domain /etc/services

[root@localhost ~grep domain /etc/services
domain          53/tcp                          # name-domain server
domain          53/udp
domaintime      9909/tcp                        # domaintime
domaintime      9909/udp                        # domaintime

看到了吧, 我们一般使用 udp 协议。

好了, 开始设置。。。

iptables -A OUTPUT -p udp --dport 53 -j ACCEPT 
这是我们 ping 一个域名,数据就是从本机出去,所以我们先设置 OUTPUT,
我们按照ping这个流程来设置。

然后 DNS 服务器收到我们发出去的包,就回应一个回来
iptables -A INPUT -p udp --sport 53 -j ACCEPT

同时还要设置 
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p udp --sport 53 -j ACCEPT

好了, 下面开始测试下, 可以用 iptables -L -n 查看设置情况,确定没有问题就可以测试了

[root@localhost ~iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:22
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp dpt:80
ACCEPT     udp -- 0.0.0.0/0            0.0.0.0/0           udp spt:53
ACCEPT     udp -- 0.0.0.0/0            0.0.0.0/0           udp dpt:53

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp spt:22 state ESTABLISHED
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0           tcp spt:80 state ESTABLISHED
ACCEPT     udp -- 0.0.0.0/0            0.0.0.0/0           udp dpt:53
ACCEPT     udp -- 0.0.0.0/0            0.0.0.0/0           udp spt:53

可以测试一下 是否 DNS 可以通过iptables 了。

[root@localhost ~host www.google.com
www.google.com is an alias for www.l.google.com.
www.l.google.com is an alias for www-china.l.google.com.
www-china.l.google.com has address 64.233.189.104
www-china.l.google.com has address 64.233.189.147
www-china.l.google.com has address 64.233.189.99

正常可以解析 google 域名。

ping 方面可能还要设置些东西。

用 nslookup 看看吧

[root@localhost ~nslookup
> www.google.com
Server:         192.168.1.1
Address:        192.168.1.1#53

Non-authoritative answer:
www.google.com canonical name = www.l.google.com.
www.l.google.com        canonical name = www-china.l.google.com.
Name:   www-china.l.google.com
Address: 64.233.189.147
Name:   www-china.l.google.com
Address: 64.233.189.99
Name:   www-china.l.google.com
Address: 64.233.189.104

说明本机DNS正常, iptables 允许53这个端口的访问。
7、iptables对ftp的设置
现在我开始对ftp端口的设置,按照我们以前的视频,添加需要开放的端口
ftp连接端口有2个 21 和 20 端口,我现在添加对应的规则。

[root@localhost rootiptables -A INPUT -p tcp --dport 21 -j ACCEPT
[root@localhost rootiptables -A INPUT -p tcp --dport 20 -j ACCEPT
[root@localhost rootiptables -A OUTPUT -p tcp --sport 21 -j ACCEPT
[root@localhost rootiptables -A OUTPUT -p tcp --sport 20 -j ACCEPT

好,这样就添加完了,我们用浏览器访问一下ftp,出现超时。

所以我刚才说 ftp 是比较特殊的端口,它还有一些端口是 数据传输端口,
例如目录列表, 上传 ,下载 文件都要用到这些端口。

而这些端口是 任意 端口。。。 这个 任意 真的比较特殊。

如果不指定什么一个端口范围, iptables 很难对任意端口开放的,
如果iptables允许任意端口访问, 那和不设置防火墙没什么区别,所以不现实的。

那么我们的解决办法就是 指定这个数据传输端口的一个范围。

下面我们修改一下ftp配置文件。

我这里使用vsftpd来修改演示,其他ftp我不知道哪里修改,大家可以找找资料。

[root@localhost rootvi /etc/vsftpd.conf

在配置文件的最下面 加入

pasv_min_port=30001
pasv_max_port=31000

然后保存退出。

这两句话的意思告诉vsftpd, 要传输数据的端口范围就在30001到31000 这个范围内传送。

这样我们使用 iptables 就好办多了,我们就打开 30001到31000 这些端口。

[root@localhost rootiptables -A INPUT -p tcp --dport 30001:31000 -j ACCEPT
[root@localhost rootiptables -A OUTPUT -p tcp --sport 30001:31000 -j ACCEPT

[root@localhost rootservice iptables save

最后进行保存, 然后我们再用浏览器范围下 ftp。可以正常访问

用个账号登陆上去,也没有问题,上传一些文件上去看看。

看到了吧,上传和下载都正常。。 再查看下 iptables 的设置

[root@localhost rootiptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp dpt:22
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp dpt:21
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp dpt:20
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp dpts:30001:31000

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy DROP)
target     prot opt source               destination
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp spt:22
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp spt:21
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp spt:20
ACCEPT     tcp -- 0.0.0.0/0            0.0.0.0/0          tcp spts:30001:31000

这是我为了演示ftp特殊端口做的简单规则,大家可以添加一些对数据包的验证
例如 -m state --state ESTABLISHED,RELATED 等等要求更加高的验证