接着上节,上节课,基本功能设置后,现在我们telnet本机一下,发现问题:
[root@test1 ~]# telnet 127.0.0.1 22 Trying 127.0.0.1... telnet: connect to address 127.0.0.1: Connection timed out [root@test1 ~]# curl www.baidu.com curl: (6) Couldn't resolve host 'www.baidu.com' [root@test1 ~]# cat /etc/resolv.conf ; generated by /sbin/dhclient-script nameserver 192.168.124.1
本机无法无法访问本机,本机无法访问其他主机。
解决:
因为规则都是对eth0这个网卡做的设置,所以要设置本机的lo设备允许访问。
[root@test1 ~]# iptables -I INPUT -i lo -j ACCEPT
-i 指定数据包进入的网卡
(1)允许所有主机包允许通过lo,现在可以了。
[root@test1 ~]# telnet 127.0.0.1 22 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. SSH-2.0-OpenSSH_5.3 ^] Protocol mismatch. Connection closed by foreign host.
(2)允许主机访问其他主机
[root@test1 ~]# iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-m state --state用来区配链接的状态
本例功能是接受目的地址是本机防火墙且链接状态是ESTABLISHED,RELATED(ftp的)的数据包,实际上是允许已建立的链接相关的新链接进入本机。
查看规则:
[root@test1 ~]# iptables -nL Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpts:10:21 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 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
再测试一下,太多了,省略一下:
[root@test1 ~]# curl www.baidu.com <!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=Edge"><meta content="always" name="referrer"><meta name="theme-color" content="#2932e1"><link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" /><link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" title="百度搜索" /><link rel="icon" sizes="any"
补充:
在场景一的基础上,修改只允许10.103.188.233访问本机的httpd服务
解:先将以前设置的关于httpd服务的规则删除
[root@test1 ~]# iptables -D INPUT -p tcp --dport 80 -j ACCEPT
设置规则:
[root@test1 ~]# iptables -I INPUT -p tcp -s 10.10.188.233 --dport 80 -jACCEPT [root@test1 ~]# iptables -nL Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- 10.10.188.233 0.0.0.0/0 tcp dpt:80 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpts:10:21 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
bingo!