4

ubuntu 18及以上版本配置IP的方法,你get了吗 - 伍工

 2 years ago
source link: https://www.cnblogs.com/wucode/p/16216709.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

本文讲讲 Ubuntu 18 及以上版本配置 IP 的方法,为什么它值得一讲,因为以 Ubuntu 16 为首的版本的配置方法已经不适用了,如果你还不知道,那本文正好 get 一个新技能。

Ubuntu 18 之后版本配置方法

需要使用 netplan 工具。

对应配置文件:

/etc/netplan/50-cloud-init.yaml,有的不一定是 50-cloud-init.yaml这个文件,但都大同小异。修改如下:

# vim /etc/netplan/50-cloud-init.yaml
network:
    version: 2
    renderer: networkd
    ethernets:
       enp10s0:
       dhcp4: no
       addresses: [192.168.1.3/24]
       gateway4: 192.168.1.1
       nameservers:
          addresses: [114.114.114.114,8.8.8.8]

其中,可以配置 IP 、网关、DNS 等。

配完需要使用 netplan apply 生效,也可以使用 netplan try 检查配置文件是否有误。

但是,以前的方法也不是完全就废弃了,如果你还想使用以前的方法,需要安装 ifupdown 工具。

apt install ifupdown

顺带把 Ubuntu 18 之前版本的配置方法列一下,方便大家阅读。

Ubuntu 18 之前版本配置方法

配置文件:

# vi /etc/network/interfaces

配置静态 IP 地址:

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.3.90
netmask 255.255.255.0
gateway 192.168.3.1
#network 192.168.3.0
#broadcast 192.168.3.255
#dns-nameservers 114.114.114.114,8.8.8.8

其中,也可以配 IP、网关、DNS 等。

以 dhcp 方式配置:

# The primary network interface - use DHCP to find our address
auto eth0
iface eth0 inet dhcp

最后启动网络使之生效:

/etc/init.d/networking/ restart

另外,DNS 也可以修改配置文件:

/etc/resolv.conf

nameserver 8.8.8.8
nameserver 114.114.114.114

注意:文章说的都是永久生效方式,临时生效就是用ifconfigip addr命令配置即可。

OK,今天的文章不用 5 分钟,相信大家已经 get 了两个新技能。我们下期再见。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK