如何在 Ubuntu 服务器 22.04 上设置静态 IP 地址
IP
![](/data/attachment/album/202210/27/091312aohaix6g6kay68xa.jpg)



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


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


**准备条件**


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


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


注意：我们不能使用 [nmcli 程序](https://www.linuxtechi.com/configure-ip-with-nmcli-command-linux/)，因为它不是 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](/data/attachment/album/202210/27/091315b0qqrl88jqqrih4j.png)


根据上面的输出，它说我们有 `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](/data/attachment/album/202210/27/091315agwwzr7raj0czbod.png)


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


* `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](/data/attachment/album/202210/27/091315xav8yqdndquhqyx5.png)


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


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




---


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


作者：[Pradeep Kumar](https://www.linuxtechi.com/author/pradeep/) 选题：[lkxed](https://github.com/lkxed) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
In this post, we will cover how to set static ip address on Ubuntu server 22.04.

It is highly recommended to have a static ip on linux server because it would be persistent across the reboot. Static IP plays an important role for servers like Mail Server, Web Server and File server etc.

#### Prerequisites

- Minimal Installed Ubuntu Server 22.04
- Regular User with sudo admin rights

In Ubuntu server 22.04, networking is controlled by netplan utility, so we will use netplan to configure static ip address on Ubuntu server.

Note: we cannot use [nmcli utiltity](https://www.linuxtechi.com/configure-ip-with-nmcli-command-linux/) as it is not the part of default installation on Ubuntu server.

## Setting up Static IP address on Ubuntu Server 22.04

Login to your Ubuntu server 22.04, look for the netplan configuration file. It is located under /etc/netplan directory.

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

Run below [cat command](https://www.linuxtechi.com/cat-command-examples-for-beginners-in-linux/) to view the contents of ‘00-installer-config.yaml’

Note: Name of configuration file may differ as your per setup. As it is an yaml file, so make sure to maintain the indentation and syntax while editing.

$ cat 00-installer-config.yaml

Output,

As per above output, it says that we have ens33 interface and it is getting ip from dhcp server. Alternate way to view interface name is via ip command.

Now, to configure static ip in place of dhcp, edit netplan configuration file using vi or nano editor and add the following content.

$ 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

save and close the file.

In the above file we have used following,

- ens33 is the interface name
- addresses are used to set the static ip
- nameservers used to specify the DNS server ips
- routes used to specify the default gateway

Note: Change the IP details and interface name as per your environment.

To make above changes into the effect the apply these changes using following netplan command,

$ sudo netplan apply

Run following [ip command](https://www.linuxtechi.com/ip-command-examples-for-linux-users/) to view the ip address on interface,

$ ip addr show ens33

To view the default route, run

$ ip route show

Output of above commands,

Perfect, above commands’ output confirms that static ip and route has been configured successfully.

That’s all from this post. Kindly do post your queries and feedback in below comments section.

Gerald BrownHow do I use Netplan to set up a static IP address on WIFI?

Aaronchange the network device “ens33” to something else, it will be listed when you run “ip a”; it;s probably wlan0 but that’s not guaranteed.

ronssmthere will be a config file for the wifi interface. look for something like 00-installer-config-wifi.yaml

David PinosHey! thanks I had problems before setting up the DNS and none config would work!

This one did and you made this post really simple to follow!

Surf40Your text for configuring a static IP address does not work in my Ubuntu 20.04.5 server’s NIC. No matter how I space or tab the indentations, I get “Ivalid YAML: inconsistent indentation: addresses:

I’ve been at the problem for a couple of weeks, with no fix in site; no spacing or tabbing change I make fixes it. Can anyone please advise me? Thanks.

choctry paste the YAML into here ‘https://www.yamllint.com/’

VietCopy paste not work here, you should try typing instead or if you paste, try to delete all the space before each line and tab key until the same format

MikaelThe spacing must be done with the space key. If you try to make spaces in a yaml file with the tab key it will not work.

Also you should let yamllint.com correct the file for you

sylvaYour article is quite nice and clear! but after followed, following error occurred when ping google.com:

“temporary failure in name resolution”, meanwhile localhost can be visited. Is anyone facing this issue as well? I’ll quite appreciate it if can get some advise.

ganeshcan we use default DHCP ip configuration along with another static ip in ubuntu 22.04 ?

i already have ens33 then i added eth0 as static ip , netplan apply did not thrown any errors but unable to see my static ip , when i do ifconfig 🙁

even after reboot its not applying, any suggestions..

Wiilliami successfully set up my static ip but I cant ping to 192.168.1.1 why