1. 程式人生 > >Centos7中網路及裝置相關配置

Centos7中網路及裝置相關配置


centos7中,不再贊成使用ifconfig工具,取而代之的是nmcli工具,服務管理也是以systemctl工具取代了service,這些之前版本的工具雖然在centos7中還可以繼續使用,只是出於版本相容的目的,
下面,我們就對centos7中網路配置和管理進行介紹和說明。
1.nmcli獲取網絡卡相關資訊
# nmcli connection show
--如網絡卡未啟用,也未建立相關配置檔案,則在device欄位顯示”—”。在配置完成,重啟網路服務後就會顯示相應的裝置名。
--注:
centos7中,對網絡卡的命名規則有所變更,具體如下。
1)eno1:由主機板BIOS內建的網絡卡;
2)ens1:由主機板BIOS內建的PCI-E介面的網絡卡;
3)enp2s0:PCI-E介面的獨立網絡卡,可能有多個網絡卡介面,所以,也許會有s0,s1...等的編號;
4)eth0:如上述命名規則都不適用,則回到原來的網絡卡命名及編號規則;

2.nmcli手工配置網絡卡
# nmcli connection modify ens1 \
connection.autoconnect yes \
ipv4.method manual \
ipv4.address 192.168.1.21/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns 114.114.114.114
--重啟網路服務
# systemctl restart network.service

# systemctl restart network

3.nmcli檢視確定網絡卡相關資訊
# nmcli connection show ens1
# nmcli device status

centos7中,不再使用ifconfig,以ip addr取而代之。
# ip addr

4.為網絡卡設定雙IP
# cd /etc/sysconfig/network-scripts/
# cp ifcfg-enps1 ifcfg-ens1:1
# vi ifcfg-enps1:1
--修改如下內容,儲存並退出。
NAME=ens1:1
DEVICE=ens1:1
IPADDR=192.168.1.22
PREFIX=24

# nmcli connection show ens1
--輸出結果中將看到該網絡卡有兩個IP地址。

--檢視所有網路裝置資訊
# ip addr
--輸出結果中兩個inet部分包含兩個IP地址。

5.配置hostname
# hostnamectl set-hostname www.test.com
# cat /etc/hostname

6.nmcli配置網絡卡自動獲取IP地址
# nmcli connection modify eth0 \
> connection.autoconnect yes \
> ipv4.method auto

# systemctl restart network

6.配置bonding網絡卡
6.1 bonding概念
將多塊網絡卡繫結到同一個IP地址,並對外提供服務,既可以實現高可用,還可以實現負載均衡。因為,不可以將兩塊網絡卡設定為同一個IP地址,通過bonding機制,可以虛擬一塊網絡卡對外提供服務,且物理網絡卡被對映為相同的MAC地址。
6.2 bonding工作模式
1) Mode 0 (balance-rr和Round-robin)
一次輪詢在每一個slave 介面上面傳送資料包,本模式提供負載均衡和容錯的能力。
2) Mode 1 (active-backup)
任何時刻只有一個slave被啟用,當且僅當活動的slave介面失敗時,才會啟用其他slave。為避免交換機發生混亂,該模式繫結的MAC地址只在一個外部埠上可見。
3) Mode 3 (broadcast)
所有slave介面上傳送所有報文,提供容錯能力。
此外,active-backup、balance-tlb 和 balance-alb 模式無需對交換機進行任何特殊配置,而其他模式為了整合連結,則需對交換機進行相關配置。
6.3 新增bonding步驟
1) 新增bonding介面
#nmcli connection add type bond con-name bond0 ifname bond0 mode active-backup ipv4.method manual ipv4.addresses 192.168.1.66/24 ipv4.dns 114.114.114.114 ipv4.gateway 192.168.1.1
--注
1)nmcli connection add:建立命令必須;
2)type bond:指定配置型別;
3)con-name bond0:指定配置檔名字;
4)ifname bond0:指定介面名字;
5)mode active-backup:指定繫結模式;
6)ipv4.method(manual | auto):指定ip地址模式,manual對應靜態地址,auto對應動態地址(ip,dns,gateway都無需指定);
7)ipv4.addresses:指定ipv4靜態地址;
8)ipv4.dns:指定dns伺服器;
9)ipv4.gateway:指定閘道器;
10)該命令自動建立配置檔案,伺服器重啟配置不會丟失。

2) 新增slave介面
# nmcli connection add con-name bond-slave0 type bond-slave ifname eth0 master bond0
# nmcli connection add con-name bond-slave1 type bond-slave ifname eth1 master bond0

3) 啟動繫結
--須先啟動slave介面
# nmcli connection up bond-slave0
# nmcli connection up bond-slave1
# nmcli connection up bond0

4) 檢視binding網絡卡資訊
cd /etc/sysconfig/network-scripts/
#cat /proc/net/bonding/bond0

6.4 刪除bonding步驟
1) down掉bonding裝置
# nmcli connection down bond0
# nmcli connection down bond-slave0
# nmcli connection down bond-slave1
2) 解除安裝bonding驅動模組
# lsmod | grep bonding
# modprobe -r bonding
3)刪除bonding相關配置檔案
# nmcli connection delete bond0
# nmcli connection delete bond-slave0
# nmcli connection delete bond-slave1
4)檢視啟動網絡卡裝置
# nmcli device status
# nmcli device show
# nmcli d c eth0
# nmcli d c eth1