网络管理

 

              本章内容

Ø  基本网络配置

Ø  route命令

Ø  ip命令

Ø  netstat/ss命令

Ø  bonding配置

Ø  Centos7网络配置工具(nmcli命令)

Ø  网络组

 

 

1、基本网络配置

       Linux主机接入到网络,需要配置网络相关设置。

       一般包括如下内容:

              主机名

              IP/netmask

              路由:默认网关

              DNS服务器

                     DNS服务器

                     DNS服务器

                     第三DNS服务器

2、网卡名称

       接口命名方式:CentOS 6

              以太网:eth[0,1,2,...]

              pppppp[0,1,2,...]

       网络接口识别并命名相关的udev配置文件:

              /etc/udev/rules.d/70-persistent-net.rules

       查看网卡:

              dmesg |grep –i eth

              ethtool -i eth0

              卸载网卡驱动:

                     modprobe -r e1000

                     rmmod e1000

              装载网卡驱动:

                     modprobe e1000

3、网络配置方式

       静态指定:

              ifconfig, route, netstat

              ip: object {link, addr, route}, ss, tc

              system-config-network-tuisetup

              配置文件

       动态分配:

              DHCP: Dynamic Host Configuration Protocol

3.1、配置网络接口

       ifconfig命令

              ifconfig [interface]

                            ifconfig -a

                            ifconfig IFACE [up|down]

              ifconfig interface [aftype] options | address ...

                     ifconfig IFACE IP/netmask [up]

                     ifconfig IFACE IP netmask NETMASK

                     注意:立即生效

                     启用混杂模式:[-]promisc

4route命令

       路由管理命令

       查看:route -n

       添加:route add

route add [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]

       目标:192.168.1.3 网关:172.16.0.1

route add -host 192.168.1.3 gw 172.16.0.1 dev eth0

       目标:192.168.0.0 网关:172.16.0.1

route add -net 192.168.0.0 netmask 255.255.255.0 gw 172.16.0.1 dev eth0

route add -net 192.168.0.0/24 gw 172.16.0.1 dev eth0

       默认路由,网关:172.16.0.1

route add -net 0.0.0.0 netmask 0.0.0.0 gw 172.16.0.1

route add default gw 172.16.0.1

       删除:route del

route del [-net|-host] target [gw Gw] [netmask Nm] [[dev] If]

       目标:192.168.1.3 网关:172.16.0.1

route del -host 192.168.1.3

       目标:192.168.0.0 网关:172.16.0.1

route del -net 192.168.0.0 netmask 255.255.255.0

5netstat命令

       netstat - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships

       显示网络连接:

netstat [--tcp|-t] [--udp|-u] [--raw|-w] [--listening|-l] [--all|-a] [--numeric|-n] [--extend|-e[--extend|-e]] [--program|-p]

              -t: tcp协议相关

              -u: udp协议相关

              -w: raw socket相关

              -l: 处于监听状态

              -a: 所有状态

              -n: 以数字显示IP和端口;

              -e扩展格式

              -p: 显示相关进程及PID

       常用组合:

              -tan, -uan, -tnl, -unl,ntua

例子:

[root@centos6 ~]#netstat -nt                #远程主机与当前建立连接情况

Active Internet connections (w/o servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State     

tcp        0      0 192.168.27.10:22            192.168.27.1:52391          ESTABLISHED

tcp        0      0 192.168.27.10:22            192.168.27.1:60547          ESTABLISHED

tcp        0      0 192.168.27.10:22            192.168.27.1:52411          ESTABLISHED

tcp        0      0 192.168.27.10:22            192.168.27.1:60548          ESTABLISHED

状态:

ESTABLISHED         #正在连接状态

LISTEN                      #正处于监听状态

[root@centos6 ~]#netstat -ntu                     #查看正在连接状态,使用tcpudp协议

Active Internet connections (w/o servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State     

tcp        0      0 192.168.27.10:22            192.168.27.1:52391          ESTABLISHED

tcp        0      0 192.168.27.10:22            192.168.27.1:60547          ESTABLISHED

tcp        0      0 192.168.27.10:22            192.168.27.1:52411          ESTABLISHED

tcp        0     96 192.168.27.10:22            192.168.27.1:60548          ESTABLISHED

[root@centos6 ~]#netstat -ntul             #查看处于监听状态

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:37904               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 :::48873                    :::*                        LISTEN     

tcp        0      0 :::111                      :::*                        LISTEN     

tcp        0      0 :::22                       :::*                        LISTEN     

tcp        0      0 ::1:631                     :::*                        LISTEN     

tcp        0      0 ::1:25                      :::*                        LISTEN      

udp        0      0 0.0.0.0:839                 0.0.0.0:*                              

udp        0      0 0.0.0.0:111                 0.0.0.0:*                              

udp        0      0 0.0.0.0:631                 0.0.0.0:*                               

udp        0      0 127.0.0.1:897               0.0.0.0:*                              

udp        0      0 0.0.0.0:49159               0.0.0.0:*                              

[root@centos6 ~]#netstat -ntua                   #查看所有监听状态连接

Active Internet connections (servers and established)

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:37904               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.27.10:22            192.168.27.1:60547          ESTABLISHED

tcp        0      0 192.168.27.10:22            192.168.27.1:52411          ESTABLISHED

tcp        0     96 192.168.27.10:22            192.168.27.1:60548          ESTABLISHED

tcp        0      0 :::48873                    :::*                        LISTEN     

tcp        0      0 :::111                      :::*                        LISTEN     

tcp        0      0 :::22                       :::*                        LISTEN     

tcp        0      0 ::1:631                     :::*                        LISTEN     

tcp        0      0 ::1:25                      :::*                        LISTEN      

udp        0      0 0.0.0.0:839                 0.0.0.0:*                              

udp        0      0 0.0.0.0:111                 0.0.0.0:*                              

udp        0      0 0.0.0.0:631                 0.0.0.0:*                               

udp        0      0 127.0.0.1:897               0.0.0.0:*                              

udp        0      0 0.0.0.0:49159               0.0.0.0:*                              

[root@centos6 ~]#netstat -ntuap          #查看所有监听状态下端口被哪个进程打开

                         

[root@centos6 ~]#netstat -ntuape        #-e查看扩展信息

       显示路由表:

              netstat {--route|-r} [--numeric|-n]

                            -r: 显示内核路由表

                            -n: 数字格式

例子:

[root@centos6 ~]#netstat -nr

Kernel IP routing table

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

192.168.27.0    0.0.0.0         255.255.255.0   U         0 0          0 eth1

172.18.0.0      0.0.0.0         255.255.0.0     U         0 0          0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth1

0.0.0.0         172.18.0.1      0.0.0.0         UG        0 0          0 eth0

       显示接口统计数据:

              netstat {--interfaces|-I|-i} [iface] [--all|-a] [--extend|-e] [--program|-p][--numeric|-n]

              netstat -i

              netstat –I=IFACE

              ifconfig -s eth0

例子:

[root@centos6 ~]#netstat -i    #显示接口统计数据

Kernel Interface table

Iface       MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg

eth0       1500   0    41109      0      0      0    12776      0      0      0 BMRU

eth1       1500   0     6142      0      0      0     3917      0      0      0 BMRU

lo        65536   0      112      0      0      0      112      0      0      0 LRU

说明:

ERR      错误

DRPdrop     丢失

OVR      过载

[root@centos6 ~]#netstat -Ieth0           #显示接口统计数据

Kernel Interface table

Iface       MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg

eth0       1500   0    41357      0      0      0    12776      0      0      0 BMRU

[root@centos6 ~]#netstat -I=eth0         #显示接口统计数据

Kernel Interface table

Iface       MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg

eth0       1500   0    41358      0      0      0    12776      0      0      0 BMRU

[root@centos6 ~]#ifconfig -s eth0              #显示接口统计数据

Iface   MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg

eth0       1500   0    41384      0      0      0    12776      0      0      0 BMRU

5.1ss命令

       1ss命令格式

       格式:ss [OPTION]... [FILTER]

netstat通过遍历proc来获取socket信息,ss使用netlink与内核tcp_diag模块通信获取socket信息。

       选项:

              -t: tcp协议相关

              -u: udp协议相关

              -w: 裸套接字相关

              -xunix sock相关

              -l: listen状态的连接

              -a: 所有

              -n: 数字格式

              -p: 相关的程序及PID

              -e: 扩展的信息

              -m内存用量

              -o计时器信息

       FILTER : [ state TCP-STATE ] [ EXPRESSION ]

       TCP的常见状态:

              tcp finite state machine:

              LISTEN: 监听

              ESTABLISHED:已建立的连接

              FIN_WAIT_1

              FIN_WAIT_2

              SYN_SENT

              SYN_RECV

              CLOSED

       EXPRESSION:

              dport =

              sport =

              示例:’( dport = :ssh or sport = :ssh )

常用组合:

              -tan, -tanl, -tanlp, -uan

       2)常用用法:

       ss -l 显示本地打开的所有端口

       ss -pl 显示每个进程具体打开的socket

       ss -t -a 显示所有tcp socket

       ss -u -a 显示所有的UDP Socekt

       ss -o state established '( dport = :ssh or sport = :ssh )' 显示所有已建立的      ssh连接

       ss -o state established '( dport = :http or sport = :http )' 显示所有已建立的HTTP连接

       ss -s 列出当前socket详细信息

例子:

[root@centos6 ~]#ss -l            #显示本地打开的所有端口

State      Recv-Q Send-Q       Local Address:Port           Peer Address:Port  

LISTEN     0      128                     :::48873                    :::*      

LISTEN     0      128                     :::sunrpc                   :::*      

LISTEN     0      128                      *:sunrpc                    *:*      

LISTEN     0      128                      *:37904                     *:*      

LISTEN     0      128                     :::ssh                      :::*      

LISTEN     0      128                      *:ssh                       *:*      

LISTEN     0      128              127.0.0.1:ipp                       *:*      

LISTEN     0      128                    ::1:ipp                      :::*      

LISTEN     0      100                    ::1:smtp                     :::*      

LISTEN     0      100              127.0.0.1:smtp                      *:*

[root@centos6 ~]#ss -pl          #显示每个进程具体打开的socket

State      Recv-Q Send-Q     Local Address:Port     Peer Address:Port  

LISTEN     0      128             :::48873         :::*        users:(("rpc.statd",1569,11))

LISTEN     0      128              :::sunrpc        :::*        users:(("rpcbind",1512,11))

LISTEN     0      128              *:sunrpc        *:*        users:(("rpcbind",1512,8))

LISTEN     0      128              *:37904         *:*        users:(("rpc.statd",1569,9))

LISTEN     0      128             :::ssh           :::*        users:(("sshd",1826,4))

LISTEN     0      128              *:ssh           *:*        users:(("sshd",1826,3))

LISTEN     0   128       127.0.0.1:ipp               *:*        users:(("cupsd",1609,7))

LISTEN     0        128             ::1:ipp              :::*        users:(("cupsd",1609,6))

LISTEN    0  100               ::1:smtp              :::*        users:(("master",1914,13))

LISTEN  0  100        127.0.0.1:smtp                  *:*        users:(("master",1914,12))

[root@centos6 ~]#ss -o state established '( dport = :ssh or sport = :ssh )'               #显示所有已建立ssh连接

Recv-Q Send-Q                        Local Address:Port                            Peer Address:Port  

0      0                             192.168.27.10:ssh                             192.168.27.1:60547    timer:(keepalive,78min,0)

0      0                             192.168.27.10:ssh                             192.168.27.1:60548    timer:(keepalive,78min,0)

[root@centos6 ~]#ss -s           #列出当前socket详细信息

Total: 457 (kernel 474)

TCP:   12 (estab 2, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 6

 

Transport Total     IP        IPv6

*         474       -         -       

RAW       0         0         0       

UDP       8         5         3       

TCP       12        7         5       

INET      20        12        8       

FRAG      0         0         0

6ip命令

      1ip link查看网卡信息

yum install bash-completion -y            #命令补全包(centos6)

       配置Linux网络属性:ip命令

       ip - show / manipulate routing, devices, policy routing and tunnels

              ip [ OPTIONS ] OBJECT { COMMAND | help }

              OBJECT := { link | addr | route }

              ip link - network device configuration

                            set dev IFACE

                                   可设置属性:

                                   up and down激活或禁用指定接口

                                          ifup/ifdown

                            show [dev IFACE]:指定接口

                                   [up]仅显示处于激活状态的接口

例子:

       ip link set IFACE [up|down]开启或关闭网卡

[root@centos6 ~]#ip link               #查看网卡信息

1: lo: mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:e9 brd ff:ff:ff:ff:ff:ff

3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:f3 brd ff:ff:ff:ff:ff:ff

[root@centos6 ~]#ifconfig

eth0      Link encap:Ethernet  HWaddr 00:0C:29:38:96:E9 

          inet addr:172.18.118.195  Bcast:172.18.255.255  Mask:255.255.0.0

          inet6 addr: fe80::20c:29ff:fe38:96e9/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:48904 errors:0 dropped:0 overruns:0 frame:0

          TX packets:5309 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:7910758 (7.5 MiB)  TX bytes:407663 (398.1 KiB)

 

eth1      Link encap:Ethernet  HWaddr 00:0C:29:38:96:F3 

          inet addr:192.168.27.10  Bcast:192.168.27.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe38:96f3/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:63961 errors:0 dropped:0 overruns:0 frame:0

          TX packets:61414 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:71829572 (68.5 MiB)  TX bytes:22998718 (21.9 MiB)

 

lo        Link encap:Local Loopback 

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:65536  Metric:1

          RX packets:10 errors:0 dropped:0 overruns:0 frame:0

          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:558 (558.0 b)  TX bytes:558 (558.0 b)

 

[root@centos6 ~]#ip link set eth0 down            #禁用网卡

[root@centos6 ~]#ifconfig            #eth0网卡禁用已不显示网卡信息

eth1      Link encap:Ethernet  HWaddr 00:0C:29:38:96:F3 

          inet addr:192.168.27.10  Bcast:192.168.27.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe38:96f3/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:64028 errors:0 dropped:0 overruns:0 frame:0

          TX packets:61451 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:71836562 (68.5 MiB)  TX bytes:23005628 (21.9 MiB)

 

lo        Link encap:Local Loopback 

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:65536  Metric:1

          RX packets:10 errors:0 dropped:0 overruns:0 frame:0

          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:558 (558.0 b)  TX bytes:558 (558.0 b)

[root@centos6 ~]#ip link        #查看网卡信息(eth0网卡状态)

1: lo: mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

2: eth0: mtu 1500 qdisc pfifo_fast state DOWN qlen 1000

    link/ether 00:0c:29:38:96:e9 brd ff:ff:ff:ff:ff:ff

3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:38:96:f3 brd ff:ff:ff:ff:ff:ff

[root@centos6 ~]#ip link set eth0 up         #启用网卡

[root@centos6 ~]#ip link              #查看网卡信息

1: lo: mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:e9 brd ff:ff:ff:ff:ff:ff

3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:f3 brd ff:ff:ff:ff:ff:ff

       ip link show IFACE(指定接口)

[root@centos6 ~]#ip link show          #查看网卡信息

1: lo: mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:e9 brd ff:ff:ff:ff:ff:ff

3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:f3 brd ff:ff:ff:ff:ff:ff

[root@centos6 ~]#ip link show eth0          #查看指定网卡信息

2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:e9 brd ff:ff:ff:ff:ff:ff

      2ip add 增加网卡地址

       ip addr { add | del } IFADDR dev STRING

              [label LABEL]添加地址时指明网卡别名

              [scope {global|link|host}]指明作用域

                            global: 全局可用(网卡地址工作内核中)

                            link: 仅链接可用

                            host: 本机可用

              [broadcast ADDRESS]指明广播地址

例子:

       ip addr  查看网卡地址信息

[root@centos6 ~]#ip a            #查看网卡地址信息

1: lo: mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: mtu 1500 qdisc pfifo_fast state DOWN qlen 1000

    link/ether 00:0c:29:38:96:e9 brd ff:ff:ff:ff:ff:ff

    inet 172.18.118.195/16 brd 172.18.255.255 scope global eth0

3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:f3 brd ff:ff:ff:ff:ff:ff

    inet 192.168.27.10/24 brd 192.168.27.255 scope global eth1

    inet6 fe80::20c:29ff:fe38:96f3/64 scope link

       valid_lft forever preferred_lft forever

       ip addr { add | del } IFADDR dev STRING增加网卡地址

[root@centos6 ~]#ip addr add 1.1.1.1/24 dev eth0          #一块网卡增加多个地址

[root@centos6 ~]#ifconfig            #使用ip addr增加网卡地址ifconfig查看不到该网卡地址

eth0      Link encap:Ethernet  HWaddr 00:0C:29:38:96:E9 

          inet addr:172.18.118.195  Bcast:172.18.255.255  Mask:255.255.0.0

          inet6 addr: fe80::20c:29ff:fe38:96e9/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:53272 errors:0 dropped:0 overruns:0 frame:0

          TX packets:5595 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:8357851 (7.9 MiB)  TX bytes:432673 (422.5 KiB)

 

eth1      Link encap:Ethernet  HWaddr 00:0C:29:38:96:F3 

          inet addr:192.168.27.10  Bcast:192.168.27.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe38:96f3/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:67106 errors:0 dropped:0 overruns:0 frame:0

          TX packets:65046 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:72164649 (68.8 MiB)  TX bytes:23458582 (22.3 MiB)

[root@centos6 ~]#ip a            #查看网卡地址

1: lo: mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:e9 brd ff:ff:ff:ff:ff:ff

    inet 172.18.118.195/16 brd 172.18.255.255 scope global eth0

    inet 1.1.1.1/24 scope global eth0         #新增网卡地址

    inet6 fe80::20c:29ff:fe38:96e9/64 scope link

       valid_lft forever preferred_lft forever

3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:f3 brd ff:ff:ff:ff:ff:ff

    inet 192.168.27.10/24 brd 192.168.27.255 scope global eth1

    inet6 fe80::20c:29ff:fe38:96f3/64 scope link

       valid_lft forever preferred_lft forever

       新增网卡地址并指定标签

例子:

[root@centos6 ~]#ip addr add 2.2.2.2/24 dev eth0 label eth0:1       #新增网卡地址及指定标签

[root@centos6 ~]#ip a            #查看网卡地址信息

1: lo: mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:e9 brd ff:ff:ff:ff:ff:ff

    inet 172.18.118.195/16 brd 172.18.255.255 scope global eth0

    inet 1.1.1.1/24 scope global eth0

    inet 2.2.2.2/24 scope global eth0:1             #新增网卡地址及指定标签

    inet6 fe80::20c:29ff:fe38:96e9/64 scope link

       valid_lft forever preferred_lft forever

3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:f3 brd ff:ff:ff:ff:ff:ff

    inet 192.168.27.10/24 brd 192.168.27.255 scope global eth1

    inet6 fe80::20c:29ff:fe38:96f3/64 scope link

       valid_lft forever preferred_lft forever

       删除新增网卡地址及标签

[root@centos6 ~]#ip addr add 3.3.3.3/24 dev eth0 label eth0:2           #新增网卡地址及指定标签

[root@centos6 ~]#ip addr del 3.3.3.3/24 dev eth0 label eth0:2            #删除网卡地址及标签

[root@centos6 ~]#ip a            #查看新增网卡及标签是否删除

1: lo: mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:e9 brd ff:ff:ff:ff:ff:ff

    inet 172.18.118.195/16 brd 172.18.255.255 scope global eth0

    inet 1.1.1.1/24 scope global eth0

    inet 2.2.2.2/24 scope global eth0:1

    inet6 fe80::20c:29ff:fe38:96e9/64 scope link

       valid_lft forever preferred_lft forever

3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:f3 brd ff:ff:ff:ff:ff:ff

    inet 192.168.27.10/24 brd 192.168.27.255 scope global eth1

    inet6 fe80::20c:29ff:fe38:96f3/64 scope link

       valid_lft forever preferred_lft forever

       ip address show - look at protocol addresses

              [dev DEVICE]

              [label PATTERN]

              [primary and secondary]

       ip address flush - 使用格式同show

              ip addr add 172.16.100.100/16 dev eth0 label eth0:0

              ip addr del 172.16.100.100/16 dev eth0 label eth0:0

              ip addr flush dev eth0 label eth0:0

例子:

       删除某网卡设备上所有标签

[root@centos6 ~]#ip addr flush dev eth0 label eth0:1            #删除某网卡上所有标签

[root@centos6 ~]#ip a            #查看某网卡上所有标签是否删除

1: lo: mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:e9 brd ff:ff:ff:ff:ff:ff

    inet 172.18.118.195/16 brd 172.18.255.255 scope global eth0

    inet 1.1.1.1/24 scope global eth0

    inet6 fe80::20c:29ff:fe38:96e9/64 scope link

       valid_lft forever preferred_lft forever

3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:f3 brd ff:ff:ff:ff:ff:ff

    inet 192.168.27.10/24 brd 192.168.27.255 scope global eth1

inet6 fe80::20c:29ff:fe38:96f3/64 scope link

       valid_lft forever preferred_lft forever

6.1ip route

       ip route - routing table management

                     添加路由:ip route add

                     ip route add TARGET via GW dev IFACE src SOURCE_IP

                                   TARGET:

                                                        主机路由:IP

                                                        网络路由:NETWORK/MASK

                                   ip route add 192.168.0.0/24 via 172.16.0.1

                                   ip route add 192.168.1.13 via 172.16.0.1

                                   添加网关:ip route add default via GW dev IFACE

                     ip route add default via 172.16.0.1

                     删除路由:ip route delete

                     ip route del TARGET

                     显示路由:ip route show|list

                     清空路由表:ip route flush [dev IFACE]  [via PREFIX]

                                   ip route flush dev eth0

例子:

       查看路由信息

[root@centos6 ~]#ip route

1.1.1.0/24 dev eth0  proto kernel  scope link  src 1.1.1.1

192.168.27.0/24 dev eth1  proto kernel  scope link  src 192.168.27.10

172.18.0.0/16 dev eth0  proto kernel  scope link  src 172.18.118.195

169.254.0.0/16 dev eth0  scope link  metric 1002

169.254.0.0/16 dev eth1  scope link  metric 1003

default via 192.168.27.2 dev eth1

[root@centos6 ~]#ip route list              #查看路由信息

192.168.27.0/24 dev eth1  proto kernel  scope link  src 192.168.27.10

172.18.0.0/16 dev eth0  proto kernel  scope link  src 172.18.118.195

169.254.0.0/16 dev eth0  scope link  metric 1002

169.254.0.0/16 dev eth1  scope link  metric 1003

default via 172.18.0.1 dev eth0

[root@centos6 ~]#ip route show          #查看路由信息

192.168.27.0/24 dev eth1  proto kernel  scope link  src 192.168.27.10

172.18.0.0/16 dev eth0  proto kernel  scope link  src 172.18.118.195

169.254.0.0/16 dev eth0  scope link  metric 1002

169.254.0.0/16 dev eth1  scope link  metric 1003

default via 172.18.0.1 dev eth0

       新增路由:ip route add TARGET via GW dev IFACE

[root@centos6 ~]#ip route add 2.2.2.0/24 via 192.168.27.200             #增加网络路由

[root@centos6 ~]#ip r             #查看路由信息

2.2.2.0/24 via 192.168.27.200 dev eth1             #新增网络路由

1.1.1.0/24 dev eth0  proto kernel  scope link  src 1.1.1.1

192.168.27.0/24 dev eth1  proto kernel  scope link  src 192.168.27.10

172.18.0.0/16 dev eth0  proto kernel  scope link  src 172.18.118.195

169.254.0.0/16 dev eth0  scope link  metric 1002

169.254.0.0/16 dev eth1  scope link  metric 1003

default via 192.168.27.2 dev eth1

[root@centos6 ~]#ip route add 3.3.3.3 via 192.168.27.200           #增加主机路由

[root@centos6 ~]#ip r             #查看路由信息

3.3.3.3 via 192.168.27.200 dev eth1           #新增主机路由

2.2.2.0/24 via 192.168.27.200 dev eth1

1.1.1.0/24 dev eth0  proto kernel  scope link  src 1.1.1.1

192.168.27.0/24 dev eth1  proto kernel  scope link  src 192.168.27.10

172.18.0.0/16 dev eth0  proto kernel  scope link  src 172.18.118.195

169.254.0.0/16 dev eth0  scope link  metric 1002

169.254.0.0/16 dev eth1  scope link  metric 1003

default via 192.168.27.2 dev eth1

[root@centos6 ~]#ip route add default via 172.18.0.1 dev eth0                       #增加默认路由

[root@centos6 ~]#ip r             #查看新增默认路由

192.168.27.0/24 dev eth1  proto kernel  scope link  src 192.168.27.10

172.18.0.0/16 dev eth0  proto kernel  scope link  src 172.18.118.195

169.254.0.0/16 dev eth0  scope link  metric 1002

169.254.0.0/16 dev eth1  scope link  metric 1003

default via 172.18.0.1 dev eth0             #新增默认路由

       删除路由:ip route delete

[root@centos6 ~]#ip route del default via 172.18.0.1 dev eth0

[root@centos6 ~]#ip r

192.168.27.0/24 dev eth1  proto kernel  scope link  src 192.168.27.10

172.18.0.0/16 dev eth0  proto kernel  scope link  src 172.18.118.195

169.254.0.0/16 dev eth0  scope link  metric 1002

169.254.0.0/16 dev eth1  scope link  metric 1003

       清空某网卡上路由表:ip route flush dev [IFACE]

[root@centos6 ~]#ip route flush dev eth0                 #清空eth0上所有路由

[root@centos6 ~]#ip r             #查看路由信息

192.168.27.0/24 dev eth1  proto kernel  scope link  src 192.168.27.10

169.254.0.0/16 dev eth1  scope link  metric 1003

7、网络配置文件

       IPMASKGWDNS相关配置文件:/etc/sysconfig/network-scripts/ifcfg-IFACE

       路由相关的配置文件:

              /etc/sysconfig/network-scripts/route-IFACE

              /etc/sysconfig/network-scripts/ifcfg-IFACE

              说明参考/usr/share/doc/initscripts-9.49.30/sysconfig.txt

                     DEVICE此配置文件应用到的设备

                     HWADDR对应的设备的MAC地址

                     BOOTPROTO激活此设备时使用的地址配置协议,常用的dhcp, static,none, bootp

                     NM_CONTROLLEDNMNetworkManager的简写,此网卡是否接受NM控制;建议CentOS6为“no

                     ONBOOT在系统引导时是否激活此设备

                     TYPE接口类型;常见有的Ethernet, Bridge

                     UUID设备的惟一标识

                     IPADDR指明IP地址

                     NETMASK子网掩码

                     GATEWAY: 默认网关

                     DNS1第一个DNS服务器指向

                     DNS2第二个DNS服务器指向

                     USERCTL普通用户是否可控制此设备

                            USERCTL=[yes|no]  #普通用户是否可以启用或禁用设备

                                   yes  #普通可以控制

                                   no   #root控制

                     PEERDNS如果BOOTPROTO的值为“dhcp”,是否允许dhcp server分配的dns服务器指向信息直接覆盖至/etc/resolv.conf文件中

                            PEERDNS=[yes|no]  #如果BOOTPROTO设置为DHCP,将从DHCP服务器获取DNS

                                   yes  #表示可以从DHCP获取DNS

                                   no   #表示不从DHCP服务器获取DNS,需要手动指定DNS

8、主机名和本地解析器

       配置当前主机的主机名:

              hostname [HOSTNAME]

              /etc/sysconfig/network

                     HOSTNAME=

提示:(良好习惯)

       修改主机名时,需要在/etc/hosts配置文件中也添加新的主机名,避免访问本机时出现错误

       解析器执行正向和逆向查询

       /etc/hosts            #具有dns功能

              本地主机名数据库和IP地址的映像

              对小型独立网络有用

              通常,在使用DNS前检查

              getent hosts 查看/etc/hosts 内容

[root@centos6 ~]#vim /etc/hosts          #添加主机名解析成IP地址

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

6.6.6.6         www.magedu.com

[root@centos6 ~]#ip a

1: lo: mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:e9 brd ff:ff:ff:ff:ff:ff

    inet 192.168.1.101/24 brd 192.168.1.255 scope global eth0

    inet 6.6.6.6/24 scope global eth0

    inet6 fe80::20c:29ff:fe38:96e9/64 scope link

       valid_lft forever preferred_lft forever

3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:38:96:f3 brd ff:ff:ff:ff:ff:ff

    inet 192.168.27.10/24 brd 192.168.27.255 scope global eth1

    inet6 fe80::20c:29ff:fe38:96f3/64 scope link

       valid_lft forever preferred_lft forever

[root@centos6 ~]#ping www.magedu.com         #直接找hosts文件中的主机名解析到IP地址

PING www.magedu.com (6.6.6.6) 56(84) bytes of data.

64 bytes from www.magedu.com (6.6.6.6): icmp_seq=1 ttl=64 time=0.025 ms

64 bytes from www.magedu.com (6.6.6.6): icmp_seq=2 ttl=64 time=0.059 ms

64 bytes from www.magedu.com (6.6.6.6): icmp_seq=3 ttl=64 time=0.057 ms

64 bytes from www.magedu.com (6.6.6.6): icmp_seq=4 ttl=64 time=0.048 ms

提示:/etc/hosts文件比/etc/resolv.conf文件优先级高

[root@centos6 ~]#getent hosts             #查看主机名配置文件

127.0.0.1       localhost localhost.localdomain localhost4 localhost4.localdomain4

127.0.0.1       localhost localhost.localdomain localhost6 localhost6.localdomain6

6.6.6.6         www.magedu.com

9dns名字解析

       DNS配置文件:/etc/resolv.conf

              /etc/resolv.conf

              nameserver DNS_SERVER_IP1

              nameserver DNS_SERVER_IP2

              nameserver DNS_SERVER_IP3

              search magedu.com

       /etc/nsswitch.conf            #可以修改hostsdns配置文件优先级

              /etc/hosts相比优先于DNS

[root@centos6 ~]#cat /etc/nsswitch.conf

。。。

#hosts:     db files nisplus nis dns

hosts:      files dns              #files优先于dnsfiles代表hosts文件

       正向解析:FQDN-->IP

              dig -t A FQDN

              host -t A FQDN

       反向解析:IP-->FQDN

              dig -x IP

              host -t PTR IP

10、网络配置文件

       /etc/sysconfig/network-scripts/route-IFACE

              注意service network restart生效

              两种风格:

                     (1) TARGET via GW

                     如:10.0.0.0/8 via 172.16.0.1

[root@centos6 network-scripts]#vim route-eth1     #配置永久路由

6.6.6.0 via 192.168.27.200

[root@centos6 network-scripts]#service network restart              #重启网卡

[root@centos6 network-scripts]#route -n          #查看路由表

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

6.6.6.0         192.168.27.200  255.255.255.255 UGH   0      0        0 eth1            #添加成功

192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

192.168.27.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1

169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1

0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

                     (2) 每三行定义一条路由

                     ADDRESS#=TARGET

                     NETMASK#=mask

                     GATEWAY#=GW

11、网卡别名

       对虚拟主机有用

       将多个IP地址绑定到一个NIC

              eth0:1 eth0:2eth0:3

例子:一块网卡配置一个动态地址,别名网卡只能配置静态地址

vim ifcfg-eth1

DEVICE=eht1

BOOTPROTO=DHCP

 

vim ifcfg-eth1:1

DEVICE=eth1:1

IPADDR=2.2.2.2

PREFIX=24

BOOTPROTO=none

总结:标准网卡配置动态地址也可以手动指定,别名网卡只能配置静态地址

       ifconfig命令:

              ifconfig eth0:0 192.168.1.100/24 up

              ifconfig eth0:0 down

       ip命令:

              ip addr add 172.16.1.2/16 dev eth0

              ip addr add 172.16.1.1/16 dev eth0 label eth0:0

              ip addr add 172.16.1.2/16 dev eth0 label eth0:0

              ip addr del 172.16.1.1/16 dev eth0 label eth0:0

              ip addr flush dev eth0 label eth0:0

12、设备别名

       为每个设备别名生成独立的接口配置文件

              关闭NetworkManager服务

              ifcfg-ethX:xxx

              必须使用静态联网

                            DEVICE=eth0:0

                            IPADDR=10.10.10.10

                            NETMASK=255.0.0.0

                            ONPARENT=yes

       注意:service network restart 生效

       参考/usr/share/doc/initscripts-*/sysconfig.txt

13、网络接口配置-bonding

       Bonding

       将多块网卡绑定同一IP地址对外提供服务,可以实现高可用或负载均衡。直接给两块网卡设置同一IP地址是不可以的。通过bonding,虚拟一块网卡对外提供连接,物理网卡被修改为相同的MAC地址。

13.1Bonding工作模式

       1Mode0balance-rr

       轮转(Round-robin)策略:从头到尾顺序的在每一个slave接口上面发送数据包。本模式提供负载均衡和容错的能力。

       2Mode1active-backup

       活动-备份(主备)策略:只有一个slave被激活,当且仅当活动的slave接口失败时才会激活其他slave。为了避免交换机发生混乱此时绑定的MAC地址只有一个外部端口上可见

       3Mode3broadcast

       广播策略:在所有的slave接口上传送所有的报文,提供容错能力

       4active-backupbalance-tlbbalance-alb模式不需要交换机的任何特殊配置。其他绑定模式需要配置交换机以便整合链接。如:Cisco交换机需要在模式023中使用EtherChannel,但在模式4中需要LACPEtherChannel

13.2Bonding配置

       1创建bonding设备的配置文件

       /etc/sysconfig/network-scripts/ifcfg-bond0

              DEVICE=bond0

              BOOTPROTO=none

              BONDING_OPTS= “miimon=100 mode=0”

              miimon=100       #监控间隔时间

              mode=0               #binding工作模式

       /etc/sysconfig/network-scripts/ifcfg-eth0

              DEVICE=eth0

              BOOTPROTO=none

              MASTER=bond0

              SLAVE=yes

              USERCTL=no

       2)查看bond0状态:/proc/net/bonding/bond0

例子:

[root@centos6 network-scripts]#rm -f ifcfg-eth*            #删除所有网卡配置文件

[root@centos6 network-scripts]#service NetworkManager status              #关闭NetworkManager

NetworkManager is stopped

[root@centos6 network-scripts]#cat >ifcfg-bond0          #配置bonding网卡

DEVICE=bond0

IPADDR=192.168.27.10

PREFIX=24

BONDING_OPTS="mode=1 miimon=100"

[root@centos6 network-scripts]#cat >ifcfg-eth0                    #配置eth0bonding网卡

DEVICE=eth0

MASTER=bond0

SLAVE=yes

[root@centos6 network-scripts]#cp ifcfg-eth0 ifcfg-eth1     

[root@centos6 network-scripts]#vim ifcfg-eth1              #配置eth0bonding网卡

DEVICE=eth1

MASTER=bond0

SLAVE=yes

[root@centos6 network-scripts]#ifconfig          #查看bonding网卡后网卡MAC地址

eth0      Link encap:Ethernet  HWaddr 00:0C:29:38:96:E9 

          inet addr:192.168.27.128  Bcast:192.168.27.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe38:96e9/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:65169 errors:0 dropped:0 overruns:0 frame:0

          TX packets:6682 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:9285265 (8.8 MiB)  TX bytes:561535 (548.3 KiB)

 

eth1      Link encap:Ethernet  HWaddr 00:0C:29:38:96:F3 

          inet addr:192.168.27.10  Bcast:192.168.27.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe38:96f3/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:70289 errors:0 dropped:0 overruns:0 frame:0

          TX packets:68070 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:72515642 (69.1 MiB)  TX bytes:23903410 (22.7 MiB)

[root@centos6 network-scripts]#service network restart              #重启网卡

[root@centos6 network-scripts]#ip a          #查看网卡信息

1: lo: mtu 65536 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: mtu 1500 qdisc pfifo_fast master bond0 state UP qlen 1000

    link/ether 00:0c:29:38:96:e9 brd ff:ff:ff:ff:ff:ff

3: eth1: mtu 1500 qdisc pfifo_fast master bond0 state UP qlen 1000

    link/ether 00:0c:29:38:96:e9 brd ff:ff:ff:ff:ff:ff

4: bond0: mtu 1500 qdisc noqueue state UP

    link/ether 00:0c:29:38:96:e9 brd ff:ff:ff:ff:ff:ff

    inet 192.168.27.10/24 brd 192.168.27.255 scope global bond0           #bond0网卡地址

    inet6 fe80::20c:29ff:fe38:96e9/64 scope link

       valid_lft forever preferred_lft forever

提示:三个网卡MAC地址全部相同,且MAC地址是eth0网卡MAC地址,表示eth0为活动网卡

[root@centos6 network-scripts]#ifconfig                 #查看bonding网卡信息

bond0     Link encap:Ethernet  HWaddr 00:0C:29:38:96:E9 

          inet addr:192.168.27.10  Bcast:192.168.27.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe38:96e9/64 Scope:Link

          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1

          RX packets:113 errors:0 dropped:0 overruns:0 frame:0

          TX packets:103 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:11054 (10.7 KiB)  TX bytes:12254 (11.9 KiB)

 

eth0      Link encap:Ethernet  HWaddr 00:0C:29:38:96:E9 

          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1

          RX packets:65283 errors:0 dropped:0 overruns:0 frame:0

          TX packets:6793 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:9296451 (8.8 MiB)  TX bytes:576291 (562.7 KiB)

 

eth1      Link encap:Ethernet  HWaddr 00:0C:29:38:96:E9 

          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1

          RX packets:70302 errors:0 dropped:0 overruns:0 frame:0

          TX packets:68074 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:72516620 (69.1 MiB)  TX bytes:23904080 (22.7 MiB)

[root@centos6 network-scripts]#cat /proc/net/bonding/bond0           #查看bond0状态

Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

 

Bonding Mode: fault-tolerance (active-backup)

Primary Slave: None

Currently Active Slave: eth0          #eth0为活动网卡

MII Status: up

MII Polling Interval (ms): 100

Up Delay (ms): 0

Down Delay (ms): 0

 

Slave Interface: eth0

MII Status: up

Speed: 1000 Mbps

Duplex: full

Link Failure Count: 0

Permanent HW addr: 00:0c:29:38:96:e9

Slave queue ID: 0

 

Slave Interface: eth1

MII Status: up

Speed: 1000 Mbps

Duplex: full

Link Failure Count: 0

Permanent HW addr: 00:0c:29:38:96:f3

Slave queue ID: 0

       关闭VM虚拟centos6系统上eth0网卡,再查看bond0状态的活动网卡

[root@centos6 network-scripts]#cat /proc/net/bonding/bond0           #关闭eth0网卡,eth1为活动网卡

Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

 

Bonding Mode: fault-tolerance (active-backup)

Primary Slave: None

Currently Active Slave: eth1                 #关闭eth0网卡,eth1为活动网卡

MII Status: up

MII Polling Interval (ms): 100

Up Delay (ms): 0

Down Delay (ms): 0

 

Slave Interface: eth0

MII Status: down

Speed: Unknown

Duplex: Unknown

Link Failure Count: 1

Permanent HW addr: 00:0c:29:38:96:e9

Slave queue ID: 0

 

Slave Interface: eth1

MII Status: up

Speed: 1000 Mbps

Duplex: full

Link Failure Count: 0

Permanent HW addr: 00:0c:29:38:96:f3

Slave queue ID: 0

       3miimon 是用来进行链路监测的如果miimon=100,那么系统每100ms 监测一次链路连接状态,如果有一条线路不通就转入另一条线路

       4)删除bond0

ifconfig bond0 down

rmmod bonding

       5)详细帮助:

/usr/share/doc/kernel-doc-version/Documentation/networking/bonding.txt

https://www.kernel.org/doc/Documentation/networking/bonding.txt

例子:

       删除bond0

[root@centos6 ~]#lsmod |grep bond            #查看加载bond模块

bonding               132885  0

[root@centos6 ~]#ifconfig bond0 down             #禁用bond0设备

       禁用bond后该bond0设备不显示,使用ifconfig -a可以查看被禁用设备

       卸载bonding设备,再使用ifconfig -a查看被禁用设备已不显示

[root@centos6 ~]#modprobe -r bonding            #卸载bonding设备

[root@centos6 network-scripts]#rm -f ifcfg-bond0         #删除bond0设备配置文件

       修改eth0eth1网卡信息

[root@centos6 network-scripts]#cat ifcfg-eth0        #eth0恢复默认设定

DEVICE=eth0

IPADDR=172.18.0.10

PREFIX=16

GATEWAY=172.18.0.1

DNS1=114.114.114.114

DNS2=223.5.5.5

[root@centos6 network-scripts]#cat ifcfg-eth1        #eth1恢复默认设定

DEVICE=eth1

IPADDR=192.168.27.10

PREFIX=24

14Centos7网络属性配置

       CentOS 6之前,网络接口使用连续号码命名:eth0eth1,当增加或删除网卡时,名

称可能会发生变化

       CentOS 7使用基于硬件,设备拓扑和设置类型命名:

       (1) 网卡命名机制

              systemd对网络设备的命名方式

              (a) 如果FirmwareBIOS主板上集成的设备提供的索引信息可用,且可预测则根据此索引进行命名,例如eno1

              (b) 如果FirmwareBIOSPCI-E扩展槽所提供的索引信息可用,且可预测,则根据此索引进行命名,例如ens1

              (c) 如果硬件接口的物理位置信息可用,则根据此信息进行命名,例如enp2s0

              (d) 如果用户显式启动,也可根据MAC地址进行命名,enx2387a1dc56

              (e) 上述均不可用时,则使用传统命名机制

14.1、网卡名称

       1)基于BIOS支持启用biosdevname软件

                     内置网卡:em1,em2             #dell服务自动命名网卡名称

                     pci卡:pYpX Yslot(插槽) ,X:port(某插槽某端口)

[root@centos6 network-scripts]#rpm -q biosdevname          

biosdevname-0.7.2-1.el6.x86_64

[root@centos6 network-scripts]#rpm -qi biosdevname          #查看biosdevname哪家公司发布

Name        : biosdevname                  Relocations: (not relocatable)

Version     : 0.7.2                             Vendor: CentOS

Release     : 1.el6                         Build Date: Wed 22 Mar 2017 12:30:37 PM CST

Install Date: Tue 15 May 2018 08:11:17 PM CST      Build Host: c1bm.rdu2.centos.org

Group       : System Environment/Base       Source RPM: biosdevname-0.7.2-1.el6.src.rpm

Size        : 55296                            License: GPLv2

Signature   : RSA/SHA1, Thu 23 Mar 2017 11:02:59 PM CST, Key ID 0946fca2c105b9de

Packager    : CentOS BuildSystem

URL         : http://linux.dell.com/files/biosdevname

Summary     : Udev helper for naming devices per BIOS names

Description :

biosdevname in its simplest form takes a kernel device name as an

argument, and returns the BIOS-given name it "should" be.  This is necessary

on systems where the BIOS name for a given device (e.g. the label on

the chassis is "Gb1") doesn't map directly and obviously to the kernel

name (e.g. eth0).

       2(2) 名称组成格式

                     en: Ethernet 有线局域网

                     wl: wlan 无线局域网

                     ww: wwan无线广域网

       名称类型:

                     o: 集成设备的设备索引号

                     s: 扩展槽的索引号

                     x: 基于MAC地址的命名

                     ps: enp2s1

                            p     #总线位置

                            s     #插槽位置

14.2、采用传统命名方式

       1)使用传统命名方式:

              (1) 编辑/etc/default/grub配置文件

                     GRUB_CMDLINE_LINUX ="rhgb quiet net.ifnames=0"

                     或:修改/boot/grub2/grub.cfg

              (2) grub2生成其配置文件

                     grub2-mkconfig -o /etc/grub2.cfg

              (3) 重启系统

例子:

       修改网卡名称(Centos7

       方法1

[root@centos7 ~]#ll /etc/grub2.cfg

lrwxrwxrwx. 1 root root 22 May 15 12:40 /etc/grub2.cfg -> ../boot/grub2/grub.cfg

[root@centos7 ~]#vim /etc/grub2.cfg         #修改grub2.cfg配置文件

### BEGIN /etc/grub.d/10_linux ###

menuentry 'CentOS Linux (3.10.0-862.el7.x86_64) 7 (Core)' --class centos --class g

nu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10

.0-862.el7.x86_64-advanced-b383ac92-8d3f-4240-bba2-29ca28911332' {

        load_video

        set gfxpayload=keep

        insmod gzio

        insmod part_msdos

        insmod xfs

        set root='hd0,msdos1'

        if [ x$feature_platform_search_hint = xy ]; then

          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-ef

i=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  2afbe302-ee0c-4b60

-bcb5-3a33ae9db654

        else

          search --no-floppy --fs-uuid --set=root 2afbe302-ee0c-4b60-bcb5-3a33ae9d

b654

        fi

        linux16 /vmlinuz-3.10.0-862.el7.x86_64 root=UUID=b383ac92-8d3f-4240-bba2-2

9ca28911332 ro rhgb quiet.UTF-8 net.ifnames=0             #增加内容

        initrd16 /initramfs-3.10.0-862.el7.x86_64.img

}

       方法2

[root@centos7 ~]#cat /etc/default/grub            #修改grub配置文件

GRUB_TIMEOUT=5

GRUB_DISTRIBUTOR="CentOS Linux"

GRUB_DEFAULT=saved

GRUB_DISABLE_SUBMENU=true

GRUB_TERMINAL_OUTPUT="console"

GRUB_CMDLINE_LINUX="rhgb quiet net.ifnames=0"              #添加net.ifnames=0

GRUB_DISABLE_RECOVERY="true"

[root@centos7 ~]#grub2-mkconfig -o /boot/grub2/grub.cfg               #生成grub.cfg

Generating grub configuration file ...

Found linux image: /boot/vmlinuz-3.10.0-862.el7.x86_64

Found initrd image: /boot/initramfs-3.10.0-862.el7.x86_64.img

Found linux image: /boot/vmlinuz-0-rescue-e9cb8f268ffa421f94912614cfdedb61

Found initrd image: /boot/initramfs-0-rescue-e9cb8f268ffa421f94912614cfdedb61.img

done

[root@centos7 ~]#reboot        #重启系统

[root@centos7 ~]#ifconfig            #网卡名称已修改成eth名称

eth0: flags=4163  mtu 1500

        inet 192.168.27.102  netmask 255.255.255.0  broadcast 192.168.27.255

        inet6 fe80::636d:6119:9b82:6bf1  prefixlen 64  scopeid 0x20

        ether 00:50:56:27:3e:dc  txqueuelen 1000  (Ethernet)

        RX packets 46  bytes 6523 (6.3 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 69  bytes 11881 (11.6 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

eth1: flags=4163  mtu 1500

        inet 192.168.1.111  netmask 255.255.255.0  broadcast 192.168.1.255

        inet6 fe80::fb31:fbea:31c7:27f1  prefixlen 64  scopeid 0x20

        ether 00:50:56:35:ef:eb  txqueuelen 1000  (Ethernet)

        RX packets 27  bytes 3423 (3.3 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 37  bytes 5287 (5.1 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

14.3Centos7网络配置工具

       1CentOS7主机名

              a、配置文件:/etc/hostname ,默认没有此文件,通过DNS反向解析获取主机名,主机名

                     默认为:localhost.localdomain

              b、显示主机名信息

                     hostname

                     hostnamectl status

              c、设置主机名

                     hostnamectl set-hostname centos7.magedu.com

提示:此命令自动修改主机名配置文件,但不会生效提示符,需要如下命令生效提示符:

                     exec bash     exit退出

建议:若修改主机名,需要在/etc/hosts文件中增加新的主机名

[root@centos7 network-scripts]#vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 centos7.magedu.com centos7

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

              d、删除文件/etc/hostname,恢复主机名localhost.localdomain

/etc/hostname            #主机名配置文件

       2CentOS 7网络配置工具

              图形工具:nm-connection-editor

              字符配置tui工具:nmtui

              命令行工具:nmcli

14.4nmcli命令

       1)地址配置工具:nmcli

       2nmcli [ OPTIONS ] OBJECT { COMMAND | help }

                     device - show and manage network interfaces

                     nmcli device help

                     connection - start, stop, and manage network connections

                     nmcli connection help

       3)修改IP地址等属性:

                     nmcli connection modify IFACE [+|-]setting.property value

                            setting.property:

                            ipv4.addresses ipv4.gateway

                            ipv4.dns1 ipv4.method manual | auto

       4)修改配置文件执行生效:systemctl restart network

                     nmcli con reload

       5nmcli命令生效:

                     nmcli con down eth0  

                     nmcli con up eth0

       提示:eth0为配置文件名,不是设备名称

       6)修改连接配置后,需要重新加载配置

nmcli con reload               #将磁盘上网卡配置文件加载到内存

nmcli con down system eth0 可被自动激活

nmcli con up “system eth0”           #启用内存中配置文件生效

nmcli dev dis eth0 禁用网卡,访止被自动激活

       7)图形工具

nm-connection-editor

       8)字符工具

nmtui            #相当于setup(centos6)

nmtui-connect

nmtui-edit

nmtui-hostname

例子:

       断开网卡连接及启用网卡

[root@centos7 network-scripts]#nmcli device disconnect eth1    #断开网卡连接(仅删除IP地址,状态仍存在)

Device 'eth1' successfully disconnected.

[root@centos7 network-scripts]#ifconfig          #查看eth1网卡状态情况

eth0: flags=4163  mtu 1500

        inet 192.168.27.7  netmask 255.255.255.0  broadcast 192.168.27.255

        inet6 fe80::636d:6119:9b82:6bf1  prefixlen 64  scopeid 0x20

        ether 00:50:56:27:3e:dc  txqueuelen 1000  (Ethernet)

        RX packets 2487  bytes 292197 (285.3 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 1428  bytes 254765 (248.7 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

eth1: flags=4163  mtu 1500           #IP地址被清空

        ether 00:50:56:35:ef:eb  txqueuelen 1000  (Ethernet)

        RX packets 17134  bytes 1513959 (1.4 MiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 195  bytes 29833 (29.1 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@centos7 network-scripts]#ip a          #查看eth1网卡状态信息

1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

    link/ether 00:50:56:27:3e:dc brd ff:ff:ff:ff:ff:ff

    inet 192.168.27.7/24 brd 192.168.27.255 scope global noprefixroute eth0

       valid_lft forever preferred_lft forever

    inet6 fe80::636d:6119:9b82:6bf1/64 scope link noprefixroute

       valid_lft forever preferred_lft forever

3: eth1: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

link/ether 00:50:56:35:ef:eb brd ff:ff:ff:ff:ff:ff

提示:状态仍是开启,但IP地址被删除

[root@centos7 network-scripts]#nmcli device connect eth1   #连接eth1网络

Device 'eth1' successfully activated with 'b677c5f5-14bb-42fe-b448-260c60cbf66a'.

[root@centos7 network-scripts]#ip a          #查看eth1设备信息

1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

    link/ether 00:50:56:27:3e:dc brd ff:ff:ff:ff:ff:ff

    inet 192.168.27.7/24 brd 192.168.27.255 scope global noprefixroute eth0

       valid_lft forever preferred_lft forever

    inet6 fe80::636d:6119:9b82:6bf1/64 scope link noprefixroute

       valid_lft forever preferred_lft forever

3: eth1: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

    link/ether 00:50:56:35:ef:eb brd ff:ff:ff:ff:ff:ff

    inet 172.18.120.135/16 brd 172.18.255.255 scope global noprefixroute dynamic eth1   #已显示IP地址

       valid_lft 86398sec preferred_lft 86398sec

    inet6 fe80::cc53:bdae:d989:1dcb/64 scope link noprefixroute

       valid_lft forever preferred_lft forever

       查看设备状态:nmcli device status

[root@centos7 ~]#nmcli device status        #查看设备状态

DEVICE      TYPE      STATE      CONNECTION      #connection代表网卡配置文件

eth1        ethernet  connected  Wired connection 2     #配置文件是系统分配创建

eth0        ethernet  connected  Wired connection 1

virbr0      bridge    connected  virbr0            

lo          loopback  unmanaged  --                

virbr0-nic  tun       unmanaged  --

提示:

    一块网卡可以多个配置文件,同一时间只有一个有效,可以临时切换配置文件

[root@centos7 ~]#nmcli connection show    #查看当前设备信息

NAME                UUID                                  TYPE      DEVICE

Wired connection 1  e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

Wired connection 2  fc519eb6-0f21-3e00-972d-edeca0b79e62  ethernet  eth1  

virbr0              690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

ens33               53e2b196-9724-4d47-a5bf-29f5893e4851  ethernet  --    

ens34               68fd5afa-10eb-45ab-a03d-f896eefb532c  ethernet  --

提示:

    当前查到的配置文件是在内存中

       查看网卡更详细信息

[root@centos7 network-scripts]#nmcli connection show eth0                    #查看网卡详细信息

connection.id:                          eth0

connection.uuid:                        e87e0e56-0126-3e0e-bae8-673911014cae

connection.stable-id:                   --

connection.type:                        802-3-ethernet

connection.interface-name:              --

connection.autoconnect:                 yes

connection.autoconnect-priority:        -999

connection.autoconnect-retries:         -1 (default)

connection.auth-retries:                -1

connection.timestamp:                   1529893756

connection.read-only:                   no

connection.permissions:                 --

connection.zone:                        --

connection.master:                      --

connection.slave-type:                  --

connection.autoconnect-slaves:          -1 (default)

connection.secondaries:                 --

connection.gateway-ping-timeout:        0

connection.metered:                     unknown

connection.lldp:                        default

802-3-ethernet.port:                    --

802-3-ethernet.speed:                   0

802-3-ethernet.duplex:                  --

802-3-ethernet.auto-negotiate:          no

802-3-ethernet.mac-address:             00:50:56:27:3E:DC

802-3-ethernet.cloned-mac-address:      --

802-3-ethernet.mac-address-blacklist:   --

802-3-ethernet.mtu:                     auto

802-3-ethernet.s390-subchannels:        --

802-3-ethernet.s390-nettype:            --

802-3-ethernet.s390-options:            --

802-3-ethernet.wake-on-lan:             default

802-3-ethernet.wake-on-lan-password:    --

ipv4.method:                            manual                #网卡IP地址获取方式:手动指定

ipv4.dns:                               --

ipv4.dns-search:                        --

ipv4.dns-options:                       ""

ipv4.dns-priority:                      0

ipv4.addresses:                         192.168.27.7/24

ipv4.gateway:                           --

ipv4.routes:                            --

ipv4.route-metric:                      -1

ipv4.route-table:                       0 (unspec)

ipv4.ignore-auto-routes:                no

ipv4.ignore-auto-dns:                   no

ipv4.dhcp-client-id:                    --

ipv4.dhcp-timeout:                      0 (default)

ipv4.dhcp-send-hostname:                yes

ipv4.dhcp-hostname:                     --

ipv4.dhcp-fqdn:                         --

ipv4.never-default:                     no

ipv4.may-fail:                          yes

ipv4.dad-timeout:                       -1 (default)

。。。

       删除网卡设备配置文件:nmcli connection delete con-name

[root@centos7 network-scripts]#nmcli connection delete ens33         #删除ens33配置文件

Connection 'ens33' (53e2b196-9724-4d47-a5bf-29f5893e4851) successfully deleted.

[root@centos7 network-scripts]#nmcli connection delete ens34         #删除ens34配置文件

Connection 'ens34' (68fd5afa-10eb-45ab-a03d-f896eefb532c) successfully deleted.

[root@centos7 network-scripts]#nmcli connection show        #查看未关联的配置文件

NAME                UUID                                  TYPE      DEVICE

Wired connection 1  e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

Wired connection 2  fc519eb6-0f21-3e00-972d-edeca0b79e62  ethernet  eth1  

virbr0              690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

[root@centos7 network-scripts]#ls                    #网卡配置文件已从磁盘删除,但内存中仍存在

ifdown-post      ifup-eth     ifup-sit

ifcfg-lo           ifdown-ppp       ifup-ib      ifup-Team

ifdown-routes    ifup-ippp    ifup-TeamPort

ifdown             ifdown-sit       ifup-ipv6    ifup-tunnel

ifdown-bnep        ifdown-Team      ifup-isdn    ifup-wireless

ifdown-eth         ifdown-TeamPort  ifup-plip    init.ipv6-global

ifdown-ib          ifdown-tunnel    ifup-plusb   network-functions

ifdown-ippp        ifup             ifup-post    network-functions-ipv6

ifdown-ipv6        ifup-aliases     ifup-ppp

ifdown-isdn        ifup-bnep        ifup-routes

       手动创建某网卡配置文件

[root@centos7 network-scripts]#nmcli connection add con-name office-eth1 ifname eth1 type ethernet ipv4.method auto connection.autoconnect yes         #新增网卡配置文件

Connection 'office-eth1' (b677c5f5-14bb-42fe-b448-260c60cbf66a) successfully added.

参数说明:

add               #添加设备配置文件名

com-name    #指定网卡对应配置文件名

ifname          #指定配置文件名对应网卡设备

type                     #指定网卡类型

ipv4.method        [auto|manual]      #指定网卡地址获取方式,自动或手动(若不指定该选项配置将失效

auto    =>BOOTPROTO=dhcp   #自动获取IP地址

manual                #手动指定IP地址

connection.autoconnect yes    =>ONBOOT=yes       #系统引导时是否激活此设备,yes表示激活

 

[root@centos7 network-scripts]#nmcli connection show  #查看新增网卡配置文件名,但未关联到网卡

NAME                UUID                                  TYPE      DEVICE

Wired connection 1  e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

Wired connection 2  fc519eb6-0f21-3e00-972d-edeca0b79e62  ethernet  eth1  

virbr0              690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

office-eth1         b677c5f5-14bb-42fe-b448-260c60cbf66a  ethernet  --    #新增网卡配置文件

       启用新增配置文件关联到对应网卡上(根据上面创建配置文件时对应网卡进行关联)

       命令:nmcli connection up con-name

[root@centos7 network-scripts]#nmcli connection up office-eth1            #启用配置文件关联到对应网卡上

Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)

[root@centos7 network-scripts]#nmcli connection show             #查看配置文件是否关联到对应网卡上

NAME                UUID                                  TYPE      DEVICE

Wired connection 1  e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

office-eth1         b677c5f5-14bb-42fe-b448-260c60cbf66a  ethernet  eth1   #office-eth1已关联到网卡上

virbr0              690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

Wired connection 2  fc519eb6-0f21-3e00-972d-edeca0b79e62  ethernet  --             #该配置文件已失效

       修改配置文件名称

[root@centos7 network-scripts]#nmcli connection modify Wired\ connection\ 2 con-name home-eth1

#修改配置文件名

[root@centos7 network-scripts]#nmcli connection show             #查看修改的配置文件名

NAME                UUID                                  TYPE      DEVICE

Wired connection 1  e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

office-eth1         b677c5f5-14bb-42fe-b448-260c60cbf66a  ethernet  eth1  

virbr0              690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

home-eth1           fc519eb6-0f21-3e00-972d-edeca0b79e62  ethernet  --     #修改成新配置文件名

[root@centos7 network-scripts]#pwd

/etc/sysconfig/network-scripts

[root@centos7 network-scripts]#ls             #查看网络目录下网卡配置生成两个网卡配置文件(只能有一个配置文件生效)

ifcfg-home-eth1    ifdown-post      ifup-eth     ifup-sit

ifcfg-lo           ifdown-ppp       ifup-ib      ifup-Team

ifcfg-office-eth1  ifdown-routes    ifup-ippp    ifup-TeamPort

ifdown             ifdown-sit       ifup-ipv6    ifup-tunnel

ifdown-bnep        ifdown-Team      ifup-isdn    ifup-wireless

ifdown-eth         ifdown-TeamPort  ifup-plip    init.ipv6-global

ifdown-ib          ifdown-tunnel    ifup-plusb   network-functions

ifdown-ippp        ifup             ifup-post    network-functions-ipv6

ifdown-ipv6        ifup-aliases     ifup-ppp

ifdown-isdn        ifup-bnep        ifup-routes

       将自动获取IP地址修改成手动指定IP地址

[root@centos7 network-scripts]#cat ifcfg-home-eth1           #查看网卡默认地址获取方法

HWADDR=00:50:56:35:EF:EB

TYPE=Ethernet

PROXY_METHOD=none

BROWSER_ONLY=no

BOOTPROTO=dhcp         #自动获取IP地址

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=home-eth1

UUID=fc519eb6-0f21-3e00-972d-edeca0b79e62

ONBOOT=yes

AUTOCONNECT_PRIORITY=-999

[root@centos7 network-scripts]#nmcli connection modify home-eth1 ipv4.addresses 172.18.0.7/14 ipv4.gateway 172.18.0.1 ipv4.dns 114.114.114.114 ipv4.method manual              #将自动获取地址修改成手动指定IP地址

[root@centos7 network-scripts]#nmcli connection show             #home-eth1配置文件未生效

NAME                UUID                                  TYPE      DEVICE

Wired connection 1  e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

office-eth1         b677c5f5-14bb-42fe-b448-260c60cbf66a  ethernet  eth1  

virbr0              690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

home-eth1           fc519eb6-0f21-3e00-972d-edeca0b79e62  ethernet  --     #获取IP地址方式静态地址

[root@centos7 network-scripts]#nmcli connection up home-eth1             #切换成home-eth1配置文件

Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)

[root@centos7 network-scripts]#nmcli connection show

NAME                UUID                                  TYPE      DEVICE

Wired connection 1  e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

home-eth1           fc519eb6-0f21-3e00-972d-edeca0b79e62  ethernet  eth1  

virbr0              690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

office-eth1         b677c5f5-14bb-42fe-b448-260c60cbf66a  ethernet  --    

[root@centos7 network-scripts]#ifconfig         #查看eth1网卡IP地址

eth0: flags=4163  mtu 1500

        inet 192.168.27.102  netmask 255.255.255.0  broadcast 192.168.27.255

        inet6 fe80::636d:6119:9b82:6bf1  prefixlen 64  scopeid 0x20

        ether 00:50:56:27:3e:dc  txqueuelen 1000  (Ethernet)

        RX packets 1181  bytes 126687 (123.7 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 774  bytes 133107 (129.9 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

eth1: flags=4163  mtu 1500

        inet 172.18.0.7  netmask 255.252.0.0  broadcast 172.19.255.255         

              #手动指定IP地址生效

        inet6 fe80::fb31:fbea:31c7:27f1  prefixlen 64  scopeid 0x20

        ether 00:50:56:35:ef:eb  txqueuelen 1000  (Ethernet)

        RX packets 2570  bytes 209791 (204.8 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 100  bytes 14757 (14.4 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@centos7 network-scripts]#nmcli connection up office-eth1     #切换成自动获取IP地址配置文件

Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)

[root@centos7 network-scripts]#ifconfig         #查看网卡地址

eth0: flags=4163  mtu 1500

        inet 192.168.27.102  netmask 255.255.255.0  broadcast 192.168.27.255

        inet6 fe80::636d:6119:9b82:6bf1  prefixlen 64  scopeid 0x20

        ether 00:50:56:27:3e:dc  txqueuelen 1000  (Ethernet)

        RX packets 1237  bytes 132669 (129.5 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 810  bytes 141747 (138.4 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

eth1: flags=4163  mtu 1500

        inet 192.168.1.111  netmask 255.255.255.0  broadcast 192.168.1.255        

              #IP地址为自动获取

        inet6 fe80::cc53:bdae:d989:1dcb  prefixlen 64  scopeid 0x20

        ether 00:50:56:35:ef:eb  txqueuelen 1000  (Ethernet)

        RX packets 2719  bytes 227673 (222.3 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 124  bytes 18729 (18.2 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

       修改eth0获取IP地址方式为手动指定且修改配置文件名称为eth0

[root@centos7 network-scripts]#nmcli connection modify Wired\ connection\ 1 con-name eth0 ipv4.method manual ipv4.addresses 192.168.27.7/24

#修改eth0配置文件名及指定手动IP地址

[root@centos7 network-scripts]#nmcli connection show             #查看修改eth0配置文件名是否生效

NAME         UUID                                  TYPE      DEVICE

eth0         e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

office-eth1  b677c5f5-14bb-42fe-b448-260c60cbf66a  ethernet  eth1  

virbr0       690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

home-eth1    fc519eb6-0f21-3e00-972d-edeca0b79e62  ethernet  --    

home2-eth1   7f23d2b6-3bbc-434d-63b7-1406da340c5f  ethernet  --   

[root@centos7 network-scripts]#ls                    #查看是否生成eth0配置文件

ifcfg-eth0         ifdown-ipv6      ifup-aliases  ifup-ppp

ifcfg-home2-eth1   ifdown-isdn      ifup-bnep     ifup-routes

ifcfg-home-eth1    ifdown-post      ifup-eth      ifup-sit

ifcfg-lo           ifdown-ppp       ifup-ib       ifup-Team

ifcfg-office-eth1  ifdown-routes    ifup-ippp     ifup-TeamPort

ifdown             ifdown-sit       ifup-ipv6     ifup-tunnel

ifdown-bnep        ifdown-Team      ifup-isdn     ifup-wireless

ifdown-eth         ifdown-TeamPort  ifup-plip     init.ipv6-global

ifdown-ib          ifdown-tunnel    ifup-plusb    network-functions

ifdown-ippp        ifup             ifup-post     network-functions-ipv6

[root@centos7 network-scripts]#ifconfig         #查看eth0IP地址是否修改生效

eth0: flags=4163  mtu 1500

        inet 192.168.27.7  netmask 255.255.255.0  broadcast 192.168.27.255

                     #修改成手动指定IP地址

        inet6 fe80::636d:6119:9b82:6bf1  prefixlen 64  scopeid 0x20

        ether 00:50:56:27:3e:dc  txqueuelen 1000  (Ethernet)

        RX packets 1830  bytes 198500 (193.8 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 1194  bytes 213680 (208.6 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

eth1: flags=4163  mtu 1500

        inet 192.168.1.111  netmask 255.255.255.0  broadcast 192.168.1.255

        inet6 fe80::cc53:bdae:d989:1dcb  prefixlen 64  scopeid 0x20

        ether 00:50:56:35:ef:eb  txqueuelen 1000  (Ethernet)

        RX packets 3871  bytes 339910 (331.9 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 129  bytes 19179 (18.7 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

       读取磁盘配置文件加载到内存中:nmcli connection reload

[root@centos7 network-scripts]#cp ifcfg-home-eth1 ifcfg-home2-eth1    #手动创建多个eth1配置文件

[root@centos7 network-scripts]#vim ifcfg-home2-eth1               #修改新配置文件

UUID=fc519eb6-0f21-3e00-972d-edeca0b79e62

HWADDR=00:50:56:35:EF:EB

TYPE=Ethernet

PROXY_METHOD=none

BROWSER_ONLY=no

BOOTPROTO=none

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=home2-eth1         #不能与home-eth1同名,删除UUID

ONBOOT=yes

AUTOCONNECT_PRIORITY=-999

IPADDR=172.18.0.7

PREFIX=14

GATEWAY=172.18.0.1

DNS1=114.114.114.114

~                                                                                

"ifcfg-home2-eth1" 19L, 349C written                           

[root@centos7 network-scripts]#nmcli connection show      #查看新创建配置文件是否加载到内存中

NAME                UUID                                  TYPE      DEVICE

Wired connection 1  e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

office-eth1         b677c5f5-14bb-42fe-b448-260c60cbf66a  ethernet  eth1  

virbr0              690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

home-eth1           fc519eb6-0f21-3e00-972d-edeca0b79e62  ethernet  --    

提示:home2-eth1未加载到内存中

[root@centos7 network-scripts]#nmcli connection reload    #将磁盘内的网卡配置文件加载到内存中

[root@centos7 network-scripts]#nmcli connection show           #查看配置文件是否加载到内存中

NAME                UUID                                  TYPE      DEVICE

Wired connection 1  e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

office-eth1         b677c5f5-14bb-42fe-b448-260c60cbf66a  ethernet  eth1  

virbr0              690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

home-eth1           fc519eb6-0f21-3e00-972d-edeca0b79e62  ethernet  --    

home2-eth1          7f23d2b6-3bbc-434d-63b7-1406da340c5f  ethernet  --    #配置文件加载成功

提示:若要启用该配置文件,需要启用配置文件即可。命令如下:

[root@centos7 network-scripts]#nmcli connection up home2-eth1            #启用home2-eth1配置文件

       删除未关联的网卡配置文件:nmcli connection delete con-name

[root@centos7 ~]#nmcli connection show               #查看连接设备信息

NAME         UUID                                  TYPE      DEVICE

eth0         e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

office-eth1  b677c5f5-14bb-42fe-b448-260c60cbf66a  ethernet  eth1  

virbr0       690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

home-eth1    fc519eb6-0f21-3e00-972d-edeca0b79e62  ethernet  --     #配置文件未关联到网卡上

home2-eth1   7f23d2b6-3bbc-434d-63b7-1406da340c5f  ethernet  --    

[root@centos7 ~]#nmcli connection delete home-eth1         #删除未关联配置文件

Connection 'home-eth1' (fc519eb6-0f21-3e00-972d-edeca0b79e62) successfully deleted.

[root@centos7 ~]#nmcli connection delete home2-eth1              #删除未关联配置文件

Connection 'home2-eth1' (7f23d2b6-3bbc-434d-63b7-1406da340c5f) successfully deleted.

[root@centos7 ~]#nmcli connection show

NAME         UUID                                  TYPE      DEVICE

eth0         e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

office-eth1  b677c5f5-14bb-42fe-b448-260c60cbf66a  ethernet  eth1  

virbr0       690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

[root@centos7 ~]#cd /etc/sysconfig/network-scripts                    #切换到网络目录下

[root@centos7 network-scripts]#pwd

/etc/sysconfig/network-scripts

[root@centos7 network-scripts]#ls                    #磁盘上的网卡配置文件被删除

ifcfg-eth0         ifdown-post      ifup-eth     ifup-sit

ifcfg-lo           ifdown-ppp       ifup-ib      ifup-Team

ifcfg-office-eth1  ifdown-routes    ifup-ippp    ifup-TeamPort

ifdown             ifdown-sit       ifup-ipv6    ifup-tunnel

ifdown-bnep        ifdown-Team      ifup-isdn    ifup-wireless

ifdown-eth         ifdown-TeamPort  ifup-plip    init.ipv6-global

ifdown-ib          ifdown-tunnel    ifup-plusb   network-functions

ifdown-ippp        ifup             ifup-post    network-functions-ipv6

ifdown-ipv6        ifup-aliases     ifup-ppp

ifdown-isdn        ifup-bnep        ifup-routes

14.5、使用nmcli配置网络

       1NeworkManager是管理和监控网络设置的守护进程

       2)设备即网络接口,连接是对网络接口的配置。一个网络接口可有多个连接配置,但同时只有一个连接配置生效

       3)显示所有包括不活动连接

              nmcli con show

[root@centos7 network-scripts]#nmcli connection show

NAME         UUID                                  TYPE      DEVICE

eth0         e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

office-eth1  b677c5f5-14bb-42fe-b448-260c60cbf66a  ethernet  eth1  

virbr0       690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

       4)显示所有活动连接

              nmcli con show --active

[root@centos7 network-scripts]#nmcli connection show --active            

NAME         UUID                                  TYPE      DEVICE

eth0         e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

office-eth1  b677c5f5-14bb-42fe-b448-260c60cbf66a  ethernet  eth1  

virbr0       690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

       5)显示网络连接配置

              nmcli con show "System eth0“

       6)显示设备状态

              nmcli dev status

例子:

[root@centos7 network-scripts]#nmcli device status

DEVICE      TYPE      STATE      CONNECTION 

eth1        ethernet  connected  office-eth1

eth0        ethernet  connected  eth0       

virbr0      bridge    connected  virbr0     

lo          loopback  unmanaged  --         

virbr0-nic  tun       unmanaged  --

14.6、使用nmcli配置网络

       1)显示网络接口属性

nmcli dev show eth0

       2)创建新连接defaultIP自动通过dhcp获取

nmcli con add con-name default type Ethernet ifname eth0

       3)删除连接

nmcli con del default

       4)创建新连接static ,指定静态IP,不自动连接

nmcti con add con-name static ifname eth0 autoconnect no type

Ethernet ipv4.addresses 172.25.X.10/24 ipv4.gateway 172.25.X.254

提示:没有指定获取IP地址方法:ipv4.method [auto|manual],默认生效自动获取IP地址

例子: 验证此方法增加IP地址是否生效

[root@centos7 network-scripts]#cat ifcfg-office-eth1          #原始IP地址自动获取

TYPE=Ethernet

PROXY_METHOD=none

BROWSER_ONLY=no

BOOTPROTO=dhcp         #默认自动获取IP地址

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=office-eth1

UUID=b677c5f5-14bb-42fe-b448-260c60cbf66a

DEVICE=eth1

ONBOOT=yes

[root@centos7 network-scripts]#cat ifcfg-office-eth1          #修改成手动指定,方便后面实验验证

TYPE=Ethernet

PROXY_METHOD=none

BROWSER_ONLY=no

BOOTPROTO=none

IPADDR=172.18.0.7         #手动指定IP地址

PREFIX=16

GATEWAY=172.18.0.1

DNS1=114.114.114.114

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=office-eth1

UUID=b677c5f5-14bb-42fe-b448-260c60cbf66a

DEVICE=eth1

ONBOOT=yes

[root@centos7 network-scripts]#nmcli connection reload           #将磁盘网卡配置文件加载到内存中

[root@centos7 network-scripts]#nmcli connection up office-eth1     #启用配置文件,使配置文件修改参数生效

Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/11)

提示:

       若修改磁盘上网络目录下配置文件,使用nmcli命令生效配置文件结果,需要先执行nmcli con reload,最后执行nmcli con up con-name。其作用是:先将磁盘中配置文件加载到内存中,最后启用内存中配置文件即可生效

[root@centos7 network-scripts]#ifconfig         #查看eth1网卡IP地址是否生效为手动指定

eth0: flags=4163  mtu 1500

        inet 192.168.27.7  netmask 255.255.255.0  broadcast 192.168.27.255

        inet6 fe80::636d:6119:9b82:6bf1  prefixlen 64  scopeid 0x20

        ether 00:50:56:27:3e:dc  txqueuelen 1000  (Ethernet)

        RX packets 3536  bytes 404222 (394.7 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 2069  bytes 407049 (397.5 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

eth1: flags=4163  mtu 1500

        inet 172.18.0.7  netmask 255.255.0.0  broadcast 172.18.255.255

              #IP为手动指定,已生效

        inet6 fe80::cc53:bdae:d989:1dcb  prefixlen 64  scopeid 0x20

        ether 00:50:56:35:ef:eb  txqueuelen 1000  (Ethernet)

        RX packets 21513  bytes 1885065 (1.7 MiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 273  bytes 41784 (40.8 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

       以上操作方便下面实验验证条件,下面进行验证上面nmcli命令中未增加ipv4.method方法最终结果:

[root@centos7 network-scripts]#nmcli connection add con-name static-eth1 ifname eth1 ipv4.addresses 5.5.5.5/24 type ethernet

#创建配置文件并指定IP地址,但未增加ipv4.method方法

Connection 'static-eth1' (b6ff99cc-b544-4ac6-ba87-ce676c7f239e) successfully added.

[root@centos7 network-scripts]#nmcli connection show             #查看新增配置文件

NAME         UUID                                  TYPE      DEVICE

eth0         e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

office-eth1  b677c5f5-14bb-42fe-b448-260c60cbf66a  ethernet  eth1  

virbr0       690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

static-eth1  b6ff99cc-b544-4ac6-ba87-ce676c7f239e  ethernet  --             #增加成功

[root@centos7 network-scripts]#ifconfig                 #查看eth1网卡IP地址是否为手动指定

eth0: flags=4163  mtu 1500

        inet 192.168.27.7  netmask 255.255.255.0  broadcast 192.168.27.255

        inet6 fe80::636d:6119:9b82:6bf1  prefixlen 64  scopeid 0x20

        ether 00:50:56:27:3e:dc  txqueuelen 1000  (Ethernet)

        RX packets 3810  bytes 432998 (422.8 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 2234  bytes 435081 (424.8 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

eth1: flags=4163  mtu 1500

        inet 172.18.120.135  netmask 255.255.0.0  broadcast 172.18.255.255

              #static-eth1配置文件中手动指定IP地址未生效,最后生成自动获取IP

        inet6 fe80::8727:839f:ad54:2d27  prefixlen 64  scopeid 0x20

        ether 00:50:56:35:ef:eb  txqueuelen 1000  (Ethernet)

        RX packets 22388  bytes 1979975 (1.8 MiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 310  bytes 47802 (46.6 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@centos7 network-scripts]#cat ifcfg-static-eth1           #查看static-eth1配置文件中IP获取方法

TYPE=Ethernet

PROXY_METHOD=none

BROWSER_ONLY=no

BOOTPROTO=dhcp         #未增加ipv4.method manual参数,默认增加自动获取IP地址

IPADDR=5.5.5.5        #表面手动指定IP地址,但BOOTPROTO=dhcp该选项为自动获取,最终IP自动获取

PREFIX=24

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=static-eth1

UUID=b6ff99cc-b544-4ac6-ba87-ce676c7f239e

DEVICE=eth1

ONBOOT=yes

[root@centos7 network-scripts]#nmcli connection show static-eth1 |grep ipv4.method  #查看static-eth1详细信息

ipv4.method:                            auto

提示:发现配置文件中ipv4.method获取IP地址方法为“auto”,最终网卡会自动获取IP地址,显而已见该参数有多重要。

总结:

       如果增加配置文件且手动指定IP地址,必须增加ipv4.method方法,即使配置文件手动指定IP地址,最终生效IP地址方法是自动获取IP地址

       解决未增加ipv4.method参数,方法如下:

[root@centos7 network-scripts]#nmcli connection modify static-eth1 ipv4.method manual

[root@centos7 network-scripts]#nmcli connection up static-eth1

Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/13)

[root@centos7 network-scripts]#ifconfig

eth0: flags=4163  mtu 1500

        inet 192.168.27.7  netmask 255.255.255.0  broadcast 192.168.27.255

        inet6 fe80::636d:6119:9b82:6bf1  prefixlen 64  scopeid 0x20

        ether 00:50:56:27:3e:dc  txqueuelen 1000  (Ethernet)

        RX packets 4193  bytes 478057 (466.8 KiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 2508  bytes 490977 (479.4 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

 

eth1: flags=4163  mtu 1500

        inet 5.5.5.5  netmask 255.255.255.0  broadcast 5.5.5.255

        inet6 fe80::8727:839f:ad54:2d27  prefixlen 64  scopeid 0x20

        ether 00:50:56:35:ef:eb  txqueuelen 1000  (Ethernet)

        RX packets 25608  bytes 2217351 (2.1 MiB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 334  bytes 50955 (49.7 KiB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

       5)启用static连接配置

nmcli con up static

例子: 启用office-eth1连接配置

[root@centos7 network-scripts]#nmcli connection show            

NAME         UUID                                  TYPE      DEVICE

eth0         e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

static-eth1  b6ff99cc-b544-4ac6-ba87-ce676c7f239e  ethernet  eth1  

virbr0       690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

office-eth1  b677c5f5-14bb-42fe-b448-260c60cbf66a  ethernet  --    

[root@centos7 network-scripts]#nmcli connection up office-eth1            #启用office-eth1连接配置

Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/14)

[root@centos7 network-scripts]#nmcli connection show           

NAME         UUID                                  TYPE      DEVICE

eth0         e87e0e56-0126-3e0e-bae8-673911014cae  ethernet  eth0  

office-eth1  b677c5f5-14bb-42fe-b448-260c60cbf66a  ethernet  eth1  

virbr0       690dde41-cf04-44e6-a780-f42f86aeaf5a  bridge    virbr0

static-eth1  b6ff99cc-b544-4ac6-ba87-ce676c7f239e  ethernet  --

       6)启用default连接配置

nmcli con up default

       7)查看帮助

nmcli con add help

例子:

[root@centos7 network-scripts]#nmcli connection add help

Usage: nmcli connection add { ARGUMENTS | help }

 

ARGUMENTS := COMMON_OPTIONS TYPE_SPECIFIC_OPTIONS SLAVE_OPTIONS IP_OPTIONS [-- ([+|-]. )+]

 

  COMMON_OPTIONS:

                  type

                  ifname | "*"

                  [con-name ]

                  [autoconnect yes|no]

                  [save yes|no]

                  [master ]

                  [slave-type ]

。。。

14.6、使用nmcli配置网络:修改连接配置

       1)修改连接设置

nmcli con mod“static” connection.autoconnect no

nmcli con mod “static” ipv4.dns 172.25.X.254

nmcli con mod “static” +ipv4.dns 8.8.8.8

nmcli con mod “static” -ipv4.dns 8.8.8.8

nmcli con mod “static” ipv4.addresses “172.25.X.10/24 172.25.X.254”

nmcli con mod “static” +ipv4.addresses 10.10.10.10/16

       2DNS设置,存放在/etc/resolv.conf文件中

PEERDNS=no 表示当IP通过dhcp自动获取时,dns仍是手动设置,不自动获取。等价于下面命令:

nmcli con mod “system eth0” ipv4.ignore-auto-dns yes

例子:

       增加IP地址:nmcli con mod “static” +ipv4.dns 8.8.8.8

[root@centos7 network-scripts]#nmcli connection modify static-eth1 +ipv4.addresses 8.8.8.8/16   #增加IP地址

[root@centos7 network-scripts]#nmcli connection up static-eth1             #启用static-eth1配置文件

Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/16)

[root@centos7 network-scripts]#ip a          #查看eth1网卡IP地址

1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

    link/ether 00:50:56:27:3e:dc brd ff:ff:ff:ff:ff:ff

    inet 192.168.27.7/24 brd 192.168.27.255 scope global noprefixroute eth0

       valid_lft forever preferred_lft forever

    inet6 fe80::636d:6119:9b82:6bf1/64 scope link noprefixroute

       valid_lft forever preferred_lft forever

3: eth1: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

    link/ether 00:50:56:35:ef:eb brd ff:ff:ff:ff:ff:ff

    inet 5.5.5.5/24 brd 5.5.5.255 scope global noprefixroute eth1

       valid_lft forever preferred_lft forever

inet 8.8.8.8/16 brd 8.8.255.255 scope global noprefixroute eth1

      #增加IP地址生效

       valid_lft forever preferred_lft forever

    inet6 fe80::8727:839f:ad54:2d27/64 scope link noprefixroute

       valid_lft forever preferred_lft forever

       删除IP地址:nmcli con modif static-eth1 -ipv4.addresses IP

[root@centos7 network-scripts]#nmcli connection modify static-eth1 -ipv4.addresses 8.8.8.8/16  #删除IP地址

[root@centos7 network-scripts]#nmcli connection up static-eth1             #启用static配置文件

Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/17)

[root@centos7 network-scripts]#ip a          #查看eth1网卡IP地址是否被删除

1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

    link/ether 00:50:56:27:3e:dc brd ff:ff:ff:ff:ff:ff

    inet 192.168.27.7/24 brd 192.168.27.255 scope global noprefixroute eth0

       valid_lft forever preferred_lft forever

    inet6 fe80::636d:6119:9b82:6bf1/64 scope link noprefixroute

       valid_lft forever preferred_lft forever

3: eth1: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

    link/ether 00:50:56:35:ef:eb brd ff:ff:ff:ff:ff:ff

    inet 5.5.5.5/24 brd 5.5.5.255 scope global noprefixroute eth1

       valid_lft forever preferred_lft forever

    inet6 fe80::8727:839f:ad54:2d27/64 scope link noprefixroute

       valid_lft forever preferred_lft forever

提示:8.8.8.8IP地址已被删除

14.7nmcli命令说明

Command

Use

nmcli dev status

List all devices.

nmcli con show

List all connetions.

nmcli con up “NAME”

Activate a connection.

nmcli con down “NAME”

Deactivate a connection. The connecton   will restart if autoconnect is yes.

nmcli dev disconnect “DEV”

Bring down an interface and temporarily   disable autoconnect.

nmcli dev connect “DEV”

Bring up an interface and temporarily   enable autoconnect.

nmcli net off

Disable all managed interfaces

nmcli con add …

Add a new connection

nmcli con mod “NAME”

Modify a connction.

nmcli con del “NAME”

Delete a connection

       nmcli con mod参数与ifcfg-*文件中参数对照

nmcli   con mod

ifcfg-*文件

ipv4.method manual

BOOTPROTO=none

ipv4.method auto

BOOTPROTO=dhcp

ipv4.addresser “192.168.0.1/24   192.168.0.254”

IPADDR=192.168.0.1

PREFIX=24

GATEWAY=192.168.0.254

ipv4.dns 8.8.8.8

DNS=8.8.8.8

ipv4.dns-search example.com

DOMAIN=example.com

ipv4.ignore-auto-dns true

PEERDNS=no

connection.autoconnect yes

ONBOOT=yes

connection autoconnect no

ONBOOT=no

connection.id eth0

NAME=eth0

connection.igterface-name eth0

DEVICE=eth0

802-3-ethernet.mac-address …

HWADDR=…

14.8、网络配置文件

       设备配置被保存在文本文件中

              /etc/sysconfig/network-scripts/ifcfg-

              帮助文档列出完整项列表:/usr/share/doc/initscripts-*/sysconfig.txt

动态配置

静态配置

DEVICE=ethX

HWADDR=X.X.X…

BOOTPROTO=dhcp

ONBOOT=yes

Type=Ethernet

DEVICE=ehtX

HWADDR=X.X.X…

IPADDR=192.168.0.1

NETMASK=255.255.255.0PREFIX=24

GATEWAY=192.168.0.254

ONBOOT=yes

Type=Ethernet

15、网络组Network Teaming

       1)网络组:是将多个网卡聚合在一起方法,从而实现冗错和提高吞吐量

       2)网络组不同于旧版中bonding技术,提供更好的性能和扩展性

       3)网络组由内核驱动和teamd守护进程实现.

       4)多种方式runner

              broadcast

              roundrobin

              activebackup

              loadbalance

              lacp (implements the 802.3ad Link Aggregation Control Protocol)

15.1、网络组

启动网络组接口不会自动启动网络组中的port接口

启动网络组接口中的port接口总会自动启动网络组接口

禁用网络组接口会自动禁用网络组中的port接口

没有port接口的网络组接口可以启动静态IP连接

启用DHCP连接时,没有port接口的网络组会等待port接口的加入

15.2、创建网络组

15.2.1创建网络组接口

nmcli con add type team con-name  CNAME ifname INAME [config JSON ]

CNAME 连接名, INAME 接口名

JSON 指定runner方式

格式:'{"runner": {"name": " METHOD "}}'

METHOD 可以是broadcast, roundrobin,

activebackup, loadbalance, lacp

15.2.2、创建port接口

nmcli con add type team-slave con-name  CNAME ifname INAME master

TEAM

CNAME 连接名

INAME 网络接口名

TEAM 网络组接口名

连接名若不指定,默认为team-slave- IFACE

nmcli dev dis  INAME

nmcli con up  CNAME

INAME 设备名 CNAME 网络组接口名或port接口

15.3、网络组示例

nmcli con add type team con-name team0 ifname team0 config ‘{"runner": {"name": "loadbalance"}}'

nmcli con mod team0 ipv4.addresses 192.168.1.100/24

nmcli con mod team0 ipv4.method manual

nmcli con add con-name team0-eth1 type team-slave ifname eth1 master team0

nmcli con add con-name team0-eth2 type team-slave ifname eth2 master team0

nmcli con up team0

nmcli con up team0-eth1

nmcli con up team0-eth2

teamdctl team0 state     #查看网络组状态

  nmcli dev dis eth1

15.4、实验:创建网络组

ip link

nmcli con add type team con-name team0 ifname team0 config '{"runner": {"name": "activebackup"}}'

nmcli con mod team0 ipv4.addresses '192.168.0.100/24'

nmcli con mod team0 ipv4.method manual

nmcli con add con-name team0-port1 type team-slave ifname eth1 master team0

nmcli con add con-name team0-port2 type team-slave ifname eth2 master team0

teamdctl team0 state

ping -I team0 192.168.0.254

nmcli dev dis eno1

teamdctl team0 state

nmcli con up team0-port1

nmcli dev dis eno2

teamdctl team0 state

nmcli con up team0-port2

teamdctl team0 state

15.5、管理网络组配置文件

/etc/sysconfig/network-scripts/ifcfg-team0配置文件:

/etc/sysconfig/network-scripts/ifcfg-team0

DEVICE=team0

DEVICETYPE=Team

TEAM_CONFIG="{\"runner\": {\"name\": \"broadcast\"}}"

BOOTPROTO=none

IPADDR0=172.25.5.100

PREFIX0=24

NAME=team0

ONBOOT=yes

/etc/sysconfig/network-scripts/ifcfg-team0-eth1配置文件:

/etc/sysconfig/network-scripts/ifcfg-team0-eth1

DEVICE=eth1

DEVICETYPE=TeamPort

TEAM_MASTER=team0

NAME=team0-eth1

ONBOOT=yes

15.6、删除网络组

nmcli connection down team0

teamdctl team0 state    

nmcli connection show

nmcli connectioni delete team0-eth0

nmcli connectioni delete team0-eth1

nmcli connection show