假设有这样一个环境,Linux主机有两张网卡,分别是10.10.10.0/24网段和192.168.10.0/24网段,
网卡的dhcp配置:
首先使用ifconfig
查看网卡状况
(这里只识别到了一张网卡应该是虚拟机的原因,正常应该是可以识别到两张网卡的。识别到的网卡默认是使用dhcp获得IP地址的)
对网卡进行编辑
sudo vim /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5)
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto ens33
iface ens33 inet dhcp
# The primary network interface
auto ens34
iface ens34 inet dhcp
将ens33和ens34都设置为静态的IP地址,ens33和ens34在网络上分别使用192.168.101.0/24和10.10.10.0/24网段。
重启网络服务
sudo /etc/init.d/networking restart
网卡的静态路由配置:
使用ifconfig
查看网卡状况
对网卡进行编辑
sudo vim /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5)
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto ens33
iface ens33 inet static
address 192.168.101.188
netmask 255.255.255.0
gateway 192.168.101.1
# The primary network interface
auto ens34
iface ens34 inet static
address 10.10.10.188
netmask 255.255.255.0
gateway 10.10.10.1
重启网络服务
sudo /etc/init.d/networking restart
网卡的dhcp配置:
首先使用ifconfig
查看网卡状况
对网卡分别进行编辑
vim /etc/sysconfig/network-script/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=on
BOOTPROTO=dhcp ##
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATIL=no
IPV6_ADDR_GEN_MODE=stable-privacy
DEVICE=ens33
ONBOOT=yes ##
重启网络服务
systemctl restart network
网卡的静态路由配置:
使用ifconfig
查看网卡状况
对网卡分别进行编辑
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
IPADD=192.168.101.230
NETMASK=255.255.255.0
GATEWAY=192.168.101.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=ens33
UUID=19576245-d886-485a-a4a5-181b88dd799a
DEVICE=ens33
ONBOOT=yes
重启网络服务
systemctl restart network
Linux的网卡配置主要的只有两点:
操作 | CentOS | Ubuntu |
---|---|---|
修改配置路径 | /etc/sysconfig/network-script/ifcfg-ensxx | /etc/network/interface |
重启网卡 | systemctl restart network | /etc/init.d/networking restart |
原文:https://blog.51cto.com/14170805/2425141