七週三次課html
10.11 Linux網絡相關linux
10.12 firewalld和netfiltervim
10.13 netfilter5表5鏈介紹windows
10.14 iptables語法centos
10.11 Linux網絡相關安全
ifconfig命令bash
查看網卡IP服務器
若是系統沒有該命令可使用yum安裝:網絡
[root@tianqi-01 ~]# yum install -y net-toolsless
查看網卡IP還可使用命令:ip add,這個顯示有點亂!
ifconfig -a
顯示全部網卡信息(包括down掉的或者沒有IP地址的網卡)
啓動/關閉網卡
ifup/ifdown [網卡名]
應用環境:更改單個指定網卡配置後須要重啓才能生效,爲了不關閉或重啓全部網卡,能夠對單個網卡執行該命令。
注: 若是網卡正在使用中,不要單獨使用ifdown命令!!!解決辦法: 「# ifdown ens33 && ifup ens33」,使兩個命令一塊兒執行!
爲虛擬機添加網卡
在linux系統中,網卡是能夠設定多重IP的。
如今想增長一個IP,就須要增長一個虛擬網卡。
[root@tianqi-01 ~]# cd /etc/sysconfig/network-scripts
[root@tianqi-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:0
[root@tianqi-01 network-scripts]# ls
ifcfg-ens33 ifdown-bnep ifdown-isdn ifdown-sit ifup ifup-ippp ifup-plusb ifup-sit ifup-wireless
ifcfg-ens33:0 ifdown-eth ifdown-post ifdown-Team ifup-aliases ifup-ipv6 ifup-post ifup-Team init.ipv6-global
ifcfg-lo ifdown-ippp ifdown-ppp ifdown-TeamPort ifup-bnep ifup-isdn ifup-ppp ifup-TeamPort network-functions
ifdown ifdown-ipv6 ifdown-routes ifdown-tunnel ifup-eth ifup-plip ifup-routes ifup-tunnel network-functions-ipv6
#之因此加\,是爲了脫義:,否則在linux命令行下面沒法識別。而後編輯ifcfg-ens33\:0,必定要把DEVICE改爲ens33\:0.
[root@tianqi-01 network-scripts]# vim ifcfg-ens33:0
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33:0
UUID=bd1e1864-e75b-4f5f-bc63-9b1b0d64feff
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.11.139
NETMASK=255.255.255.0
GATEWAY=192.168.11.2
[root@tianqi-01 network-scripts]# ifdown ens33 && ifup ens33
Device 'ens33' successfully disconnected.
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)
[root@tianqi-01 network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.11.136 netmask 255.255.255.0 broadcast 192.168.11.255
inet6 fe80::1eb9:8f9e:264a:7159 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:08:64:43 txqueuelen 1000 (Ethernet)
RX packets 1503 bytes 133236 (130.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1210 bytes 115291 (112.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.11.139 netmask 255.255.255.0 broadcast 192.168.11.255
ether 00:0c:29:08:64:43 txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
上面就多了一個網卡,看看windows能不能ping通192.168.11.139,發現是能夠ping通的,這就證實沒問題。
• mii-tool ens33 查看網卡是否鏈接
查看服務器是否連着網線,在機房的話,能夠看網卡的燈是否是亮着,在系統裏面的話,使用上面的命令。
[root@tianqi-01 network-scripts]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok
這裏顯示link ok,說明網卡爲鏈接狀態。若是顯示no link,就說明網卡壞了或者沒有鏈接網線。若是顯示not support,可使用下面一個命令。
• ethtool ens33 也能夠查看網卡是否鏈接
[root@tianqi-01 network-scripts]# ethtool ens33
Settings for ens33:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supported pause frame use: No
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Speed: 1000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
MDI-X: off (auto)
Supports Wake-on: d
Wake-on: d
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes
若是網卡沒有鏈接,最下面一行Link detected顯示爲no。
• 更改主機名 hostnamectl set-hostname aminglinux
[root@tianqi-01 network-scripts]# hostname
tianqi-01
[root@tianqi-01 network-scripts]# hostnamectl set-hostname tianqi
[root@tianqi-01 network-scripts]# bash
#這裏root@tianqi-01沒有發生變化,能夠退出從新登錄或者bash命令進入子目錄。
[root@tianqi network-scripts]# cat /etc/hostname
tianqi
• DNS配置文件/etc/resolv.conf
DNS是用來解析域名的,平時咱們訪問網站都是直接輸入一個網址,而DNS把這個網址解析到一個IP。
在linux下設置DNS很是簡單,只要把DNS地址寫到配置文件/etc/resolv.conf中便可。
[root@tianqi-01 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 119.29.29.29
第一行是個註釋,說明這個配置文件中的DNS IP地址是由NetworkManager服務生成的。NetworkManager是一個集成的linux網絡管理器,不經常使用。resolv.conf有它固定的格式,必定要寫成
nameserver IP的格式。能夠多寫幾個nameserver,系統默認會用第一個nameserver去解析域名,當第一個解析不成功時會使用第二個。
若是隻是臨時修改DNS IP地址,就直接修改/etc/resolv.conf;若是是永久生效的話,仍是要修改網卡的配置文件。
[root@tianqi-01 network-scripts]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=bd1e1864-e75b-4f5f-bc63-9b1b0d64feff
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.11.136
NETMASK=255.255.255.0
GATEWAY=192.168.11.2
DNS1=119.29.29.29
DNS2=8.8.8.8 #這個是谷歌DNS
[root@tianqi-01 network-scripts]# ifdown ens33 && ifup ens33
Device 'ens33' successfully disconnected.
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
[root@tianqi-01 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 119.29.29.29
nameserver 8.8.8.8
/etc/hosts文件是Linux、Windows都有的,用於指定域名訪問的IP地址:
在linux下還有一個特殊文件也能解析域名,不過須要咱們在裏面手動添加IP地址和域名。它的做用是臨時解析某個域名,很是有用。
[root@tianqi-01 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
如今ping一下qq,如今發現是公網IP。
[root@tianqi-01 network-scripts]# ping www.qq123.com
PING www.qq123.com (211.155.235.108) 56(84) bytes of data.
64 bytes from 211.155.235.108 (211.155.235.108): icmp_seq=1 ttl=128 time=33.8 ms
64 bytes from 211.155.235.108 (211.155.235.108): icmp_seq=2 ttl=128 time=30.0 ms
64 bytes from 211.155.235.108 (211.155.235.108): icmp_seq=3 ttl=128 time=30.1 ms
64 bytes from 211.155.235.108 (211.155.235.108): icmp_seq=4 ttl=128 time=29.9 ms
64 bytes from 211.155.235.108 (211.155.235.108): icmp_seq=5 ttl=128 time=38.9 ms
^C
--- www.qq123.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4030ms
rtt min/avg/max/mdev = 29.984/32.593/38.929/3.493 ms
例如我修改hosts文件指定一個域名的ip:
[root@tianqi-01 network-scripts]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 www.test.com
[root@tianqi-01 network-scripts]# ping www.test.com
PING www.test.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.070 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.085 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.130 ms
^C
--- www.test.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2020ms
rtt min/avg/max/mdev = 0.070/0.095/0.130/0.025 ms
ping這個域名時就會訪問127.0.0.1 IP地址
一個IP能夠指定多個域名:
[root@tianqi-01 network-scripts]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 www.test.com www.123.com www.0000000.com www.8888.com
[root@tianqi-01 network-scripts]# ping www.8888.com
PING www.test.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.058 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.057 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.057 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.058 ms
^C
--- www.test.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3018ms
rtt min/avg/max/mdev = 0.057/0.057/0.058/0.007 ms
若是有兩個一樣的域名指定了不一樣的IP,以第一個的那個IP爲準:
[root@tianqi-01 network-scripts]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 www.test.com www.123.com www.0000000.com www.8888.com
192.168.11.136 www.123.com
[root@tianqi-01 network-scripts]# ping www.123.com
PING www.123.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.067 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.186 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.057 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.057 ms
^C
--- www.123.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.057/0.091/0.186/0.055 ms
[root@tianqi-01 network-scripts]#
10.12 firewalld和netfilter
selinux是linux系統特有的安全機制,安裝完系統後,通常要把selinux關閉,以避免引發沒必要要的麻煩。
•selinux臨時關閉 setenforce 0
這僅僅是臨時的,要想永久關閉須要更改配置文件,須要把SELINUX=enforcing改爲
SELINUX=disabled。
更改完配置文件之後,重啓系統方可生效。可使用getenforce命令得到當前selinux的狀態。
[root@tianqi-01 network-scripts]# vim /etc/selinux/config
# 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=disabled
# 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@tianqi-01 network-scripts]# getenforce
Enforcing
[root@tianqi-01 network-scripts]# setenforce 0
[root@tianqi-01 network-scripts]# getenforce
Permissive
• centos7以前使用netfilter防火牆,centos7的防火牆是firewalld,iptables是其中的工具。工具用法是同樣的,用這個工具能夠添加一些規則,好比關閉和開啓一些端口。
在centos7上使用centos6的防火牆機制也是沒有問題的。
一、關閉firewalld服務
[root@tianqi-01 network-scripts]# systemctl disable firewalld //停掉firewalld,就是限制開機啓動
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@tianqi-02 ~]# systemctl stop firewalld //關閉firewalld服務
2.安裝iptables-services包:
開啓netfilter
在開啓以前,須要先安裝一個iptables-services包
[root@tianqi-01 network-scripts]# yum install -y iptables-services
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
這樣就可使用以前的iptables了
安裝完以後就會有一個iptables服務:
[root@tianqi-01 network-scripts]# systemctl enable iptables //設置開機啓動
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
3.開啓iptables服務:
[root@tianqi-01 ~]# systemctl start iptables
4.使用iptables -nvL命令查看iptables服務自帶的一些規則:
[root@tianqi-01 network-scripts]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
27 1808 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 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:22
0 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, 1460 bytes)
pkts bytes target prot opt in out source destination
10.13 netfilter5表5鏈介紹
• netfilter的5個表
• filter表用於過濾包,最經常使用的表,有INPUT、FORWARD、OUTPUT三個鏈
• nat表用於網絡地址轉換,有PREROUTING、OUTPUT、POSTROUTING三個鏈
• managle表用於給數據包作標記,幾乎用不到
• raw表能夠實現不追蹤某些數據包,阿銘歷來不用
• security表在centos6中並無,用於強制訪問控制(MAC)的網絡規則,阿銘沒用過
• 參考文章 http://www.cnblogs.com/metoy/p/4320813.html
netfilter的五個表
netfilter的五個鏈
PREROUTING:數據包進入路由表以前
INPUT:經過路由表後目的地爲本機
FORWARDING:經過路由表後,目的地不爲本機
OUTPUT:由本機產生,向外轉發
POSTROUTING:發送到網卡接口以前
iptables傳輸數據包的過程
① 當一個數據包進入網卡時,它首先進入PREROUTING鏈,內核根據數據包目的IP判斷是否須要轉送出去。
② 若是數據包就是進入本機的,它就會沿着圖向下移動,到達INPUT鏈。數據包到了INPUT鏈後,任何進程都會收到它。本機上運行的程序能夠發送數據包,這些數據包會通過OUTPUT鏈,而後到達POSTROUTING鏈輸出。
③ 若是數據包是要轉發出去的,且內核容許轉發,數據包就會如圖所示向右移動,通過FORWARD鏈,而後到達POSTROUTING鏈輸出。
學習擴展連接文章,能夠幫助咱們更好的學習iptables的表和鏈。
10.14 iptables語法
•數據包流向與netfilter的5個鏈
• PREROUTING:數據包進入路由表以前
• INPUT:經過路由表後目的地爲本機
• FORWARD:經過路由表後,目的地不爲本機
• OUTPUT:由本機產生,向外發出
• POSTROUTING:發送到網卡接口以前
iptables的默認規則在/etc/sysconfig/iptables文件裏保存着:
[root@tianqi-01 network-scripts]# cat /etc/sysconfig/iptables
# 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
清除規則使用iptables -F命令:
這樣雖然清空了規則,可是默認規則還會保存在/etc/sysconfig/iptables文件裏。
想要當前的規則保存到/etc/sysconfig/iptables文件裏就要使用service iptables save命令:
若是沒有保存到/etc/sysconfig/iptables文件裏的話,重啓服務後就會從新加載/etc/sysconfig/iptables文件裏的規則:
[root@tianqi-01 network-scripts]# iptables -nvL //查看iptables默認規則
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
107 7496 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 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:22
0 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 69 packets, 7784 bytes)
pkts bytes target prot opt in out source destination
[root@tianqi-02 ~]# service iptables restart
Redirecting to /bin/systemctl restart iptables.service
[root@tianqi-02 ~]#
存放默認規則的位置
[root@tianqi-02 ~]# cat /etc/sysconfig/iptables
# 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@tianqi-02 ~]#
[root@tianqi-02 ~]# iptables -F
[root@tianqi-02 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 28 packets, 1848 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 15 packets, 1412 bytes)
pkts bytes target prot opt in out source destination
[root@tianqi-02 ~]#
service iptables save 保存規則
在iptables -F清空規則後,重啓service restart iptables.service(重啓服務器或者iptables規則),都會加載配置文件裏面的規則
[root@tianqi-02 ~]# service iptables restart
Redirecting to /bin/systemctl restart iptables.service
[root@tianqi-02 ~]# 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,ESTABLISHED
0 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:22
0 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, 1412 bytes)
pkts bytes target prot opt in out source destination
[root@tianqi-02 ~]#
目前咱們作的這些操做都是針對的filter表,這是默認的表。
查看nat表的規則:
[root@tianqi-01 network-scripts]# 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
[root@tianqi-01 network-scripts]#
使用iptables -nvL命令顯示的規則裏,能夠看到第一行是有數字的,加上-Z選項能夠將計數器清零:
[root@tianqi-01 network-scripts]# iptables -Z;iptables -nvL
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
0 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:22
0 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 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
[root@tianqi-01 network-scripts]#
[root@tianqi-02 ~]# iptables -t filter -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
49 3244 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 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:22
1 229 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 27 packets, 3484 bytes)
pkts bytes target prot opt in out source destination
[root@tianqi-02 ~]#
清零是爲了在某些需求下,能夠計算某個時間段某個ip傳送過來的數據包數量。
添加規則示例:
[root@tianqi-01 network-scripts]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
[root@tianqi-01 network-scripts]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
10 760 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 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:22
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
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)
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 3 packets, 356 bytes)
pkts bytes target prot opt in out source destination
[root@tianqi-01 network-scripts]#
[root@tianqi-01 network-scripts]# iptables -I INPUT -p tcp --dport 80 -j DROP
[root@tianqi-01 network-scripts]# iptables -nvL
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:80
23 1696 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 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:22
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
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)
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 3 packets, 356 bytes)
pkts bytes target prot opt in out source destination
插入規則會在最前面,添加的規則會在後面。
[root@tianqi-01 network-scripts]# iptables -D INPUT -p tcp --dport 80 -j DROP
[root@tianqi-01 network-scripts]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
34 2484 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 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:22
1 229 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
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)
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 3 packets, 356 bytes)
pkts bytes target prot opt in out source destination
[root@tianqi-01 network-scripts]# iptables -I INPUT -p tcp --dport 80 -j DROP
[root@tianqi-01 network-scripts]# iptables -nvL
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:80
44 3196 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 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:22
1 229 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
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)
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 3 packets, 356 bytes)
pkts bytes target prot opt in out source destination
[root@tianqi-01 network-scripts]#
[root@tianqi-01 network-scripts]# iptables -nvL --line-number #加上這個選項後會顯示行號
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
2 51 3684 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
3 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
4 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
5 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
6 1 229 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
7 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 8 packets, 2172 bytes)
num pkts bytes target prot opt in out source destination
刪除第7條規則
[root@tianqi-01 network-scripts]# iptables -D INPUT 7
[root@tianqi-01 network-scripts]# iptables -nvL --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
2 67 4776 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
3 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
4 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
5 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
6 1 229 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 3 packets, 372 bytes)
num pkts bytes target prot opt in out source destination
iptables -P OUTPUT DROP 默認的規則
默認的規則最好不要去改變!!!
注意: 儘可能不要隨意更改該配置,尤爲是在進行遠程登陸時,一旦執行該命令後將會斷開鏈接。這個策略設定後只能用命令:‘iptables -P OUTPUT ACCEPT’來恢復成原始狀態,不能使用-F參數。
友情連接:阿銘Linux