Linux 基础 |
您所在的位置:网站首页 › Ubuntu修改固定ip地址 › Linux 基础 |
Linux 基础 - 在 Ubuntu 上设置静态 IP
本教程介绍如何从命令行在 Ubuntu 系统上设置静态 IP 地址。它涵盖了所有当前版本的 Ubuntu 的网络配置,包括配置静态 IP 地址、设置主机名和配置名称解析的说明。 Ubuntu 20.04 LTS 和 Ubuntu 18.04 LTS 上的网络配置Ubuntu 中的网络配置是使用名为 netplan 的工具进行的,它取代了传统的 /etc/network/interfaces 文件。 在 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.yamlUbuntu 18.04: sudo nano /etc/netplan/01-netcfg.yaml配置语法是Python编程语言(.yaml格式),所以行的缩进很重要! 该文件的内容在 Ubuntu 20.04 和 18.04 上是相同的。 这是第一个网络接口 ens33 和网关 IP 192.168.1.1 上的静态 IPv4 地址 192.168.1.100 示例。服务器将使用免费的 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 服务器的屏幕截图: 可以在 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使用 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,动态一词已被 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 interfaceauto loiface lo inet loopback # The primary network interfaceauto ens33iface 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 interfaceauto loiface lo inet loopback # test # The primary network interfaceauto ens33iface 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 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 文件并在 gateway 行之后添加一行 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此时可以检查设置是否正确: 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 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |