七周三次课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