目錄
?
Linux防火墻
?
netfilter
iptables filter表小案例
nat表應(yīng)用
Linux防火墻
[root@zyshanlinux-01 ~]# getenforce ##防火墻狀態(tài)開啟
Enforcing
[root@zyshanlinux-01 ~]# setenforce 0 ##臨時(shí)關(guān)閉
[root@zyshanlinux-01 ~]# getenforce ##臨時(shí)關(guān)閉狀態(tài)
Permissive
selinux是Linux特有的安全機(jī)制,因?yàn)榕渲锰闊?#xff0c;幾乎沒有人真正的應(yīng)用它。安裝完系統(tǒng)后我們一般會(huì)選擇關(guān)閉selinux。
[root@zyshanlinux-01 ~]# vi /etc/selinux/config ##永久關(guān)閉要改配置文件
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# ? ? enforcing - SELinux security policy is enforced.
# ? ? permissive - SELinux prints warnings instead of enforcing.
# ? ? disabled - No SELinux policy is loaded.
SELINUX=enforcing ##把這行改成這樣SELINUX=disabled,重啟系統(tǒng)就好。
# SELINUXTYPE= can take one of three two values:
# ? ? targeted - Targeted processes are protected,
# ? ? minimum - Modification of targeted policy. Only selected processes are protected.
# ? ? mls - Multi Level Security protection.
SELINUXTYPE=targeted
?
?
[root@zyshanlinux-001 ~]# getenforce ##重啟系統(tǒng)后狀態(tài)
Disabled
在centos5和6上用的防火墻是netfiler,其配置工具為iptables。centos7則用的是firewalld防火墻,其配置工具也是iptables。但是現(xiàn)在依然有很多企業(yè)使用centos6。
firewalld向下兼容netfilter,所以在firewalld里面也可以用netfilter的設(shè)置方法。
[root@zyshanlinux-001 ~]# systemctl disable firewalld ##先停掉,不讓它開機(jī)啟動(dòng)
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@zyshanlinux-001 ~]# systemctl stop firewalld ##關(guān)閉服務(wù),讓服務(wù)停止
[root@zyshanlinux-001 ~]# yum install -y iptables-services ##先安裝個(gè)包,裝完后就會(huì)產(chǎn)生一個(gè)服務(wù)。
[root@zyshanlinux-001 ~]# systemctl enable iptables ##iptables服務(wù)
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@zyshanlinux-001 ~]# systemctl start iptables ##把iptables服務(wù)開啟
[root@zyshanlinux-001 ~]# iptables -nvL ##這個(gè)命令可以查看默認(rèn)規(guī)則
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 37 2508 ACCEPT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state RELATED,ESTABLISHED0 ? ? 0 ACCEPT ? ? icmp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? 0 ? ? 0 ACCEPT ? ? all -- lo ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? 0 ? ? 0 ACCEPT ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state NEW tcp dpt:220 ? ? 0 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited
?
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 0 ? ? 0 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited
?
Chain OUTPUT (policy ACCEPT 23 packets, 2028 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination
?
netfilter
netfilter的5個(gè)表
filter表用于過濾包,最常用的表,有INPUT、FORWARD、OUTPUT三個(gè)個(gè)鏈
nat表用于網(wǎng)絡(luò)地址轉(zhuǎn)換,有PREROUTING、OUTPUT、POSTROUTING三個(gè)鏈
managle表用于給數(shù)據(jù)包做標(biāo)記,幾乎用不到
raw表可以實(shí)現(xiàn)不追蹤某些數(shù)據(jù)包,幾乎用不到
security表在centos6中并沒有,用于強(qiáng)制訪問控制(MAC)的網(wǎng)絡(luò)規(guī)則,幾乎用不到
參考文章http://www.cnblogs.com/metoy/p/4320813.html
?
數(shù)據(jù)包流向與netfilter的5個(gè)鏈
PREROUTING:數(shù)據(jù)包進(jìn)入路由表之前
INPUT:通過路由表后目的地為本機(jī)
FORWARD:通過路由表后,目的地不為本機(jī)
OUTPUT:有本機(jī)產(chǎn)生,向外發(fā)出
POSTROUTING:發(fā)送到網(wǎng)卡接口之前
?
[root@zyshanlinux-001 ~]# iptables -nvL ##查看規(guī)則
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 8 ? 576 ACCEPT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state RELATED,ESTABLISHED0 ? ? 0 ACCEPT ? ? icmp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? 0 ? ? 0 ACCEPT ? ? all -- lo ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? 0 ? ? 0 ACCEPT ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state NEW tcp dpt:220 ? ? 0 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited
?
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 0 ? ? 0 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited
?
Chain OUTPUT (policy ACCEPT 5 packets, 684 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination [root@zyshanlinux-001 ~]# cat /etc/sysconfig/iptables ##iptables規(guī)則的配置文件
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
[root@zyshanlinux-001 ~]# iptables -F ##清空規(guī)則
[root@zyshanlinux-001 ~]# iptables -nvL ##規(guī)則臨時(shí)清空了
Chain INPUT (policy ACCEPT 16 packets, 1204 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain OUTPUT (policy ACCEPT 12 packets, 1928 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination
[root@zyshanlinux-001 ~]# cat /etc/sysconfig/iptables ##規(guī)則仍在配置文件里保存著
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
[root@zyshanlinux-001 ~]# service iptables restart ##重啟iptables服務(wù)或系統(tǒng)后,規(guī)則都會(huì)加載回來
Redirecting to /bin/systemctl restart iptables.service
[root@zyshanlinux-001 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 28 1848 ACCEPT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state RELATED,ESTABLISHED0 ? ? 0 ACCEPT ? ? icmp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? 0 ? ? 0 ACCEPT ? ? all -- lo ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? 0 ? ? 0 ACCEPT ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state NEW tcp dpt:220 ? ? 0 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited
?
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 0 ? ? 0 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited
?
Chain OUTPUT (policy ACCEPT 15 packets, 1444 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination
更改了iptables規(guī)則僅僅在當(dāng)前的內(nèi)存中生效,想要在系統(tǒng)或服務(wù)重啟后生效必須使用保存的命令。
[root@zyshanlinux-001 ~]# iptables -t nat -nvL ##指定nat表
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination
[root@zyshanlinux-001 ~]# iptables -t filter -nvL ##這個(gè)與iptables -nvL是一樣的
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 63 4264 ACCEPT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state RELATED,ESTABLISHED0 ? ? 0 ACCEPT ? ? icmp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? 0 ? ? 0 ACCEPT ? ? all -- lo ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? 0 ? ? 0 ACCEPT ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state NEW tcp dpt:220 ? ? 0 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited
?
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 0 ? ? 0 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited
?
Chain OUTPUT (policy ACCEPT 36 packets, 4600 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination[root@zyshanlinux-001 ~]# iptables -Z;iptables -nvL ##數(shù)據(jù)包個(gè)數(shù)和大小被清空了
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 0 ? ? 0 ACCEPT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state RELATED,ESTABLISHED
注:如果要用sport和dport,必須用-p tcp,才能用。
[root@zyshanlinux-001 ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
[root@zyshanlinux-001 ~]# iptables -nvL ##增加的規(guī)則加到最后了
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 172 13128 ACCEPT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state RELATED,ESTABLISHED0 ? ? 0 ACCEPT ? ? icmp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? 0 ? ? 0 ACCEPT ? ? all -- lo ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? 0 ? ? 0 ACCEPT ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state NEW tcp dpt:221 ? 244 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited0 ? ? 0 DROP ? ? ? tcp -- * ? ? * ? ? ? 192.168.188.1 ? ? ? 192.168.188.128 ? ? tcp spt:1234 dpt:80
?
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 0 ? ? 0 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited
?
Chain OUTPUT (policy ACCEPT 15 packets, 1444 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination
[root@zyshanlinux-001 ~]# iptables -I INPUT -p tcp --dport 80 -j DROP ##-I插隊(duì)到規(guī)則的最前面
[root@zyshanlinux-001 ~]# iptables -nvL ##-I插入,-A增加,前面的規(guī)則優(yōu)先過濾,有前后規(guī)則相同的元素,經(jīng)過前面的規(guī)則過濾后,后面就沒有包含該元素的數(shù)據(jù)了,后面的過濾規(guī)則就過濾不到需求元素了。
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 0 ? ? 0 DROP ? ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? tcp dpt:80318 23200 ACCEPT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state RELATED,ESTABLISHED0 ? ? 0 ACCEPT ? ? icmp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? 0 ? ? 0 ACCEPT ? ? all -- lo ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? 0 ? ? 0 ACCEPT ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state NEW tcp dpt:222 ? 488 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited0 ? ? 0 DROP ? ? ? tcp -- * ? ? * ? ? ? 192.168.188.1 ? ? ? 192.168.188.128 ? ? tcp spt:1234 dpt:80
[root@zyshanlinux-001 ~]# iptables -D INPUT -p tcp --dport 80 -j DROP ##-D刪除規(guī)則
[root@zyshanlinux-001 ~]# iptables -nvL ##第一條規(guī)則沒了
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 373 28196 ACCEPT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state RELATED,ESTABLISHED
[root@zyshanlinux-001 ~]#
[root@zyshanlinux-001 ~]# iptables -D INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP ##-D刪除最后一條規(guī)則
-i etho針對(duì)網(wǎng)卡的
[root@zyshanlinux-001 ~]# iptables -nvL --line-numbers ?
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num ? pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
1 ? ? 492 39336 ACCEPT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state RELATED,ESTABLISHED
2 ? ? ? 0 ? ? 0 ACCEPT ? ? icmp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ?
3 ? ? ? 0 ? ? 0 ACCEPT ? ? all -- lo ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ?
4 ? ? ? 0 ? ? 0 ACCEPT ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state NEW tcp dpt:22
5 ? ? ? 2 ? 488 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited
6 ? ? ? 0 ? ? 0 DROP ? ? ? tcp -- * ? ? * ? ? ? 192.168.188.1 ? ? ? 192.168.188.128 ? ? tcp spt:1234 dpt:80
?
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num ? pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
1 ? ? ? 0 ? ? 0 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited
?
Chain OUTPUT (policy ACCEPT 101 packets, 11124 bytes)
num ? pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
[root@zyshanlinux-001 ~]# iptables -D INPUT 6 ##針對(duì)INPUT連第6條規(guī)則進(jìn)行刪除
[root@zyshanlinux-001 ~]# iptables -nvL --line-numbers ##INPUT鏈只剩下5條規(guī)則了
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num ? pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
1 ? ? 560 43984 ACCEPT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state RELATED,ESTABLISHED
2 ? ? ? 0 ? ? 0 ACCEPT ? ? icmp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ?
3 ? ? ? 0 ? ? 0 ACCEPT ? ? all -- lo ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ?
4 ? ? ? 0 ? ? 0 ACCEPT ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state NEW tcp dpt:22
5 ? ? ? 2 ? 488 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited
?
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num ? pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
1 ? ? ? 0 ? ? 0 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited
?
Chain OUTPUT (policy ACCEPT 14 packets, 2760 bytes)
num ? pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ?
Chain OUTPUT (policy ACCEPT 14 packets, 2760 bytes) ##OUTPUT鏈沒有規(guī)則的時(shí)候是默認(rèn)為ACCEPT規(guī)則
num ? pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination
?
[root@zyshanlinux-001 ~]# iptables -P INPUT DROP ##如果你更改這個(gè)默認(rèn)規(guī)則為DROP,遠(yuǎn)程連接就會(huì)斷開,因?yàn)镈ROP會(huì)把數(shù)據(jù)通通禁止。必須到本地去把這個(gè)更改后的默認(rèn)規(guī)則改回更改前的默認(rèn)規(guī)則ACCEPT
[root@zyshanlinux-001 ~]# iptables -P INPUT ACCEPT ##給它放行就可以了
iptables filter表小案例
#! /bin/bashipt="/usr/sbin/iptables" ##定義變量$ipt -F ##首先把之前的規(guī)則清空$ipt -P INPUT DROP ##把默認(rèn)策略定義下$ipt -P OUTPUT ACCEPT$ipt -P FORWARD ACCEPT$ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT ##加規(guī)則$ipt -A INPUT -s 192.168.106.0/24 -p tcp --dport 22 -j ACCEPT$ipt -A INPUT -p tcp --dport 80 -j ACCEPT$ipt -A INPUT -p tcp --dport 21 -j ACCEPT
再執(zhí)行該腳本:sh /usr/local/sbin/iptables.sh
[root@zyshanlinux-001 ~]# w17:24:25 up 5:15, 1 user, load average: 0.00, 0.01, 0.05
USER ? ? TTY ? ? FROM ? ? ? ? ? ? LOGIN@ ? IDLE ? JCPU ? PCPU WHAT
root ? ? pts/0 ? 192.168.106.1 ? 12:09 ? 1.00s 0.18s 0.00s w
[root@zyshanlinux-001 ~]# vi /usr/local/sbin/iptables.sh ##加入上面代碼
[root@zyshanlinux-001 ~]# w ##106網(wǎng)段是允許通過的17:26:10 up 5:17, 2 users, load average: 0.00, 0.01, 0.05
USER ? ? TTY ? ? FROM ? ? ? ? ? ? LOGIN@ ? IDLE ? JCPU ? PCPU WHAT
root ? ? pts/0 ? 192.168.106.1 ? 12:09 ? 2.00s 0.18s 0.00s w
root ? ? pts/1 ? 192.168.106.1 ? 17:25 ? 26.00s 0.01s 0.01s -bash
[root@zyshanlinux-001 ~]# sh /usr/local/sbin/iptables.sh ##執(zhí)行該腳本
[root@zyshanlinux-001 ~]# iptables -nvL ##規(guī)則增加了
Chain INPUT (policy DROP 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 32 2112 ACCEPT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state RELATED,ESTABLISHED0 ? ? 0 ACCEPT ? ? tcp -- * ? ? * ? ? ? 192.168.106.0/24 ? ? 0.0.0.0/0 ? ? ? ? ? tcp dpt:220 ? ? 0 ACCEPT ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? tcp dpt:800 ? ? 0 ACCEPT ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? tcp dpt:21
?
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain OUTPUT (policy ACCEPT 17 packets, 1644 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
[root@zyshanlinux-001 ~]# iptables -nvL ##數(shù)據(jù)和大小確實(shí)增加了
Chain INPUT (policy DROP 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 36 2392 ACCEPT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state RELATED,ESTABLISHED0 ? ? 0 ACCEPT ? ? tcp -- * ? ? * ? ? ? 192.168.106.0/24 ? ? 0.0.0.0/0 ? ? ? ? ? tcp dpt:220 ? ? 0 ACCEPT ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? tcp dpt:800 ? ? 0 ACCEPT ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? tcp dpt:21
?
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain OUTPUT (policy ACCEPT 20 packets, 2832 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination
[root@zyshanlinux-001 ~]# service iptables restart ##先把前面的腳本恢復(fù)為默認(rèn)策略
Redirecting to /bin/systemctl restart iptables.service
[root@zyshanlinux-001 ~]# iptables -nvL ##腳本規(guī)則恢復(fù)為默認(rèn)規(guī)則
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 28 1848 ACCEPT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state RELATED,ESTABLISHED0 ? ? 0 ACCEPT ? ? icmp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? 0 ? ? 0 ACCEPT ? ? all -- lo ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? 0 ? ? 0 ACCEPT ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? state NEW tcp dpt:220 ? ? 0 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited
?
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 0 ? ? 0 REJECT ? ? all -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 0.0.0.0/0 ? ? ? ? ? reject-with icmp-host-prohibited
?
Chain OUTPUT (policy ACCEPT 15 packets, 1444 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
[root@zyshanlinux-001 ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP ##可以ping通外面,但禁止外面ping你。
[root@zyshanlinux-001 ~]# ping www.qq.com ##ping外面可以
PING www.qq.com (140.206.160.207) 56(84) bytes of data.
64 bytes from 140.206.160.207 (140.206.160.207): icmp_seq=1 ttl=128 time=59.9 ms
64 bytes from 140.206.160.207 (140.206.160.207): icmp_seq=2 ttl=128 time=52.2 ms
64 bytes from 140.206.160.207 (140.206.160.207): icmp_seq=3 ttl=128 time=54.6 ms
^C
--- www.qq.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 52.263/55.614/59.906/3.196 ms
ping到本機(jī)不允許。
C:\Users\zhengyushan>ping 192.168.106.128
?
正在 Ping 192.168.106.128 具有 32 字節(jié)的數(shù)據(jù):
請(qǐng)求超時(shí)。
[root@zyshanlinux-001 ~]# service iptables restart ##恢復(fù)默認(rèn)規(guī)則
Redirecting to /bin/systemctl restart iptables.service
?
nat表應(yīng)用
需求1具體步驟
A機(jī)器增加一塊網(wǎng)卡 默認(rèn)設(shè)置 選擇LAN區(qū)段網(wǎng)絡(luò)連接 選擇LAN區(qū)段“網(wǎng)絡(luò)交換機(jī)01” B機(jī)器是A機(jī)器克隆的所以原有網(wǎng)卡是配好IP的,需要去掉這塊網(wǎng)卡 添加新網(wǎng)卡,也是LAN區(qū)段連接“網(wǎng)絡(luò)交換01” 命令給A機(jī)器新網(wǎng)卡ens37附上臨時(shí)IP:192.168.100.1
[root@zyshanlinux-001 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.106.128 netmask 255.255.255.0 broadcast 192.168.106.255inet6 fe80::8fc3:bbdf:ba89:22a7 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:a1:d4:eb txqueuelen 1000 (Ethernet)RX packets 76 bytes 8349 (8.1 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 90 bytes 12925 (12.6 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
?
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.106.150 netmask 255.255.255.0 broadcast 192.168.106.255ether 00:0c:29:a1:d4:eb txqueuelen 1000 (Ethernet)
?
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet6 fe80::7285:a690:d34:bb0c prefixlen 64 scopeid 0x20<link>ether 00:0c:29:a1:d4:f5 txqueuelen 1000 (Ethernet)RX packets 8 bytes 2736 (2.6 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 25 bytes 4326 (4.2 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
?
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10<host>loop txqueuelen 1 (Local Loopback)RX packets 40 bytes 3192 (3.1 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 40 bytes 3192 (3.1 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
?
[root@zyshanlinux-001 ~]# ifconfig ens37 192.168.100.1/24 ##命令行手動(dòng)設(shè)置IP,這個(gè)只是臨時(shí)重啟就沒了,永久的需要在配置文件中改,ens37沒有配置文件,需要復(fù)制ens33網(wǎng)卡的配置文件,更改各個(gè)參數(shù)。
[root@zyshanlinux-001 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.106.128 netmask 255.255.255.0 broadcast 192.168.106.255inet6 fe80::8fc3:bbdf:ba89:22a7 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:a1:d4:eb txqueuelen 1000 (Ethernet)RX packets 242 bytes 20623 (20.1 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 156 bytes 25683 (25.0 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
?
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.106.150 netmask 255.255.255.0 broadcast 192.168.106.255ether 00:0c:29:a1:d4:eb txqueuelen 1000 (Ethernet)
?
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.100.1 netmask 255.255.255.0 broadcast 192.168.100.255inet6 fe80::20c:29ff:fea1:d4f5 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:a1:d4:f5 txqueuelen 1000 (Ethernet)RX packets 20 bytes 6840 (6.6 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 58 bytes 9320 (9.1 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
?
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10<host>loop txqueuelen 1 (Local Loopback)RX packets 40 bytes 3192 (3.1 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 40 bytes 3192 (3.1 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
B機(jī)器無法遠(yuǎn)程連接需要接入本地操作,物理上已經(jīng)將把ens33斷開,謹(jǐn)慎起見還是斷開網(wǎng)卡命令:ifdown ens33
仍然是手動(dòng)命令給B機(jī)器附上臨時(shí)IP:192.168.100.100
給B機(jī)器附上A機(jī)器的網(wǎng)關(guān)命令route add default gw 192.168.100.1
B機(jī)器設(shè)置DNS:vi /etc/resolv.conf
在配置文件里加上:nameserver 119.29.29.29
以A機(jī)器為內(nèi)核轉(zhuǎn)發(fā),必須打開端口轉(zhuǎn)發(fā)才能實(shí)現(xiàn)NAT的應(yīng)用
[root@zyshanlinux-001 ~]# cat /proc/sys/net/ipv4/ip_forward ##默認(rèn)是0,沒有開啟內(nèi)核轉(zhuǎn)發(fā)
0
[root@zyshanlinux-001 ~]# echo "1" > !$
echo "1" > /proc/sys/net/ipv4/ip_forward
[root@zyshanlinux-001 ~]# !cat
cat /proc/sys/net/ipv4/ip_forward ##打開端口轉(zhuǎn)發(fā),要想實(shí)現(xiàn)NAT的應(yīng)用必須打開端口轉(zhuǎn)發(fā)
1
[root@zyshanlinux-001 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE ##要增加條規(guī)則,欺騙,令192.168.100.0這個(gè)網(wǎng)段能夠上網(wǎng)
[root@zyshanlinux-001 ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 0 ? ? 0 MASQUERADE all -- * ? ? ens33 ? 192.168.100.0/24 ? ? 0.0.0.0/0 ? ##增加的規(guī)則在這
結(jié)果:
A機(jī)器可以ping外網(wǎng),可以pingB機(jī)器192.168.100.100,一切都可以ping 。
B機(jī)器只能pingA機(jī)器的ens37網(wǎng)卡(網(wǎng)關(guān)),外網(wǎng)、公網(wǎng)、DNS都ping不了。
命令賦予的臨時(shí)ip100.1和100.100很容易丟失,在不注銷當(dāng)前用戶的前提下。
B上設(shè)置網(wǎng)關(guān)為192.168.100.1
需求2:C機(jī)器只能和A通信,讓C機(jī)器可以直接連通B機(jī)器的22端口
A上打開路由轉(zhuǎn)發(fā)echo "1">/ proc/sys/net/ipv4/ip_forward
需求2實(shí)驗(yàn)步驟
A機(jī)器操作
[root@zyshanlinux-001 ~]# cat /proc/sys/net/ipv4/ip_forward ##打開端口轉(zhuǎn)發(fā),上面做了只是確認(rèn)下
1
##刪除上條測(cè)試的規(guī)則,增加2條規(guī)則
[root@zyshanlinux-001 ~]# iptables -t nat -D POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
[root@zyshanlinux-001 ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain OUTPUT (policy ACCEPT 2 packets, 152 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain POSTROUTING (policy ACCEPT 2 packets, 152 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
[root@zyshanlinux-001 ~]# iptables -t nat -A PREROUTING -d 192.168.43.32 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22
[root@zyshanlinux-001 ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 0 ? ? 0 DNAT ? ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 192.168.43.32 ? ? ? tcp dpt:1122 to:192.168.100.100:22
?
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
[root@zyshanlinux-001 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.43.32
[root@zyshanlinux-001 ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 0 ? ? 0 DNAT ? ? ? tcp -- * ? ? * ? ? ? 0.0.0.0/0 ? ? ? ? ? 192.168.43.32 ? ? ? tcp dpt:1122 to:192.168.100.100:22
?
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ?
?
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)pkts bytes target ? ? prot opt in ? ? out ? ? source ? ? ? ? ? ? ? destination ? ? ? ? 0 ? ? 0 SNAT ? ? ? all -- * ? ? * ? ? ? 192.168.100.100 ? ? 0.0.0.0/0 ? ? ? ? ? to:192.168.43.32
B機(jī)器操作,設(shè)置網(wǎng)關(guān)
[root@zyshanlinux-001 ~]# route add default gw 192.168.100.1
[root@zyshanlinux-001 ~]# route -n
Kernel IP routing table
Destination ? ? Gateway ? ? ? ? Genmask ? ? ? ? Flags Metric Ref ? Use Iface
0.0.0.0 ? ? ? ? 192.168.100.1 ? 0.0.0.0 ? ? ? ? UG ? 0 ? ? 0 ? ? ? 0 ens37
192.168.100.0 ? 0.0.0.0 ? ? ? ? 255.255.255.0 ? U ? ? 0 ? ? 0 ? ? ? 0 ens37
遠(yuǎn)程連接
需求2失敗
?
擴(kuò)展(selinux了解即可)?
selinux教程 http://os.51cto.com/art/201209/355490.htm?
selinux pdf電子書 http://pan.baidu.com/s/1jGGdExK?
iptables應(yīng)用在一個(gè)網(wǎng)段 http://www.aminglinux.com/bbs/thread-177-1-1.html?
sant,dnat,masquerade http://www.aminglinux.com/bbs/thread-7255-1-1.html?
iptables限制syn速率 http://www.aminglinux.com/bbs/thread-985-1-1.html http://jamyy.us.to/blog/2006/03/206.html?
總結(jié)
以上是生活随笔 為你收集整理的Linux日常运维管理技巧(二)Linux防火墙:你可以ping别人,别人ping不了你、转发、代理 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。