本文是基于centosnode
配置文件:/etc/resolv.conflinux
hostname NAME。
重启后失效hostnamectl
1,静态指定:ios
命令:c++
ifcfg家族:shell
ifconfig:配置IP,NETMASK(子网掩码)centos
route:路由查看及管理服务器
路由条目类型:微信
netstat:查看状态及统计数据网络
iproute2家族:dom
CentOS7专用:nm(network manager)家族:
配置文件
redhat及相关发行版:/etc/sysconfig/network-scripts/ifcfg-网卡名
ll /etc/sysconfig/network-scripts/ifcfg-* -rw-r--r--. 1 root root 282 Nov 30 12:14 /etc/sysconfig/network-scripts/ifcfg-enp0s3 -rw-r--r--. 1 root root 254 Aug 24 2018 /etc/sysconfig/network-scripts/ifcfg-lo
2,动态指定:依赖于本地网络中的DHCP服务器
DHCP:Dynamic Host Configure Procotol
客户端请求DHCP服务器,给我:【IP/netmask】,【gateway】,【链接外网的路由】,使用命令:dhclient -d
客户端想DHCP服务器请求ip地址时,是使用RARP协议的。
RARP协议:我(客户端)有MAC地址,谁(DHCP服务器)能给我一个IP啊。以广播形式发送。
若是网络里有多个DHCP服务器的话,谁给的快,就使用谁给的ip。这个ip是有使用期限的。
到期前,客户端给DHCP服务器单独发送请求(注意,这里不是广播了),要求延期使用。
1,传统命名方式
2,可预测命名方案(centos7):支持多种不一样的命名机制。
基于固件(fireware)命名,或基于主板拓扑结构命名(pci插槽号等)。
命名格式的含义:
ifconfig interface [aftype] options | address ...
查看全部激活的网卡信息:ifconfig
查看全部的网卡信息(包括为激活的):ifconfig -a
查看某个网卡的信息(centos6):ifconfig 网卡名
# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 08:00:27:80:F4:35 inet addr:192.168.0.108 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe80:f435/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:570 errors:0 dropped:0 overruns:0 frame:0 TX packets:72 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:40376 (39.4 KiB) TX bytes:12224 (11.9 KiB)
查看某个网卡的信息(centos7):ifconfig 网卡名
# ifconfig enp0s3 enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.0.113 netmask 255.255.255.0 broadcast 192.168.0.255 inet6 fe80::b497:5ec:1efb:72b5 prefixlen 64 scopeid 0x20<link> ether 08:00:27:10:c2:53 txqueuelen 1000 (Ethernet) RX packets 16814 bytes 2630803 (2.5 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2433 bytes 569211 (555.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
flags:网卡支持的功能
激活网卡(给网卡赋予ip地址和子网掩码):
ipconfig eth0 192.168.0.110/24 up
24的含义是24个1,因此24个1对应的子网掩码就是255.255.255.0.
若是是16的话,对应的子网掩码就是255.255.0.0
ipconfig eth0 192.168.0.110 netmask 255.255.255.0 up
此命令是当即生效的,可是重启后,失效。
给一个网卡添加多个ip地址:ifconfig IFACE:LABEL 192.168.0.110/24
IFACE:LABEL:eth0:0或者eth0:1。必须有冒号,冒号前面必须是原来网卡的名字。
使网卡无效:ipconfig eth0 down
添加/删掉ipv6地址
ifconfig eth0 add addr/prefixlen
ifconfig eth0 del addr/prefixle
选项介绍:
启用某个功能/让某个功能无效:使用-号。
好比启用功能:promisc
# ifconfig eth0 promisc # ifconfig eth0 eth0 Link encap:Ethernet HWaddr 08:00:27:80:F4:35 inet addr:192.168.0.110 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe80:f435/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1 RX packets:2116 errors:0 dropped:0 overruns:0 frame:0 TX packets:715 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:185897 (181.5 KiB) TX bytes:116601 (113.8 KiB)
多了PROMISC。
好比禁用广播和组播功能:# ifconfig eth0 -allmulti
以数字格式查看路由条目:route -n
$ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 enp0s3 192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 enp0s3 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
反解数字成主机名,查看路由条目:route
$ route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default gateway 0.0.0.0 UG 100 0 0 enp0s3 192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 enp0s3 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
添加路由
路由条目类型:
route add [-net|-host] target [netmask Nm][gw Gw] [[dev] If]
net:表明目标是ip网络
host:表明目标是单个
Gw:下一跳的地址
Flags:
Metric:度量值,到达这个网络须要的开销。
Iface:通过本地的哪一个接口(网卡)
route结果里的Destination:target(目标网络的IP地址)
route结果里的Genmask:目标网络的掩码
route结果里的Gateway:下一跳的地址
实例:添加目标网络为10.0.0.0/8(由于目标是网络,因此使用-net),下一跳的地址是192.168.0.1,通过本地的eth0网卡(本地的网卡能够省略,会根据下一跳的地址,自动找出来:使用本地哪一个网卡)。
注意:下一跳的IP必须和本地的网卡在同一个网络里。
# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 # route add -net 10.0.0.0/8 gw 192.168.0.1 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 10.0.0.0 192.168.0.1 255.0.0.0 UG 0 0 0 eth0 # route add -net 10.0.0.0/16 gw 192.168.0.1 dev eth0 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 10.0.0.0 192.168.0.1 255.255.0.0 UG 0 0 0 eth0 10.0.0.0 192.168.0.1 255.0.0.0 UG 0 0 0 eth0
添加目标为单个IP
# route add -host 192.168.0.111 gw 192.168.0.1 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.111 192.168.0.1 255.255.255.255 UGH 0 0 0 eth0 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 10.0.0.0 192.168.0.1 255.255.0.0 UG 0 0 0 eth0 10.0.0.0 192.168.0.1 255.0.0.0 UG 0 0 0 eth0
注意:使用-host时,不能指定子网掩码;Flags处有了H标识。
删除路由:
route del addr/prefixlen gw GW dev if
删除单个IP:
# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.111 192.168.0.1 255.255.255.255 UGH 0 0 0 eth0 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 10.0.0.0 192.168.0.1 255.255.0.0 UG 0 0 0 eth0 10.0.0.0 192.168.0.1 255.0.0.0 UG 0 0 0 eth0 # route del 192.168.0.111 gw 192.168.0.1 dev eth0 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 10.0.0.0 192.168.0.1 255.255.0.0 UG 0 0 0 eth0 10.0.0.0 192.168.0.1 255.0.0.0 UG 0 0 0 eth0
删除默认网关:
# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0 0.0.0.0 192.168.0.2 0.0.0.0 UG 0 0 0 eth0 # route del default # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 0.0.0.0 192.168.0.2 0.0.0.0 UG 0 0 0 eth0
删除网络:
# route del -net 10.0.0.0 gw 192.168.0.1 netmask 255.255.0.0 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 10.0.0.0 192.168.0.1 255.0.0.0 UG 0 0 0 eth0 # route del -net 10.0.0.0/8 gw 192.168.0.1 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
如何让机器链接上互联网?通常都是网关机器是连着外网的,因此只须要添加一个路由体哦条目,让目的是外网的访问,都扔给网关就行了。
显示路由信息:-r
# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
-n:以数字方式显示。
显示网络链接信息
显示tcp协议的链接信息:-t
# netstat -t Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 64 192.168.0.111:ssh 192.168.0.110:56087 ESTABLISHED
显示udp协议的链接信息:-u
# netstat -u Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State
以数字形式显示链接信息:-n
$ netstat -tn Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 64 192.168.0.113:22 192.168.0.110:64168 ESTABLISHED
Proto:协议类型
Recv-Q:接受队列的排队数量
Send-Q:发送队列的排队数量
Lacal Address:这个链接的本机地址和端口号
Foreign Address:这个链接的另外一端的地址和端口号
State:链接的状态
显示sctp协议的链接信息:-S
显示raw(裸套接字)的链接信息:-w
显示全部状态的链接信息:-a
不加a的话,只显示state是非listen状态的链接;加上a就意味着显示全部状态的tcp链接信息。
# netstat -tan Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:53824 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 0 192.168.0.111:22 192.168.0.110:56087 ESTABLISHED tcp 0 0 :::43002 :::* LISTEN tcp 0 0 :::111 :::* LISTEN tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:631 :::* LISTEN tcp 0 0 ::1:25 :::* LISTEN
显示处于监听状态下的链接信息:-l
$ netstat -tln Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
显示链接是由哪一个进程(并显示pid)发起的:-p
# netstat -tpe Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name tcp 0 64 192.168.0.111:ssh 192.168.0.110:56087 ESTABLISHED root 14440 1874/sshd
显示扩展信息:-e
显示user和Inode
user:由哪一个用户打开的链接
Inode:打开的链接在linux里都存在一个与之对应的套接字文件,这个套接字文件的inode号码。
经常使用选项的组合:
-tan:显示全部状态的tcp链接,以数字形式表示。
-uan:显示全部状态的udp链接,以数字形式表示。
-tnl:只显示处于监听状态下的tcp链接,以数字形式表示。
-unl:只显示处于监听状态下的udp链接,以数字形式表示。
-tunlp:只显示处于监听状态下的tcp或udp链接,以数字形式表示,并显示是由哪一个进程(并显示pid)发起的。
查看全部网卡的接收和发送的信息:-i
$ netstat -i Kernel Interface table Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg enp0s3 1500 2472 0 0 0 1003 0 0 0 BMRU lo 65536 84 0 0 0 84 0 0 0 LRU virbr0 1500 0 0 0 0 0 0 0 0 BMU
查看指定网卡的接收和发送的信息:-I网卡名
。注意中间没有空格
$ netstat -Ienp0s3 Kernel Interface table Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg enp0s3 1500 2464 0 0 0 998 0 0 0 BMRU
Iface:网卡名
MTU:单词传输的单位字节数
RX/TX-OK:接收/发送成功的字节数
RX/TX-ERR:接收/发送错误的字节数
RX/TX-DRP:接收/发送丢弃的字节数
RX/TX-OVR:接收/发送溢出的字节数
FLG:此网卡启用的功能
注意:要想执行下面2个命令,必须存在/etc/sysconfig/network-scripts/ifcfg-网卡名文件才能够。
hostname命令:
当即生效,重启后失效。
hostname
hostname HOSTNAME
hostnamectl命令:centos7才可使用。
显示当前主机名的相信信息:hostnamectl status
$ hostnamectl status Static hostname: localhost.localdomain Icon name: computer-vm Chassis: vm Machine ID: 49e09ca283034d21b72a490d0560bad0 Boot ID: cbec389bce8c495c8ae9fefa9f2331e1 Virtualization: kvm Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-957.el7.x86_64 Architecture: x86-64
修改主机名:hostnamectl set-hostname
当即生效,而且永久有效。
hostnamectl的使用帮助:hostnamectl --help
经过修改配置文件,修改主机名称。
文件:/etc/sysconfig/network。
修改HOSTNAME=后面的内容,就修改了主机名称。但不能当即生效,重启后会生效,并永久有效。
# cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=c6
1,经过本地的/etc/hosts
添加一个条目:192.168.0.113 centos7 cent.com。一个IP地址后面能够有多个别名。
# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.0.113 centos7 cent.com # ping centos7 PING centos7 (192.168.0.113) 56(84) bytes of data. 64 bytes from centos7 (192.168.0.113): icmp_seq=1 ttl=64 time=0.644 ms # ping cent.com PING centos7 (192.168.0.113) 56(84) bytes of data. 64 bytes from centos7 (192.168.0.113): icmp_seq=1 ttl=64 time=0.352 ms
2,经过配置文件,指定DNS服务器的IP地址。这个DNS服务器本身若是没有维护,它就会自动去互联网上找别的DNS服务器去解析网址,因此这个DNS服务器要能链接上外网!
配置文件:/etc/resolv.conf
nameserver就是DNS服务器的IP,最多能够指定3个nameserver。下面的192.168.0.1是网关,能够链接外网。
# cat /etc/resolv.conf # Generated by NetworkManager nameserver 192.168.0.1
3,测试DNS服务器是否好用:dig -t A www.baidu.com
dig命令不会去先找/etc/hosts文件,它直接从dns服务器上找。
# dig -t A www.baidu.com ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.68.rc1.el6 <<>> -t A www.baidu.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 14893 ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ;www.baidu.com. IN A ;; ANSWER SECTION: www.baidu.com. 933 IN CNAME www.a.shifen.com. www.a.shifen.com. 50 IN A 39.156.66.18 www.a.shifen.com. 50 IN A 39.156.66.14 ;; Query time: 19 msec ;; SERVER: 192.168.0.1#53(192.168.0.1) ;; WHEN: Sat Jan 4 21:43:54 2020 ;; MSG SIZE rcvd: 90
ip命令后面紧跟,起名叫object。iproute2家族命令将来可能取代ifconfig命令。
object有:link,netns,route等等。
每一个object基本都有:add,del,set,show/list等操做。
并且,object和操做命令都支持简写。
1,添加,删除,修改网卡(接口):ip link...
ip link add
ip link del
ip link set
ip link show/list
link能够简写成li
list能够简写成li
show能够简写成sh
# ip link show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff # ip link show eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff
<BROADCAST,MULTICAST,UP,LOWER_UP>:启用的功能
qdisc:队列
pfifo_fast:先进先出队列
qlen:队列长度
brd:广播地址
启用/禁用网络接口:ip link set eth1 up/down
# ip li li eth1.1 4: eth1.1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff # ip link set eth1.1 up # ip li li 4: eth1.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff # ip link set eth1.1 down # ip li li eth1.1 4: eth1.1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff
启用/禁用多播功能:ip link set eth1 multicast on/off
# ip li li eth1.1 4: eth1.1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff # ip link set eth1.1 multicast off # ip li li eth1.1 4: eth1.1: <BROADCAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff # ip link set eth1.1 multicast on # ip li li eth1.1 4: eth1.1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff
修改网络接口的名称:ip link set eth1 name NAME
# ip link list 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff 3: veth1.2: <NO-CARRIER,BROADCAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether d2:01:ed:42:dd:67 brd ff:ff:ff:ff:ff:ff 4: eth1.1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff # ip link set eth1.1 name eth3.1 # ip link list 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff 3: veth1.2: <NO-CARRIER,BROADCAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether d2:01:ed:42:dd:67 brd ff:ff:ff:ff:ff:ff 4: eth3.1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff
修改mtu:ip link set eth1.1 mtu 1000
以太网的最大mtu值是1500
# ip link set eth3.1 mtu 1000 [root@c6 ~]# ip li li eth3.1 4: eth3.1: <BROADCAST,MULTICAST> mtu 1000 qdisc pfifo_fast state DOWN qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff
显示帮助:ip link help
2,在指定网卡上添加,删除,显示ip地址:ip addr...
在同一个网卡上能够添加多个不一样的ip地址!!
从指定网卡(接口)上删除ip地址:ip addr del ipaddr/len dev eth0
例子:从网卡eth0上删除ip地址192.168.0.11/24
# ip ad li 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff inet 192.168.0.111/24 scope global eth0 inet 192.168.0.11/24 scope global secondary eth0 inet6 fe80::a00:27ff:fe80:f435/64 scope link valid_lft forever preferred_lft forever # ip addr del 192.168.0.11/24 dev eth0 # ip ad li 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff inet 192.168.0.111/24 scope global eth0 inet6 fe80::a00:27ff:fe80:f435/64 scope link valid_lft forever preferred_lft forever
一次删除网卡上的全部ip
# ip addr flush dev eth0
显示网卡(接口)上的ip地址:ip addr show/list
其余选项:
给添加的ip地址起一个别名,不起别名的话,使用ifconfig
命令,只能看到一个ip地址。
别名不能乱起,格式:原来的网卡:冒号后面随意!
格式不对会出下面的错误:
# ip addr add 192.168.0.13/24 dev eth0 label sec "dev" (eth0) must match "label" (sec).
不加冒号,也能够成功,可是ifconfig
命令就会出错。
# ip addr add 192.168.0.14/24 dev eth0 label eth011 # ifconfig eth0 Link encap:Ethernet HWaddr 08:00:27:80:F4:35 inet addr:192.168.0.111 Bcast:0.0.0.0 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe80:f435/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:3930 errors:0 dropped:0 overruns:0 frame:0 TX packets:1314 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:316975 (309.5 KiB) TX bytes:205600 (200.7 KiB) eth011: error fetching interface information: Device not found
例子:给添加的ip一个label叫eth0:1。
# ip addr add 192.168.0.12/24 dev eth0 label eth0:1 # ip ad sh 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff inet 192.168.0.111/24 scope global eth0 inet 192.168.0.11/24 scope global secondary eth0 inet 192.168.0.12/24 scope global secondary eth0:1 inet6 fe80::a00:27ff:fe80:f435/64 scope link valid_lft forever preferred_lft forever # ifconfig eth0:1 eth0:1 Link encap:Ethernet HWaddr 08:00:27:80:F4:35 inet addr:192.168.0.12 Bcast:0.0.0.0 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
给新添加的IP一个广播地址:
例子:给ip一个广播地址192.168.0.255。
# ip addr add 192.168.0.13/24 dev eth0 label eth0:2 broadcast 192.168.0.255 # ip ad li 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff inet 192.168.0.111/24 scope global eth0 inet 192.168.0.11/24 scope global secondary eth0 inet 192.168.0.12/24 scope global secondary eth0:1 inet 192.168.0.13/24 brd 192.168.0.255 scope global secondary eth0:2 inet6 fe80::a00:27ff:fe80:f435/64 scope link valid_lft forever preferred_lft forever # ifconfig eth0:2 eth0:2 Link encap:Ethernet HWaddr 08:00:27:80:F4:35 inet addr:192.168.0.13 Bcast:192.168.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
给新添加的IP,一个可以使用的限定范围:
例子:添加一个IP,使用范围是global。还可使用的范围:link,host
# ip addr add 192.168.0.14/24 dev eth0 label eth0:3 broadcast 192.168.0.255 scope global # ip ad li 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff inet 192.168.0.111/24 scope global eth0 inet 192.168.0.11/24 scope global secondary eth0 inet 192.168.0.12/24 scope global secondary eth0:1 inet 192.168.0.13/24 brd 192.168.0.255 scope global secondary eth0:2 inet 192.168.0.14/24 brd 192.168.0.255 scope global secondary eth0:3 inet6 fe80::a00:27ff:fe80:f435/64 scope link valid_lft forever preferred_lft forever
3,在指定的网卡上添加,删除,修改路由:ip route ...
添加路由:必须指明目的,下一跳在哪,经过哪一个网卡,经过网卡上的哪一个ip(由于同一个网卡能够添加多个ip地址)
下面例子的目的是:0.0.0.0/0。
注意,和route add
命令不一样的是:目的不须要指明是单一ip仍是网络,会自动识别。
下一跳:192.168.0.1
经过哪一个网卡:eth0
经过eth0上的哪一个ip:192.168.0.11
# ip route add default via 192.168.0.1 dev eth0 src 192.168.0.111 # ip route list default via 192.168.0.1 dev eth0 src 192.168.0.111
修改路由/替换:ip route change/replace
用法和add同样。
删除路由:ip route del 192.168.0.12/24
若是目的地(192.168.0.12/24)是惟一的,则后面的dev,src等能够省略。
显示路由:ip route show/list
清空指定网段的路由:ip route flush 192/24
显示特定的路由:ip route get 192.168.3.3/24
命令:ss
。和netstat
命令很像,将来可能取代netstat
命令。
上面netstat命令的选项,ss也都有,用法也同样。
查看链接耗费的内存:-m
查看链接的计时器信息:-o
使用过滤功能,去特定想看的信息:FILTER := [ state STATE-FILTER ][ EXPRESSION ]
# ss -tan State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 :::111 :::* LISTEN 0 128 *:111 *:* LISTEN 0 128 :::22 :::* LISTEN 0 128 *:22 *:* LISTEN 0 128 *:39286 *:* LISTEN 0 128 127.0.0.1:631 *:* LISTEN 0 128 ::1:631 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::54873 :::* ESTAB 0 0 10.247.237.54:22 10.247.237.16:61550 # ss -tan '( dport = :22 or sport = :22 )' State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 :::22 :::* LISTEN 0 128 *:22 *:* ESTAB 0 64 10.247.237.54:22 10.247.237.16:61550 # ss -tan state ESTABLISHED '( dport = :22 or sport = :22 )' Recv-Q Send-Q Local Address:Port Peer Address:Port 0 64 10.247.237.54:22 10.247.237.16:61550
'( dport = :22 or sport = :22 )':只看目标端口是22或者源端口是22的链接。
state ESTABLISHED:只看tcp链接状态是ESTABLISHED的链接。
TCP的状态:
上面介绍的修改网络配置的命令,都是临时有效的,系统重启后,就都没了。
要想永久有效,修改修改配置文件或者使用特殊的命令。
特殊的命令:
system-config-network
或者setup
nmtui
配置文件:
ip/netmask/gw/dns等相关的配置文件:/etc/sysconfig/network-scripts/ifcfg-接口名
centos6配置文件示例:
DEVICE=eth0 HWADDR=08:00:27:80:F4:35 TYPE=Ethernet UUID=f303b98f-14d7-4198-900a-0b2329bb8cae ONBOOT=no NM_CONTROLLED=yes BOOTPROTO=dhcp
centos7配置文件示例:
TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=dhcp 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=enp0s3 UUID=e2d60303-d70d-4335-aabd-f6c4cf86fade DEVICE=enp0s3 ONBOOT=yes
DEVICE:设备名字,也就是ifcfg-DEVICE。
ONBOOT:系统系统后,是否自动激活此接口。
NETBOOT:网络引导:
UUID:此设别的惟一标识
IPV6INIT:是否初始化IPv6协议
BOOTPROTO:激活此接口时使用上面协议来激活此接口。
经常使用的有:dhcp,bootp,static,none。
若是指定成了dhcp,而且还手动指定了ip地址,则手动指定的ip地址不生效,dhcp生效。
TYPE:接口类型,常见的有:Ethernet,Bridge
DNS1:第一DNS服务器指向。
DNS2:第二(备用)DNS服务器指向。
DOMAIN:DNS搜索域
DEFROUTE:是不是默认路由所使用的接口(网卡)。
GATEWAY:默认网关
IPADDR:IP地址
NETMASK/PREFIX:子网掩码。centos6是NETMASK;centos7是PREFIX
IPV4_FAILURE_FATAL:ipv4出故障后,是不是严重错误。
USERCTL:是否容许普通用户控制此设备
PEERDNS:若是BOOTPROTO的值为dhcp
默认是yes:容许dhcp服务器给的dns服务器指向,覆盖本地的dns服务器指向。
no:不容许dhcp服务器给的dns服务器指向,覆盖本地的dns服务器指向。目的是使用本地的dns服务器指向。
NM_CONTROLLED:是否容许使用networkManage服务来控制此接口。
HWADDR:MAC地址。
修改配置文件后,必须重启网络服务,才能让修改生效。
centos6的网络服务有2个:network和NetworkManager。可是NetworkManager不稳定,不要使用。
centos7的网络服务有2个:network和NetworkManager。NetworkManager已经很稳定,推荐使用。
centos6重启网络服务的命令:service network restart
centos6开始/中止网络服务的命令:service network start|stop
查看centos6网络服务的命令:service network status
# service network status Configured devices: lo eth0 Currently active devices: lo eth0 eth1
Configured devices:有配置文件的接口(网卡),因为系统里存在文件:/etc/sysconfig/network-scripts/ifcfg-eth0;不存在/etc/sysconfig/network-scripts/ifcfg-eth1文件,全部这里没有显示eth1.
Currently active devices:处于激活的接口(网卡)
添加/etc/sysconfig/network-scripts/ifcfg-eth1文件后,再使用service network status
,eth1就出现了。
# service network status Configured devices: lo eth0 eth1 Currently active devices: lo eth0 eth1
centos7重启网络服务的命令也可使用:service network restart
可是最佳用法:systemctl restart|start|stop|status network[.service]
经过配置文件,给同一网卡添加多个ip地址。
建立一个/etc/sysconfig/network-scripts/ifcfg-网卡名字:0的文件,文件里的DEVICE名字,要修改为和文件名ifcfg-后面的部分相同。
注意:网卡别名的ip地址不能是从dhcp获取,因此BOOTPROTO的值不能够是dhcp,只能是static或none。ip地址必须手动指定。
路由相关的配置文件:/etc/sysconfig/network-scripts/route-接口名
支持2种方式,但不能够混用。
方式1:每行一个路由条目:TARGET VIA
例子:/etc/sysconfig/network-scripts/route-eth0文件的内容:
192.168.0.10/24 via 10.247.236.1
方式2:每2行一个路由条目
ADDRESS#=IP
NETMASK#=NETMASK
GATEWAY#=NEXTHOP
例子:/etc/sysconfig/network-scripts/route-eth0文件的内容:
ADDRESS0=20.0.0.0 METMASK0=255.0.0.0 GATEWAY0=10.247.236.1
nmcli,nmtui
nmcli device:至关于ip link,控制接口相关的链接。
# nmcli device status DEVICE TYPE STATE CONNECTION enp0s3 ethernet connected enp0s3 virbr0 bridge connected virbr0 lo loopback unmanaged -- virbr0-nic tun unmanaged --
# nmcli device disconnect virbr0 Device 'virbr0' successfully disconnected.
# nmcli device connect virbr0 Device 'virbr0' successfully activated with '11f16b3f-e0fd-4b46-9f7f-b786feb8489b'.
nmcli connection:管理接口上的地址的。至关于ip addr命令。
# nmcli connection show NAME UUID TYPE DEVICE enp0s3 e2d60303-d70d-4335-aabd-f6c4cf86fade ethernet enp0s3 virbr0 11f16b3f-e0fd-4b46-9f7f-b786feb8489b bridge virbr0
# nmcli connection down virbr0 Connection 'virbr0' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)
# nmcli connection modify enp0s3 +ipv4.address 10.247.236.12
# nmcli connection modify enp0s3 -ipv4.address 10.247.236.12