10.11 Linux网络相关
•ifconfig 查看网卡ip
如果没有此命令则需要进行安装(yum install net-tools)
[root@linux-5 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.88.5 netmask 255.255.255.0 broadcast 192.168.88.255
inet6 fe80::531b:14ea:3a75:f113 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:58:d2:aa txqueuelen 1000 (Ethernet)
RX packets 48508 bytes 28087085 (26.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 67588 bytes 16710879 (15.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
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
ifconfig -a 查看网卡ip(包含宕掉的网卡)
• ifup ens33/ifdown 网卡名称 启动/关闭网卡
用于对单一网卡进行配置后,对网卡进行重启的操作,如果正在进行远程连接,慎用ifdown,否则需要手动重启服务器。
ifdown 网卡名称 && ifup 网卡名称
该命令较为常用,先关掉再重启,两个命令一起执行,避免出现关掉网卡后不能连接的情况。
• 设定虚拟网卡ens33:0
1.进入网卡配置文件所在目录(本例为/etc/sysconfig/network-scripts/)
[root@linux-5 ~]# cd /etc/sysconfig/network-scripts/
[root@linux-5 network-scripts]# ls
ifcfg-ens33 ifdown-ippp ifdown-routes ifup ifup-ipv6 ifup-ppp ifup-tunnel
ifcfg-lo ifdown-ipv6 ifdown-sit ifup-aliases ifup-isdn ifup-routes ifup-wireless
ifdown ifdown-isdn ifdown-Team ifup-bnep ifup-plip ifup-sit init.ipv6-global
ifdown-bnep ifdown-post ifdown-TeamPort ifup-eth ifup-plusb ifup-Team network-functions
ifdown-eth ifdown-ppp ifdown-tunnel ifup-ippp ifup-post ifup-TeamPort network-functions-ipv6
2.拷贝一份网卡配置文件,并重命名为虚拟网卡的名称(ens33:0)
[root@linux-5 network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:0 //反斜杠为了拖译“:”
3.编辑虚拟网卡的配置文件
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=0dadc82b-dcff-4cca-b166-c2e3f074fc35
DEVICE=ens33:0 // 修改为虚拟网卡的名称
ONBOOT=yes
IPADDR=192.168.88.50
NETMASK=255.255.255.0
注:网关和DNS均可不填,因为是在原有网卡的基础上创建的虚拟网卡
4.保存退出,重启相应网卡
[root@linux-5 network-scripts]# ifdown ens33 && ifup ens33
成功断开设备 'ens33'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/6)
5.查看网卡IP,发现虚拟网卡已成功创建
[root@linux-5 network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.88.5 netmask 255.255.255.0 broadcast 192.168.88.255
inet6 fe80::531b:14ea:3a75:f113 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:58:d2:aa txqueuelen 1000 (Ethernet)
RX packets 49198 bytes 28147145 (26.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 67977 bytes 16765661 (15.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.88.50 netmask 255.255.255.0 broadcast 192.168.88.255
ether 00:0c:29:58:d2:aa txqueuelen 1000 (Ethernet)
• mii-tool 网卡名称 查看网卡是否连接
[root@linux-5 ~]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok
如果网卡已连接,则显示“link ok”,反之,则会显示“no link”
• ethtool 网卡名称 也可以查看网卡是否连接
[root@linux-5 ~]# 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: yes”,反之,则会显示“Link detected: no”
• 更改主机名
hostnamectl set-hostname 主机名称
注:更改主机名称后要重新登录才可完全生效
• DNS配置文件/etc/resolv.conf
注:仅用于一次性修改,当系统重启后,该系统的DNS依旧会被网卡配置文件中的DNS所覆盖
• /etc/hosts文件
hosts文件的作用是使本机的域名进行自定义解析
修改方式: 左边是解析后的目的IP,右面为解析前的域名,多个域名可以对应一个IP,用空格分隔开。
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
目的IP 解析前域名
如果是一个域名多个IP,只生效最后IP。
192.168.1.1 www.baidu.com //不生效
192.168.2.2 www.baidu.com //只有这个自定义域名解析会生效
10.12 firewalld和netfilter
• selinux临时关闭
setenforce 0
• selinux永久关闭
编辑配置文件/etc/selinux/config
vi /etc/selinux/config
• centos7之前使用netfilter防火墙,centos7开始使用firewalld防火墙,但在centos7系统上也可以运行netfilter防火墙,centos7默认开启firewalld防火墙,想要在centos7上使用netfilter防火墙,需要手动进行开启。
• 关闭firewalld开启netfilter方法
关闭firewalld防火墙服务
systemctl stop firewalld
禁止firewalld防火墙服务开机自启动
systemctl disable firewalld
打开netfilter,打开前需要先安装一个包 iptables-service
yum install -y iptables-services
允许netfilter防火墙服务开机自启动
systemctl enable iptables
开启netfilter防火墙服务
systemctl start iptables
查看防火墙规则
[root@linux-5 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
77 5124 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 41 packets, 5032 bytes)
pkts bytes target prot opt in out source destination
10.13 netfilter5表5链介绍
• netfilter的5个表:filter,nat,managle,raw,security
• filter表用于过滤包,最常用的表,有INPUT、FORWARD、OUTPUT三个链
• nat表用于网络地址转换,有PREROUTING、OUTPUT、POSTROUTING三个链
• managle表用于给数据包做标记(不常用)
• raw表可以实现不追踪某些数据包(不常用)
• security表在centos6中并没有,用于强制访问控制(MAC)的网络规则(不常用)
• netfilter最常用的5个链:INPUT、OUTPUT、FORWARD、PREROUTING、POSTROUTING
所谓链,可以看做是工作台,不同数据流向的数据包,会经过相应的工作台(链),到达工作台后,工作台可对数据包进行相应的操作(封堵、放通等)
INPUT:数据包进入本机时经过的链,可针对数据包源IP以及访问的目标端口等做一些操作限制。
OUTPUT:在本机产生的一些包,在出去之前需要经过的链,可针对数据包目的IP等做一些操作限制。
FORWARD:数据包进入本机时需要经过内核判断目的地是否为本机,如果不是,则经过FORWARD链进 行转发,可以通过FORWARD链对转发地址进行修改等操作。
PREROUTING:在数据包进入网卡时的那一刻(还未到本机),需要经过的链,可用于做端口映射。
端口映射:将实际可以被访问到的公网端口A所接收的数据传输给想要被访问却不能直接被访问的内网端口 B(A映射到B)。
POSTROUTING:在数据包发送到网卡时的那一刻(已从本机出去),需要经过的链,可用于做代理上网。
代理上网(地址转换):不同的内网用户与公网通信时,自身IP地址均会转换为出口的公网IP,用于和公网 进行通信。
• iptables传输数据包的过程
当一个数据包进入网卡时,它首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转送出去
数据包传输流程总体分为两种:
1.数据包需要进入本机,PREROUTING-->INPUT-->OUTPUT-->POSTROUTING
2.数据包不需要进入本机,PREROUTING-->FORWARD-->POSTROUTING
10.14 iptables语法
• 查看iptables规则
iptables -nvL
• 清空规则
iptables -F
注:如果清空后未保存至规则配置文件(/etc/sysconfig/iptables),则服务重启或系统重启后,netfilter防火墙服务依旧会重新加载规则配置文件中的原有规则。
• 重启iptables服务
service iptables restart
• 保存规则
service iptables save
• 查看指定的表
iptables -t 表名 -nvL
注:不加-t,默认为filter表
• 计数器清零
iptables -Z
清零前
[root@linux-5 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
382 29930 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
157 16186 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 309 packets, 23350 bytes)
pkts bytes target prot opt in out source destination
清零后
[root@linux-5 ~]# iptables -Z
[root@linux-5 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
6 428 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 4 packets, 448 bytes)
pkts bytes target prot opt in out source destination
注:清零后,往往还会有数据包,因为规则一直在生效
iptables -I/-A/-D 针对的链 -s 源IP -p 协议类型 --sport 源端口 -d 目的IP --dport 80 -i 网卡名称 -j 动作
-I 插入一条规则(放置在所有规则的最前面)
-A 增加一条规则
-D 删除一条规则
-s 来源IP
-p 指定协议
--sport 来源端口
-d 目标IP
--dport 目标端口
-i 指定网卡
-j 进行操作
规则匹配机制:由上至下匹配,如数据包匹配上第一条规则,即使后续还有其他规则也可与数据包相匹配,数据包也不会继续向下匹配,立即执行它所匹配到的第一条规则。
• 例1:增加一条规则,针对INPUT链,来源IP为192.168.188.1,针对TCP协议,来源端口为1234,目的IP为192.168.188.128,目的端口为80,针对ens33网卡,执行丢弃的动作
iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -i ens33 -j DROP
TIPS:与DROP相似的动作还有REJECT,区别在于,DROP直接将数据包丢掉,REJECT是查看数据包后将其 拒绝。
• 例2:插入/增加/删除一条规则,针对INPUT链,来源IP为1.1.1.1,执行丢弃的动作
iptables -I/-A/-D INPUT -s 1.1.1.1 -j DROP
• 例3:插入一条规则,针对INPUT链,来源IP为192.168.1.0/24网段,执行放通的动作
iptables -I INPUT -s 192.168.1.0/24 -i eth0 -j ACCEPT
• 查看iptables规则,并显示规则相对应的序号
iptables -nvL --line-numbers
• 根据规则相对应的序号,对相应规则进行快速删除
iptables -D 针对的链 规则序号
• 默认规则
[root@linux-5 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes) //默认规则
pkts bytes target prot opt in out source destination
6 428 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 4 packets, 448 bytes) //默认规则
pkts bytes target prot opt in out source destination
• 修改默认规则
iptables -P 针对的链 执行的动作
注:不要轻易修改默认规则,否则容易造成误拦截!