linux相關網絡接口配置


在linux中網絡接口的配置文件一般都在一個文件下,設定接口,設備名,方案名,IP地址的獲取方式,開機時是否自動分配IP等等,這一切所有的設置都在配置文件/etc/sysconfig/network-script下,想設置那個網卡找到其對應文件即可編輯,說到網卡,在現實生活中為了提高網卡的冗余性,以防單點故障的發生,我們通常要適應實際生活的需要,對網卡進行相關設置,接下來我們一起來熟悉一下相關命令。

一、網卡本身相關命令

  • ifconfig  查看該虛擬機上相關網卡的信息

如果ifconfig命令后什么都不跟,那會顯示所有的網卡信息,如果ifconfig后跟上設備名,則顯示指定網卡的相關信息。

  • cd /etc/sysconfig/network-scripts  查看該目錄下的配置文件
[root@localhost network-scripts]# cat ifcfg-eth0 eth0的詳細配置
DEVICE=eth0 設備名 HWADDR=00:0C:29:6A:7A:68 該網卡的MAC地址 TYPE=Ethernet 設備類型為Ethernet UUID=158888e2-4c5d-4859-8be8-903cee9f7c7a ONBOOT=yes 表示隨着網絡服務的啟動,該網卡是否隨之啟動 NM_CONTROLLED=no 表示該網卡是否支持NetworkManager管理,默認yes表示支持,no代表不支持。 BOOTPROTO=dhcp|static|none 如刪除該行,則為手動設置 IPADDR=6.6.6.6 NETMASK=255.255.255.0 手動設置地址及掩碼 USERCTRL=yes 該網卡的開關是否被普通用戶所調整 PEERDNS=no                                          默認yes當該網卡啟動時,而PEERDNS=yes,則dns配置文件中的dns會被該網卡的dns設置所覆蓋,若網卡是dhcp則被dhcp的設置覆蓋,若網卡是靜態地址,則被靜態配置的dns覆蓋
  • 給網卡設置別名
[root@localhost network-scripts]# ifconfig eth0:0 172.17.2.241/24
 [root@localhost network-scripts]# vim ifcfg-eth0:0
 DEVICE=eth0:0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes HWADDR=00:0C:29:77:8E:C9 DEFROUTE=yes PEERDNS=yes PEERROUTES=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no IPADDR=172.17.2.241 NETMASK=255.255.255.0 NAME="System eth0"

二、配置bond

bond的配置主要是為了提高冗余性,然而centos6上和centos7上bond的相關配置不太一樣,接下來分別展示。

  • centos6上配置bond

1、為虛擬機再添加兩塊網卡。

2、創建bond0文件

[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-bond0
 [root@localhost network-scripts]# vim ifcfg-bond0
DEVICE=bond0 設備名修改為bond0 BONDING_OPTS="miimon=100 mode=1" bond此行是關鍵 TYPE=Ethernet IPADDR=20.20.20.6 NETMASK=255.255.255.0 ONBOOT=yes NM_CONTROLLED=no

3、添加了兩塊網卡后,分別為eth2和eth3,它們是用來做slave的,復制后修改相關配置

[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth2 [root@localhost network-scripts]# vim ifcfg-eth2 
DEVICE=eth2 TYPE=Ethernet ONBOOT=yes SLAVE=yes 重點行,同意為slave MASTER=bond0 管理者為bond0 NM_CONTROLLED=no [root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth3
 [root@localhost network-scripts]# vim ifcfg-eth3 
DEVICE=eth3 TYPE=Ethernet ONBOOT=yes SLAVE=yes 重點行,同意為slave MASTER=bond0 管理者為bond0 NM_CONTROLLED=no

 

4、重啟網絡服務

[root@localhost network-scripts]# service network restart
  • centos7配置bond

1、增加兩個網卡

2、增加bond的方案

[root@localhost ~]#nmcli connection add con-name bond0 ifname bond0 type bond mode active-backup

3、增加slave的方案給兩塊新加的網卡

[root@localhost ~]#nmcli connection add con-name slave-38 ifname ens38 type ethernet master bond0
 [root@localhost ~]#nmcli connection add con-name slave-39 ifname ens39 type ethernet master bond0

4、配置bond0

[root@localhost ~]#nmcli connection modify bond0 ipv4.method manual connection.autoconnect yes ipv4.addresses 20.20.20.20/24 ipv4.dns 8.8.8.8 ipv4.gateway 20.20.20.1

5、直接激活bond0

[root@localhost ~]#nmcli connection up bond0
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/27) [root@localhost ~]#ifconfig bond0

三、為一個網卡配置兩個方案

1、修改舊的網卡名

[root@localhost network-scripts]#nmcli connection modify ens33 connection.id class
 [root@localhost network-scripts]#mv ifcfg-ens33 ifcfg-class

2、將class的方案設置自動獲取ipv4地址

[root@localhost network-scripts]#nmcli connection modify class ipv4.method auto 

3、使方案生效

[root@localhost network-scripts]#nmcli connection up class

4、新增方案名為home

[root@localhost network-scripts]#nmcli connection add con-name home type ethernet ifname ens33

5、設置新增方案為靜態獲取地址

[root@localhost network-scripts]#nmcli connection modify home ipv4.method manual connection.autoconnect yes ipv4.addresses 8.8.8.8/24 ipv4.gateway 8.8.8.1 ipv4.dns 4.4.4.4

6、切換到新方案,使之生效

[root@localhost network-scripts]#nmcli connection up home

四、創建網絡組(team)提高網卡性能

1、創建team方案

[root@localhost network-scripts]#nmcli connection add con-name team0 type team ifname team0 config '{"runner": {"name": "loadbalance"}}'

2、創建slave的方案並指定master

[root@localhost network-scripts]#nmcli connection add con-name slave-38 type team-slave ifname ens38 master team0
 [root@localhost network-scripts]#nmcli connection add con-name slave-39 type team-slave ifname ens39 master team0

3、設置team0的ip

[root@localhost network-scripts]#nmcli connection modify team0 ipv4.method manual connection.autoconnect yes ipv4.addresses 20.20.20.20/24

4、激活team0及slave

[root@localhost network-scripts]#nmcli connection up team0
 [root@localhost network-scripts]#nmcli connection up slave-38
 [root@localhost network-scripts]#nmcli connection up slave-39

此時team已經創建成功。網卡配置相關介紹到此結束。

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM