Linux基础知识-在Ubuntu上设置一个静态IP

本教程介绍如何从命令行在Ubuntu系统上设置静态IP。

它涵盖了所有最新版本的Ubuntu的网络配置,包括配置静态IP地址、设置主机名和配置名称解析的说明。

使用Netplan配置DHCP地址

下面是从DHCP服务器获取IPv4和IPv6的网络配置的配置。

# This file describes the network interfaces available on your system

# For more information, see netplan(5).

network:

 version: 2

 renderer: networkd

 ethernets:

 ens33:

   dhcp4: yes

   dhcp6: yes

要应用更改,请运行:

sudo netplan apply

在Ubuntu上用Netplan配置一个静态IP地址

这里是使用Netplan配置静态IP地址的步骤。Netplan配置文件位于目录 /etc/Netplan/。默认配置文件是 /etc/netplan/01-netcfg.yaml.

打开网络配置文件。netplan配置文件名不同,这取决于Ubuntu版本。

Ubuntu 20.04

sudo nano /etc/netplan/00-installer-config.yaml

Ubuntu 18.04

sudo nano /etc/netplan/01-netcfg.yaml

配置语法是Python编程语言(.yaml格式),所以行的缩进非常重要!

文件的内容在ubuntu20.04和18.04上是相同的。

下面是静态IPv4地址 192.168.1.100的示例。
它在第一个网络接口 ens33和网关IP 192.168.1.1
服务器将使用免费的Google DNS服务器 8.8.8.88.8.4.4进行名称解析。

# This file describes the network interfaces available on your system

# For more information, see netplan(5).

network:

version: 2

renderer: networkd

ethernets:

  ens33:

    dhcp4: no

    dhcp6: no

    addresses: [192.168.1.100/24]

    gateway4: 192.168.1.1

    nameservers:

      addresses: [8.8.8.8,8.8.4.4]

可以在 addresses地址行中添加IPv6地址,用逗号分隔。例子:

# This file describes the network interfaces available on your system

# For more information, see netplan(5).

network:

version: 2

renderer: networkd

ethernets:

  ens33:

    dhcp4: no

    dhcp6: no

    addresses: [192.168.1.100/24, '2001:1::1/64']

    gateway4: 192.168.1.1

    nameservers:

      addresses: [8.8.8.8,8.8.4.4]

把IPv6地址用单引号括起来很重要,否则会出现语法错误。

要使更改生效,请运行:

sudo netplan apply

或者在netplan配置文件解析成功的情况下,将它与--debug开关一起使用,以获得一些有用的输出。

sudo netplan --debug apply

ubuntu12.04-17.04(包括ubuntu16.04 LTS)上的网络配置

Ubuntu18.04和Ubuntu18.04网络配置

Ubuntu17.10已经完全改变了网络配置。Canonical(开发UbuntuLinux发行版的公司)推出了一个名为 Netplan的新工具,用于网络设置管理,从17.10版开始,所有新的Ubuntu版本都会用到这个工具。Netplan也用于最新版本的ubuntu20.04。

以前的网络配置文件 /etc/network/interfaces不再使用。

步骤1:配置网络接口

在此步骤中,我们将使用首选文本编辑器(nano gedit vi)编辑以下文件,手动配置网络接口。在这个例子中,我使用的是“nano”编辑器。我们可以通过在终端中输入以下命令来编辑相应的文件:

我们可以直接从这一行复制和粘贴。

sudo nano /etc/network/interfaces

输入root密码,一旦我们首选的编辑器打开文件,我们可以在旧版的Ubuntu上看到:

auto lo eth0
iface lo inet loopback
iface eth0 inet dynamic

带有systemd的Ubuntu系统(比如Ubuntu 16.04及更高版本),网络接口被命名为ens33而不是eth0,单词“dynamic”被替换为“dhcp”。

DHCP自动分配IP地址get的配置如下所示:

# 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 ens33

iface ens33 inet dhcp

静态配置的网卡在旧版的Ubuntu上会有这样一个部分:

auto lo eth0
iface lo inet loopback
iface eth0 inet static
        address xxx.xxx.xxx.xxx(enter your ip here)
        netmask xxx.xxx.xxx.xxx
        gateway xxx.xxx.xxx.xxx(enter gateway ip here,usually the address of the router)

下面是一个旧版Ubuntu的示例:

auto lo eth0
iface lo inet loopback
iface eth0 inet static
	address 192.168.1.100
	netmask 255.255.255.0
	gateway 192.168.1.1

下面是Ubuntu 16.04及更高版本的示例:

# 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

# test

# The primary network interface

auto ens33

iface ens33 inet static

 address 192.168.1.100

 netmask 255.255.255.0

 network 192.168.1.0

 broadcast 192.168.1.255

 gateway 192.168.1.1

 dns-nameservers 8.8.8.8 8.8.4.4

这里是来自ubuntu16.04系统的完整网络配置文件。

如果使用“nano”编辑器编辑配置文件,请键入Ctrl+x保存更改。

如果是Ubuntu版本14.04和16.04

再次打开/etc/network/interfaces文件,并在网关行之后添加一行dns nameservers 8.8.8.8。

sudo nano /etc/network/interfaces
auto lo
iface lo inet loopback
iface ens33 inet static
        address xxx.xxx.xxx.xxx(enter your ip here)
        netmask xxx.xxx.xxx.xxx
        gateway xxx.xxx.xxx.xxx(enter gateway ip here,usually the address of the router)
        dns-nameservers 8.8.8.8

Google提供了名称服务器8.8.8.8和8.8.4.4供公众使用,因此我们可以在网络配置中使用它们。

如果使用“nano”编辑器,请键入Ctrl+x保存更改。

执行

ifconfig

如果一切都是正确的,你就会得到这个结果。

eth0      Link encap:Ethernet  direcciónHW 00:33:27:46:2v:34

          Direc. inet:192.168.1.101  Difus.:0.0.0.0  Másc:255.255.255.0

 ...  ****

对于Ubuntu<14.04,使用networking init脚本:

sudo /etc/init.d/networking restart

这将返回如下结果:

*Reconfiguring network interfaces… [OK]

更多Netplan配置选项

Netplan是一个复杂的新配置系统,用于配置ubuntu18.04中的网卡、虚拟设备、vlan和网桥。

配置主机名

Ubuntu服务器或桌面的主机名配置在文件 /etc/hostname/etc/hosts中。 /etc/hostname文件设置实际的系统主机名,而 /etc/hosts用于本地名称解析。

在本例中,我将把系统的主机名改为 jack.example.com.

首先,编辑/etc/hostname文件

sudo nano /etc/hostname

主机名文件只包含主机名的本地部分。这里的本地部分是 "jack" 。将/etc/hostname文件的内容更改为:

jack

保存文件。

然后用编辑器打开 /etc/hosts文件:

sudo nano /etc/hosts

然后更改以系统IP地址开头的行,如下所示:

192.168.1.100   jack.example.com     jack

格式如下:

[IP Addesss][完整主机名,包括域] [主机名的本地部分]

最后,重新启动系统以使主机名更改生效。

日期:2019-04-29 03:18:23 来源:oir作者:oir