配置网卡
ubuntu17.10
开始使用netplan
来配置网络- 传统的
ifupdown
已经被废弃,包括配套的配置文件/etc/network/interface
ubuntu 18.04 lts
的默认配置文件为/etc/netplan/50-cloud-init.yaml
0x1 启用 netplan
netplan
的配置文件夹为/etc/netplan
,这里面包含了所有网卡的配置ubuntu 18.04 lts
开始系统中内置了cloud-int
服务,这个服务会自动修改netplan
的网络配置
根据配置文件/etc/netplan/50-cloud-init.yaml
中的提示,使用以下命令可以禁用cloud-int
对网路的影响
echo "network: {config: disabled}" >/etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
0x2 配置 netplan
netplan
的配置文件是yaml
格式的,这个格式 根据缩进来区分配置快yaml
是key: value
的格式来指定配置,但是符号:
后一定要先写 空格 再接value
0x2.1 配置dhcp
- 这是一段
netplan
的配置文件/etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
enp2s0:
dhcp4: yes
dhcp6: no
optional: true
enp4s0:
dhcp4: yes
dhcp6: no
optional: true
version: 2
optional: true
表示:这个网卡是可选的,开机时配置失败的话就直接跳过enp2s0
和enp4s0
是两张网卡(ip a
查看所有网卡)
0x2.2 配置为 静态ip
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
enp2s0:
dhcp4: no
dhcp6: no
addresses: [192.168.123.31/24]
gateway4: 192.168.123.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
enp4s0:
dhcp4: yes
dhcp6: no
optional: true
version: 2
0x3 刷新网络
按照新的配置设置网络
netplan apply
刷新ip地址
- 在某些情况下需要重新获取 ip地址
- 建议配合 随机mac地址使用
dhclient
- 这里linux内置的方案,但某些接精简的系统上可能没有
- 路由器没有做 ip绑定的话一般情况下获得是同一个ip
# 首先 强制释放 eth0的ip地址
dhclient -r eth0
# 重新申请 ip地址
dhclient eth0
# 为所有网卡申请新的 ip地址
# dhclient
ifconfig
dhclient
获取到的ip由路由器决定- 这里可以 使用 随机mac地址,强制路由器分配新的ip
# 随机生成mac地址
# mac地址有一些固定前缀,代表某些厂商和用途
# 完全随机生成的话会被识破
mac_tag="88:d7:f6:"
mac_add=$(date +%s|md5sum | sed 's/\(..\)/&:/g' | cut -c1-8)
new_mac="${mac_tag}${mac_add}"
# 需要修改的mac地址
echo "gen new mac: ${new_mac}"
nic=eth0
ifconfig $nic down
# sleep 3
ifconfig $nic hw ether "$new_mac"
# ifconfig $nic hw ether "88:D7:F6:25:d2:e2"
ifconfig $nic up
Configuring a network card
ubuntu17.10
starts usingnetplan
to configure the network- The legacy
ifupdown
has been deprecated, including the companion configuration file/etc/network/interface
ubuntu 18.04 lts
default configuration file is/etc/netplan/50-cloud-init.yaml
0x1 Enable netplan
- The configuration folder for
netplan
is/etc/netplan
, which contains the configuration for all network cards ubuntu 18.04 lts
starts with a built-incloud-int
service, which will automatically modify the network configuration ofnetplan
.
Following the instructions in the configuration file /etc/netplan/50-cloud-init.yaml
, you can disable the effect of cloud-int
on the network by using the following command
echo "network: {config: disabled}" >/etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
0x2 configure netplan
- the configuration file of
netplan
is inyaml
format, which distinguishes the configuration fast according to the indentation yaml
is thekey: value
format to specify the configuration, but the symbol:
must be followed by space and thenvalue
.
0x2.1 Configuring dhcp
- This is the
netplan
configuration file/etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by the datasource. changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
enp2s0:
dhcp4: yes
dhcp6: no
optional: true
enp4s0:
dhcp4: yes
dhcp6: no
optional: true
version: 2
optional: true
means: this NIC is optional, if the configuration fails at boot time, just skip itenp2s0
andenp4s0
are two NICs (ip a
to see all NICs)
0x2.2 configured as static ip
# This file is generated from information provided by the datasource. changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
enp2s0:
dhcp4: no
dhcp6: no
addresses: [192.168.123.31/24]
gateway4: 192.168.123.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
enp4s0:
dhcp4: yes
dhcp6: no
optional: true
version: 2
0x3 Refresh the network
Set up the network according to the new configuration
netplan apply
Refresh ip address
- In some cases it is necessary to reacquire the ip address
- recommended for use with random mac addresses
dhclient
- This is a built-in solution for Linux, but may not be available on some lite systems.
- if the router does not do ip binding, it usually gets the same ip
# First, force the release of eth0 ip address
dhclient -r eth0
# reapply ip address
dhclient eth0
# Apply new ip addresses for all NICs
# dhclient
ifconfig
- the ip obtained by
dhclient
is determined by the router
Here you can use random mac addresses to force the router to assign a new ip
# random mac address generation
# mac addresses have some fixed prefixes, representing certain vendors and uses
# Totally random generation will be detected
mac_tag="88:d7:f6:"
mac_add=$(date +%s|md5sum | sed 's/\(... \)/&:/g' | cut -c1-8)
new_mac="${mac_tag}${mac_add}"
# The mac address to be modified
echo "gen new mac: ${new_mac}"
nic=eth0
ifconfig $nic down
# sleep 3
ifconfig $nic hw ether "$new_mac"
# ifconfig $nic hw ether "88:D7:F6:25:d2:e2"
ifconfig $nic up