❏ 站外平台:

如何在 Ubuntu 服务器 22.04 上设置静态 IP 地址

作者: Pradeep Kumar 译者: LCTT geekpi

| 2022-10-27 09:13   评论: 13 收藏: 1    

在这篇文章中,我们将介绍如何在 Ubuntu 服务器 22.04 上设置静态 IP 地址。

强烈建议在 Linux 服务器上使用静态 IP,因为它会在重启后保持不变。静态 IP 对邮件服务器、Web 服务器和文件服务器等服务器起着重要作用。

准备条件

  • 最小安装的 Ubuntu 服务器 22.04
  • 具有 sudo 管理员权限的普通用户

在 Ubuntu 服务器 22.04 中,网络由 netplan 程序控制,因此我们将使用 netplan 在 Ubuntu 服务器上配置静态 IP 地址。

注意:我们不能使用 nmcli 程序,因为它不是 Ubuntu 服务器上默认安装的一部分。

在 Ubuntu 服务器 22.04 上设置静态 IP 地址

登录到你的 Ubuntu 服务器 22.04,查找 netplan 配置文件。它位于 /etc/netplan 目录下。

$ cd /etc/netplan/
$ ls -l
total 4
-rw-r--r-- 1 root root 116 Oct 12 04:03 00-installer-config.yaml
$

运行以下 cat 命令以查看 00-installer-config.yaml 的内容。

注意:配置文件的名称可能因你的设置而异。由于它是一个 yaml 文件,因此请确保在编辑时保持缩进和语法。

$ cat 00-installer-config.yaml

输出:

Default-Content-netplan-ubuntu-server

根据上面的输出,它说我们有 ens33 接口,它正在从 DHCP 服务器获取 IP。查看接口名称的另一种方法是通过 ip 命令。

现在,要配置静态 IP 代替 DHCP,使用 vi 或 nano 编辑器编辑 netplan 配置文件并添加以下内容。

$ sudo vi 00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
  renderer: networkd
  ethernets:
    ens33:
      addresses:
        - 192.168.1.247/24
      nameservers:
        addresses: [4.2.2.2, 8.8.8.8]
      routes:
        - to: default
          via: 192.168.1.1
  version: 2

保存并关闭文件。

Updated-Netplan-Config-File-Content-Ubuntu-Server

在上面的文件中,我们使用了以下内容,

  • ens33 为接口名称
  • 用于设置静态 IP 的地址
  • nameservers 用于指定 DNS 服务器的 IP
  • 用于指定默认网关的路由

注意:根据你的环境更改 IP 详细信息和接口名称。

要是上述修改生效,请使用以下 netplan 命令应用这些更改:

$ sudo netplan apply

运行以下 IP 命令查看接口上的 IP 地址:

$ ip addr show ens33

要查看默认路由,请运行:

$ ip route show

上述命令的输出。

ip-addr-route-command-output-ubuntu-server

完美,以上命令的输出确认静态 IP 和路由配置成功。

这就是这篇文章的全部内容。请在下面的评论部分发表你的问题和反馈。


via: https://www.linuxtechi.com/static-ip-address-on-ubuntu-server/

作者:Pradeep Kumar 选题:lkxed 译者:geekpi 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出



最新评论

来自120.244.60.116的 Chrome 122.0|Windows 10 用户 2024-04-12 08:45 回复
我改了静态ip,但是执行netplan apply 报错


(generate:2213): GLib-WARNING **:02:20:14.465: GError set over the top of a previous GError or uninitialized memory.This indicates a bug in someone's code, You must ensure an error is Null before it's set.The overuriting error message was: /etc/netplan/00-installer-config.yaml:11:9: Error in network def inition:unicast route must include both a to and via IP
- to: default
/etc/netplan/00-installer-config.yaml:11:15: Error in network definition: invalid I
来自120.244.60.116的 Chrome 122.0|Windows 10 用户 2024-04-12 08:45 回复
我改了静态ip,但是执行netplan apply 报错


(generate:2213): GLib-WARNING **:02:20:14.465: GError set over the top of a previous GError or uninitialized memory.This indicates a bug in someone's code, You must ensure an error is Null before it's set.The overuriting error message was: /etc/netplan/00-installer-config.yaml:11:9: Error in network def inition:unicast route must include both a to and via IP
- to: default
/etc/netplan/00-installer-config.yaml:11:15: Error in network definition: invalid I
来自120.244.60.116的 Chrome 122.0|Windows 10 用户 2024-04-12 08:45 回复
我改了静态ip,但是执行netplan apply 报错


(generate:2213): GLib-WARNING **:02:20:14.465: GError set over the top of a previous GError or uninitialized memory.This indicates a bug in someone's code, You must ensure an error is Null before it's set.The overuriting error message was: /etc/netplan/00-installer-config.yaml:11:9: Error in network def inition:unicast route must include both a to and via IP
- to: default
/etc/netplan/00-installer-config.yaml:11:15: Error in network definition: invalid I
来自120.244.60.116的 Chrome 122.0|Windows 10 用户 2024-04-12 08:45 回复
我改了静态ip,但是执行netplan apply 报错


(generate:2213): GLib-WARNING **:02:20:14.465: GError set over the top of a previous GError or uninitialized memory.This indicates a bug in someone's code, You must ensure an error is Null before it's set.The overuriting error message was: /etc/netplan/00-installer-config.yaml:11:9: Error in network def inition:unicast route must include both a to and via IP
- to: default
/etc/netplan/00-installer-config.yaml:11:15: Error in network definition: invalid I
来自120.244.60.116的 Chrome 122.0|Windows 10 用户 2024-04-12 08:45 回复
我改了静态ip,但是执行netplan apply 报错


(generate:2213): GLib-WARNING **:02:20:14.465: GError set over the top of a previous GError or uninitialized memory.This indicates a bug in someone's code, You must ensure an error is Null before it's set.The overuriting error message was: /etc/netplan/00-installer-config.yaml:11:9: Error in network def inition:unicast route must include both a to and via IP
- to: default
/etc/netplan/00-installer-config.yaml:11:15: Error in network definition: invalid I
来自120.244.60.116的 Chrome 122.0|Windows 10 用户 2024-04-12 08:45 回复
我改了静态ip,但是执行netplan apply 报错


(generate:2213): GLib-WARNING **:02:20:14.465: GError set over the top of a previous GError or uninitialized memory.This indicates a bug in someone's code, You must ensure an error is Null before it's set.The overuriting error message was: /etc/netplan/00-installer-config.yaml:11:9: Error in network def inition:unicast route must include both a to and via IP
- to: default
/etc/netplan/00-installer-config.yaml:11:15: Error in network definition: invalid I
来自上海的 Chrome 120.0|Windows 10 用户 2023-12-10 22:07 2 回复
非常感谢
来自上海的 Chrome 120.0|Windows 10 用户 2023-12-10 22:07 2 回复
非常感谢
[1]
来自北京的 Chrome 115.0|Mac 10.15 用户 发表于 2023-07-23 19:38 的评论:
如何在没有插入网线,或者网线被拔掉后,静态IP地址也存在呢
来自上海的 Chrome 115.0|Windows 10 用户 2023-09-22 13:58 2 回复
这是一个网络问题。静态ip的存在,在于路由器的分配,不只是终端是否设置。你可以做双向绑定的操作。即在路由器上设置ip和mac的绑定,同时在对应的终端上做静态ip的设置。这时,不管终端是否有网线或网络驱动,路由器都不会把绑定的ip分给其它的终端使用。
[1]
来自北京的 Chrome 108.0|Windows 10 用户 发表于 2022-12-28 18:12 的评论:
您好,请问有没有可以用命令行更改为固定ip的方法?
来自上海的 Chrome 115.0|Windows 10 用户 2023-09-22 13:54 2 回复
nmtui 命令行下的gui配置IP
[1]
来自北京的 Chrome 108.0|Windows 10 用户 发表于 2022-12-28 18:12 的评论:
您好,请问有没有可以用命令行更改为固定ip的方法?
来自北京的 Chrome 115.0|Mac 10.15 用户 2023-07-23 19:39 3 回复
nmcli 命令可以
LCTT 译者
geekpi 💎💎💎💎
共计翻译: 2095.5 篇 | 共计贡献: 3750
贡献时间:2013-10-25 -> 2024-01-31
访问我的 LCTT 主页 | 在 GitHub 上关注我


返回顶部

分享到微信

打开微信,点击顶部的“╋”,
使用“扫一扫”将网页分享至微信。