Linux 基础知识 您所在的位置:网站首页 netplan双网卡dhcp加静态ip Linux 基础知识

Linux 基础知识

2024-07-11 06:58| 来源: 网络整理| 查看: 265

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

本教程介绍如何从命令行在 Ubuntu 系统上设置静态 IP 地址。它涵盖了所有当前版本的 Ubuntu 的网络配置,并包括配置静态 IP 地址、设置主机名和配置名称解析的说明。

Ubuntu 22.04、Ubuntu 20.04 和 Ubuntu 18.04 上的网络配置

Ubuntu 中的网络配置是通过一个名为 netplan 的工具进行的。它取代了传统的 /etc/network/interfaces 文件。

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

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

使用编辑器打开网络配置文件。 Netplan 配置文件名因 Ubuntu 版本而异。

Ubuntu 22.04 和 Ubuntu 20.04:

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

Ubuntu 18.04:

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

配置语法采用Python编程语言(.yaml格式),因此行的缩进很重要!

该文件的内容在 Ubuntu 22.04 - 18.04 上是相同的。

以下是第一个网络接口 ens33 上的静态 IPv4 地址 192.168.1.100 和网关 IP 192.168.1.1 的示例。服务器将使用免费的 Google DNS 服务器 8.8.8.8 和 8.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]

或者,作为 Ubuntu 服务器的屏幕截图:

可以在地址行中添加 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使用 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更多 Netplan 配置选项

Netplan 是 Ubuntu 18.04 中一个复杂的新配置系统,用于配置网卡、虚拟设备、VLAN 和网桥。请参阅手册页以获取更多示例和语法的深入解释。

Ubuntu 12.04 - 17.04(包括 Ubuntu 16.04 LTS)上的网络配置第 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 地址的配置如下所示:

# 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

这里是 Ubuntu 16.04 系统的完整网络配置文件。

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

保存修改后的缓冲区(回答“否”会破坏更改)?

要写入的文件名:interfaces

步骤 2:配置 DNS 服务器

仅在 Ubuntu /etc/resolv.conf,对于较新的 Ubuntu 版本,名称服务器在 /etc/network/interfaces 文件或 netplan 配置文件中进行配置。

a) Ubuntu 20.04

使用编辑器打开 netplan 配置文件。我将在本例中使用 Nano 编辑器:

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

我已将 DNS 服务器 IP 地址标记为粗体:

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

b) Ubuntu 18.04

使用nano编辑器打开netplan配置文件:

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

我已将 DNS 服务器 IP 地址标记为粗体:

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

c) Ubuntu 版本 14.04 和 16.04

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

sudo nano /etc/network/interfacesauto 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

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

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

保存修改后的缓冲区(回答“否”会破坏更改)?

要写入的文件名:interfaces

d) Ubuntu 版本

在此步骤中,您将手动配置您的 dns 配置文件。

sudo nano /etc/resolv.conf

编辑器打开文件后,您需要输入以下信息...

nameserver xxx.xxx.xxx.xxx(enter your dns server ip) nameserver xxx.xxx.xxx.xxx(enter your alt dns server ip)

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

保存修改后的缓冲区(回答“否”会破坏更改)?

要写入的文件名:resolv.conf

这是一个例子:

nameserver 8.8.8.8 nameserver 8.8.4.4第 3 步:重新启动网络

使用新设置手动重新启动网络接口。

对于 Ubuntu 20.04 和 18.04,请使用 netplan 命令应用更改并重新启动网络。命令是:

sudo netplan apply

对于 Ubuntu 版本 14.04 和 16.04,我们使用 systemctl 代替:

systemctl restart ifup@eth0

此时可以检查设置是否正确:

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

sudo /etc/init.d/networking restart

这应该返回如下所示的结果:

*Reconfiguring network interfaces… [OK]配置主机名

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

在此示例中,我将系统的主机名更改为 obelix.example.com。

首先,编辑/etc/hostname文件

sudo nano /etc/hostname

主机名文件仅包含主机名的本地部分。这里的本地部分是“obelix”。将 /etc/hostname 文件的内容更改为:

obelix

并保存文件。编辑后在 Nano 中显示的主机名文件:

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

sudo nano /etc/hosts

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

192.168.1.100   obelix.example.com     obelix

这是 /etc/hosts 文件的屏幕截图。

格式是这样的:

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

最后,重新启动系统以应用主机名更改。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有