Linux 基础

您所在的位置:网站首页 Ubuntu修改固定ip地址 Linux 基础

Linux 基础

2024-07-11 17:54:09| 来源: 网络整理| 查看: 265

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.yaml

Ubuntu 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 地址] [完整的主机名,包括。域] [主机名的本地部分]

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



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭