查看ubuntu当前已启用网卡信息,使用ifconfig命令:
也可以去/sys/class/net目录查看共有网卡设备多少个(包括没启用的)。
没启用的网卡怎么启用呢?
以root用户身份修改/etc/network/interfaces文件
假设/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 eth0 iface eth0 inet dhcp
如果添加新网卡eth1,可直接在interfaces文件追加:
1)自动获取IP
auto eth1 iface eth1 inet dhcp
2)静态IP地址
auto eth1 iface eth1 inet static address 192.168.0.112 #IP netmask 255.255.255.0 #子网 gateway 192.168.0.1 #网关 #dns-nameservers 1.2.4.8 223.5.5.5 #DNS
address 是 ip 地址,netmask 是子网掩码,gateway 是网关,dns-nameservers 是 DNS (看个人需求,若不需要指定DNS则不写,如果是虚拟机的话尽量写网关地址)
添加完网卡,启动可使用命令(root身份):
ifup eth1
若重启网卡则是
ifdown eth1 && ifup eth1
也可以用
service networking restart
附:ubuntu18.04以后
ubuntu从17.10开始,已放弃在/etc/network/interfaces里固定IP的配置,即使配置也不会生效,而是改成netplan方式 ,配置写在/etc/netplan/01-netcfg.yaml或者类似名称的yaml文件里。
18.04的server版本安装好以后,配置文件是:/etc/netplan/50-cloud-init.yaml,修改配置以后不用重启,执行 netplan apply 命令可以让配置直接生效。
以前的重启网络服务命令/etc/init.d/networking restart或者services network restrart也都会提示为无效命令。
并且在/etc/network/interfaces有这样解释:
#ifupdown has been replaced by netplan(5) on this system. See #/etc/netplan for current configuration. #To re-enable ifupdown on this system, you can run: #sudo apt install ifupdown
直接配置netplan吧 /etc/netplan/50-cloud-init.yaml
分配静态ip
network: ethernets: enp4s0: addresses: [192.168.0.20/24] //IP址 gateway4: 192.168.0.1 // 网关 nameservers: addresses: [114.114.114.114, 192.168.0.1] //DNS dhcp4: no optional: no version: 2
图片示例:
自动获取IP
network: ethernets: enp0s3: addresses: [] dhcp4: true enp0s8: addresses: [] dhcp4: true version: 2
图片示例:
必须要按照分层设置才能生效。
配置完,启用
netplan apply