三大 Linux 发行版网络配置

Linux 是一种自由和开放原始码的类UNIX操作系统,Linux 发行版指的就是通常所说的「Linux操作系统」,它一般是由一些组织、团体、公司或者个人制作并发行的。目前,超过三百个发行版被积极的开发,最普遍被使用的发行版有大约十多个。较为知名的有 Debian、Ubuntu、Fedora、CentOS、Arch Linux 和 openSUSE等。
根据「身边即世界」,各大 Linux 发行版使用人数较多的是 Debian、Ubuntu 和 CentOS。由此,整理记录一下它们的网络配置。

Debian

debian的网络配置文件是 /etc/network/interfaces

下面是一个eth0接口配置示例

手动配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
auto eth0
iface eth0 inet static
address x.x.x.x
netmask x.x.x.x
gateway x.x.x.x
dns-nameservers x.x.x.x

auto eth0
iface eth0 inet6 static
address your_ip
netmask 64
gateway ipv6_geteway
dns-nameservers ipv6 dns

使用DHCP自动配置

1
2
3
4
5
6
7
auto eth0
allow-hotplug eth0
iface eth0 inet dhcp

auto eth0
allow-hotplug eth0
iface eth0 inet6 dhcp

使用 systemctl restart networking 即可重启网络

编辑并保存文件后,请检查配置文件的语法,因为语法错误可能会中断与 服务器的连接并使您无法访问。

Ubuntu

ubuntu 16.04

ubuntu 16.04的网络配置文件为 /etc/network/interfaces.d/50-cloud-init.cfg
语法跟 Debian 相同

ubuntu 18.04 & 20.04

在 Ubuntu 18.04 和 20.04 上,一般云镜像网络配置文件为 /etc/netplan/50-cloud-init.yaml

下面是一个eth0接口配置示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
network:
version: 2
ethernets:
eth0:
addresses:
- ipv4_address/24
- ipv6_address/64
gateway4: ipv4_gateway
gateway6: ipv6_gateway
match:
macaddress: 5e:5x:5a:5m:5p:5e
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
- 2001:4860:4860::8844
- 2001:4860:4860::8888
search: []
set-name: eth0

使用 netplan --debug apply 命令可检查应用配置,然后重启服务器即可。

CentOS

在 CentOS 上,网络配置文件一般为 /etc/sysconfig/network-scripts/ifcfg-eth0

下面是一个eth0接口的配置示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6ADDR=<IPv6地址>/<子网前缀长度>
IPV6_DEFAULTGW=<IPv6网关>
IPV6_AUTOCONF=no
IPV6_DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=eth0
UUID=...
ONBOOT=yes
HWADDR=5e:5x:5a:5m:5p:5e
IPADDR=x.x.x.x
NETMASK=x.x.x.x
GATEWAY=x.x.x.x
DNS1=8.8.8.8
DNS2=8.8.4.4
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes

使用 systemctl restart network 命令即可重启网络服务