debian11设置静态ip地址

2023-08-19 16:29 综合百科 0阅读 投稿:小七

当我们在安装 Debian Linux 时,在安装过程中它试图从调制解调器(或 DHCP 服务器)获得 DHCP ip 地址(如果可用的话)。但是在您的系统上总是建议有一个静态 IP 地址。静态 IP 地址的主要好处是,它将在整个重新引导过程中保持持久。

在本指南中,我们将演示如何在 Debian 11 (Bullseye) 上设置静态 ip 地址。

在 Debian 11 中,我们可以通过 GUI (图形用户界面) 或命令行 (cli) 设置静态 ip 地址。

通过 GUI 设置静态 IP 地址

登录到您的 Debian Desktop 环境, 选择菜单 Settings –> Choose Network

debian11设置静态ip地址图1

点击 Wired Settings,然后我们会看到下面的窗口

debian11设置静态ip地址图2

*注意: 如果要禁用 IPv6,请进入 IPv6 Tab 页并选择 Disable 选项

单击 IPv4 Tab 页

debian11设置静态ip地址图3

上面的窗口显示已经配置了自动 DHCP,因此要配置静态 IP 的话,请选择 Manual,然后指定 IP 地址、子网掩码、网关和 dns 服务器 IP 等详细信息。

注意: 若要指定 dns 服务器 IP,请先关闭自动 dns 服务器 IP。

debian11设置静态ip地址图4

单击 Apply 保存更改

然后,通过切换禁用和启用接口,以便为接口分配新的 IP 地址。

debian11设置静态ip地址图5

再次单击 wired settings,以验证是否分配了新的静态 ip 地址。

debian11设置静态ip地址图6

完美,以上确认新的静态 IP 地址配置成功。

通过命令行设置静态 IP 地址

Open the terminal and identify the interface on which we will configure static IP address. Run below ip command

打开终端并识别网络接口,执行以下 ip 命令

$ ip add show
debian11设置静态ip地址图7

运行 nmcli 命令获取连接名称

$ nmcli connection

Once we get the connection name, run below nmcli command to assign static ipv4 address,

获取连接名后,运行下面的 nmcli 命令分配静态 ipv4 地址

$ nmcli con mod 'eth0' ipv4.address 192.168.1.151/24

运行下面的命令设置网关

$ nmcli con mod 'eth0' ipv4.gateway 192.168.1.1

将配置从 DHCP 更改为手动,这样 IP 将是静态和持久的,运行

$ nmcli con mod 'eth0' ipv4.method manual

使用如下命令设置 DNS 服务器 IP 地址

$ nmcli con mod 'eth0' ipv4.dns '8.8.8.8'

禁用并启用连接,以便上述更改生效。

$ nmcli connection down eth0$ nmcli connection up eth0

运行 IP 命令查看 IP 地址

$ ip add show eth0
debian11设置静态ip地址图8

完美,以上输出确认静态 IP 地址已在 eth0 接口上成功配置。

在最小安装的 Debian 11 上设置静态 IP 地址

Whenever we install minimal Debian 11 then we will have only the CLI console and don’t have any nmcli utility. So, to configure static ip address we will edit the file ‘/etc/network/interfaces’.

每当我们安装最小的 Debian 11 时,我们只有 CLI 控制台,并且没有任何 nmcli 实用程序。因此,要配置静态 IP 地址,我们需要编辑 /etc/network/interfaces 文件。

查看文件的默认内容

$ cat /etc/network/interfaces
debian11设置静态ip地址图9
$ sudo vi /etc/network/interfaces

删除 allow-htplug enp0s3 行,修改 dhcp 参数为 static。下面是我的示例文件,根据您的环境更改接口名称和 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 enp0s3iface enp0s3 inet static address 192.168.1.183 netmask 255.255.255.0 gateway 192.168.1.1 dns-nameservers 8.8.8.8

保存并关闭文件

debian11设置静态ip地址图10

要使上述更改生效,必须重新启动网络服务

$ sudo systemctl restart networking.service

执行 IP 命令验证 IP 地址

$ ip add show
debian11设置静态ip地址图11

我的开源项目

debian11设置静态ip地址图12

酷瓜云课堂 - 开源在线教育解决方案

  • course-tencent-cloud(酷瓜云课堂 - gitee 仓库)
  • course-tencent-cloud(酷瓜云课堂 - github 仓库)
声明:若水百科所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系youzivr@vip.qq.com