Red Hat Enterprise Linux 7 與 CentOS 7 中默認的網絡服務由 NetworkManager 提供,這是動態控制及配置網絡的守護進程,它用於保持當前網絡設備及鏈接處於工做狀態,同時也支持傳統的 ifcfg 類型的配置文件。bash
NetworkManager 能夠用於如下類型的鏈接:Ethernet,VLANS,Bridges,Bonds,Teams,Wi-Fi,mobile boradband(如移動3G)以及 IP-over-InfiniBand。針對與這些網絡類型,NetworkManager 能夠配置他們的網絡別名,IP 地址,靜態路由,DNS,×××鏈接以及不少其它的特殊參數。網絡
能夠用命令行工具 nmcli 來控制 NetworkManager。ide
在CentOS / RHEL 7中網絡管理命令行工具,也叫nmcli。常常使用ifconfig的用戶應該在CentOS 7中避免使用ifconfig了。nmcli的功能要強大、複雜的多。工具
## 使用語法oop
[root@CentOS7 ~]# nmcli helpui
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }this
OPTIONS | |
-t[erse] | terse output(簡潔的輸出) |
-p[retty] | pretty output(所有輸出) |
-m[ode] tabular|multiline | output mode(輸出模式 可選 tabular、multiline) |
-f[ields] <field1,field2,...>|all| | common specify fields to output(指定輸出字段) |
-e[scape] yes|no | escape columns separators in values(在值中轉義列分隔符) |
-n[ocheck] | don't check nmcli and NetworkManager versions(不要檢查nmcli和網絡管理器版本) |
-a[sk] | ask for missing parameters(找失蹤的參數) |
-w[ait] <seconds> | set timeout waiting for finishing operations(設置超時等待完成操做) |
-v[ersion] | show program version(顯示程序版本) |
-h[elp] | print this help(打印幫助) |
OBJECT | |
g[eneral] | NetworkManager's general status and operations(網絡管理器的通常狀態和操做) |
n[etworking] | overall networking control(總體網絡控制) |
r[adio] | NetworkManager radio switches(使用無線網絡) |
c[onnection] | NetworkManager's connections(使用其餘網絡) |
d[evice] | devices managed by NetworkManager(網絡設備管理) |
OBJECT和COMMAND能夠用全稱也能夠用簡稱,最少能夠只用一個字母,建議用頭三個字母。spa
OBJECT裏面咱們平時用的最多的就是connection和device,這裏須要簡單區分一下connection和device。命令行
如connection能夠簡寫爲con;3d
device可簡寫爲dev。
device - 指網絡接口,物理網卡
connection - 指鏈接,偏重於邏輯設置
多個connection能夠應用到同一個device,但同一時間只能啓用其中一個connection。
這樣的好處是針對一個網絡接口,咱們能夠設置多個網絡鏈接,好比靜態IP和動態IP,再根據須要up相應的connection。
要獲得系統中全部接口的地址信息
## 查看設備信息
[root@localhost ~]# ip addr show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:a3:93:f4 brd ff:ff:ff:ff:ff:ff inet 192.168.10.140/24 brd 192.168.10.255 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fea3:93f4/64 scope link valid_lft forever preferred_lft forever ## 查看當前設備 [root@localhost ~]# nmcli connection show NAME UUID TYPE DEVICE eno123 7b3f7a19-0f5e-4bb1-85ab-ac77d5b46f88 802-3-ethernet eno16777736
獲得特定鏈接的詳情
[root@localhost ~]# nmcli connection show eno16777736 connection.id: eno16777736 connection.uuid: 13756690-ac77-b776-4fc1-f5535cee6f16 connection.stable-id: -- connection.interface-name: eno16777736 connection.type: 802-3-ethernet connection.autoconnect: yes connection.autoconnect-priority: 0 connection.timestamp: 1513912633 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: -1 (default) 802-3-ethernet.port: -- 802-3-ethernet.speed: 0 802-3-ethernet.duplex: -- 802-3-ethernet.auto-negotiate: yes 802-3-ethernet.mac-address: -- 802-3-ethernet.cloned-mac-address: -- 802-3-ethernet.generate-mac-address-mask:-- 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: 1 (default) 802-3-ethernet.wake-on-lan-password: -- ipv4.method: manual ipv4.dns: ipv4.dns-search: ipv4.dns-options: (default) ipv4.dns-priority: 0 ipv4.addresses: 192.168.10.140/24 ipv4.gateway: 192.168.10.2 ipv4.routes: ipv4.route-metric: -1 ipv4.ignore-auto-routes: no ipv4.ignore-auto-dns: no ipv4.dhcp-client-id: -- ipv4.dhcp-timeout: 0 ipv4.dhcp-send-hostname: yes ipv4.dhcp-hostname: -- ipv4.dhcp-fqdn: -- ipv4.never-default: no ipv4.may-fail: no ipv4.dad-timeout: -1 (default) ipv6.method: auto ipv6.dns: ipv6.dns-search: ipv6.dns-options: (default) ipv6.dns-priority: 0 ipv6.addresses: ipv6.gateway: -- ipv6.routes: ipv6.route-metric: -1 ipv6.ignore-auto-routes: no ipv6.ignore-auto-dns: no ipv6.never-default: no ipv6.may-fail: yes ipv6.ip6-privacy: -1 (unknown) ipv6.addr-gen-mode: eui64 ipv6.dhcp-send-hostname: yes ipv6.dhcp-hostname: -- ipv6.token: -- GENERAL.NAME: eno16777736 GENERAL.UUID: 13756690-ac77-b776-4fc1-f5535cee6f16 GENERAL.DEVICES: eno16777736 GENERAL.STATE: activated GENERAL.DEFAULT: yes GENERAL.DEFAULT6: no GENERAL.×××: no GENERAL.ZONE: -- GENERAL.DBUS-PATH: /org/freedesktop/NetworkManager/ActiveConnection/5 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/Settings/1 GENERAL.SPEC-OBJECT: / GENERAL.MASTER-PATH: -- IP4.ADDRESS[1]: 192.168.10.140/24 IP4.GATEWAY: 192.168.10.2 IP6.ADDRESS[1]: fe80::20c:29ff:fea3:93f4/64 IP6.GATEWAY:
獲得網絡設備狀態
[root@localhost ~]# nmcli device status DEVICE TYPE STATE CONNECTION eno16777736 ethernet connected eno16777736 lo loopback unmanaged --
配置新鏈接
## 使用「dhcp」建立新的鏈接 [root@localhost ~]# nmcli connection add con-name "enodhcp" type ethernet ifname eno16777736 Connection 'enodhcp' (edd1525f-9c81-49bb-b98a-e76ad1314ab5) successfully added. connection add – 添加新的鏈接 con-name "enodhcp" – 鏈接名(可自定義) type – 設備類型 ifname – 接口名(指定網卡名稱) ## 不經過dhcp分配IP,使用「static」添加地址 [root@localhost ~]# nmcli connection add con-name "enostatic" ifname eno16777736 autoconnect no type ethernet ip4 192.168.10.145 gw4 192.168.10.2 Connection 'enostatic' (8e69d847-03d7-47c7-8623-bb112f5cc842) successfully added. connection add – 添加新的鏈接 con-name "enostatic" – 鏈接名(可自定義) type ethernet – 設備類型 ifname – 接口名(指定網卡名稱) ip4 - IP地址 gw4 - 網關 ## 更新鏈接;這裏若是你是用終端鏈接的話更新以後,當前終端會被卡死。斷開從新用新的IP地址鏈接就能夠 [root@localhost ~]# nmcli connection up enostatic Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5) ## 再查看網卡信息,IP地址已經改變 [root@localhost ~]# ip addr show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:a3:93:f4 brd ff:ff:ff:ff:ff:ff inet 192.168.10.145/32 brd 192.168.10.145 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::c7f7:1818:2ffc:f9ea/64 scope link valid_lft forever preferred_lft forever ## 查看路由配置,這樣能夠直觀的看到鏈接信息。 [root@localhost ~]# ip route default via 192.168.10.140 dev eno16777736 proto static metric 100 192.168.10.140/24 dev eno16777736 proto kernel scope link src 192.168.10.145 metric 100 ## 添加DNS設置到靜態鏈接中 [root@localhost ~]# nmcli connection modify "enostatic" ipv4.dns 114.114.114.114 ## 增長更多的DNS;由於是新添加額外的dns,全部在使用"+"號(+ipv4.dns),而不是ip4.dns。增長IP同理 [root@localhost ~]# nmcli connection modify "enostatic" +ipv4.dns 8.8.8.8 ## 添加一個額外的ip地址 [root@localhost ~]# nmcli connection modify "enostatic" +ipv4.addresses 192.168.10.146/24