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已经创建成功。网卡配置相关介绍到此结束。

 

 

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/w-s-y/p/9498115.html