# ubuntu16

# 1.修改静态ip

$sudo vi /etc/network/interfaces

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto ens32
iface ens32 inet static
address 192.168.4.1
netmask 255.255.0.0
gateway 192.168.1.1
dns-nameservers 61.134.1.4

# 2.路由地址

$sudo vi /etc/resolvconf/resolv.conf.d/base

nameserver 218.30.19.40
nameserver 61.1.34.1.4

用户可以通过以下命令查看 /etc/resolvconf/resolv.conf.d/base 的值是否被刷到 /etc/resolv.conf 上

resolvconf -u

# 3.修改ip后不生效(以root用户运行)

ip addr flush dev ens32
ifdown ens32
ifup ens32

如果运行(ip addr flush dev ens33)后,ifdown ens32 错误,运行

sudo /etc/init.d/networking restart

# 4.查看网卡硬件信息

lshw -C network

lshw列出硬件(hardware)信息.其中logical name为网卡的名称

# ubuntu18及以上

# 1. 适用系统

本文介绍的静态 ip 的配置方法适用于 Ubuntu 系统的 18.04 及其以上版本。

# 2. 确认以太网连接的网络接口

一般情况下的个人 PC 只会有一张网卡,但在服务器中可能存在多张网卡的情况,使用 ifconfig 命令查看对应 ip 的网络接口。 若提示未找到 ifconfig 命令则使用如下命令按装该工具。

sudo apt-get install net-tools

查看需要修改的网卡,如下可以查到当前所有的网络接口信息,由于我的主机只有一块网卡,因此就只对它修改即可。

#ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.253.129  netmask 255.255.255.0  broadcast 192.168.253.255
        inet6 fe80::bbd9:a68d:6982:fa33  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:b8:7a:f4  txqueuelen 1000  (Ethernet)
        RX packets 11433  bytes 15280828 (15.2 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5919  bytes 391957 (391.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 911  bytes 68550 (68.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 911  bytes 68550 (68.5 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# 3. 修改网卡默认配置文件

其默认配置信息的路径如下。

/etc/netplan/01-network-manager-all.yaml

使用vim修改该文件

$ sudo vim /etc/netplan/01-network-manager-all.yaml

若为修改过的文件应该会是这样:

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager

将其修改为静态 ip 的模式。

# 3.1. ubuntu20.04 / 18.04 参考以下配置

network:
    ethernets:
        ens33:
            dhcp4: no
            addresses: [192.168.1.10/24]
            optional: true
            gateway4: 192.168.1.1
            nameservers:
                    addresses: [114.114.114.114,8.8.8.8]

    version: 2

应用该配置

$ sudo netplan apply

# 3.2. ubuntu22.04 参考以下配置

network:
	ethernets:
		ens32:
			dhcp4: no
			dhcp6: no
			addresses:
				- 192.168.0.10/24
			routes:
				- to: default
				  via: 192.168.0.1
			nameservers:
				addresses:
					- 114.114.114.114
					- 8.8.8.8
	version: 2
	renderer: networkd

应用该配置

$ sudo netplan apply

# 3.3. 注意

若 Ubuntu22.04 应用 ubuntu20.04 /18.04 的配置来设置静态 IP,当使用 netplan apply 则会得到如下警告。

$ sudo netplan apply

** (generate:3591): WARNING **: 11:29:00.676: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.

** (process:3589): WARNING **: 11:29:00.788: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.

# 4.公共DNS

1、百度公共DNS服务IP:180.76.76.76

2、阿里云DNS,223.6.6.6 还有一个223.5.5.5这一个PING值50多

3、腾讯DNS:119.29.29.29

4、 电信:首选:101.226.4.6

5.联通:首选:123.125.81.6

6.移动:首选:101.226.4.6

7.铁通:首选:101.226.4.6

8.114 DNS 114.114.114.114