失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 如何在 Debian11系统上设置静态IP地址 ?

如何在 Debian11系统上设置静态IP地址 ?

时间:2022-03-18 20:53:58

相关推荐

如何在 Debian11系统上设置静态IP地址 ?

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

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

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

通过 GUI 设置静态 IP 地址

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

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

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

单击 IPv4 Tab 页

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

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

单击 Apply 保存更改

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

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

完美,以上确认新的静态 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

运行 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

完美,以上输出确认静态 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

编辑该文件并设置静态 IP 地址,如下所示

$ 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 staticaddress 192.168.1.183netmask 255.255.255.0gateway 192.168.1.1dns-nameservers 8.8.8.8

保存并关闭文件

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

$ sudo systemctl restart networking.service

执行 IP 命令验证 IP 地址

$ ip add show

以上就是本文的全部内容,请在下面的评论区分享你的反馈和疑问。

我的开源项目

course-tencent-cloud(酷瓜云课堂 - gitee仓库)course-tencent-cloud(酷瓜云课堂 - github仓库)

如果觉得《如何在 Debian11系统上设置静态IP地址 ?》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。