如何在 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

Network-Settings-Debian11

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

Details-Tab-Network-Settings-Debian-Linux

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

单击 IPv4 Tab 页

Automatic-DHCP-Network-Debian-Linux

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

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

Set-Static-IPv4-GUI-Debian11

单击 Apply 保存更改

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

Disable-Enable-Interface-Toggle-GUI-Debian11

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

New-Static-IPv4-Debian11

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

IP-Command-Interface-Name-debian

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

Configure-Static-IP-with-nmcli-command-Debian11

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

Default-Interface-File-Debian

编辑该文件并设置静态 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 interface
auto lo
iface lo inet loopback

# The primary network interface
auto enp0s3
iface enp0s3 inet static
 address 192.168.1.183
 netmask 255.255.255.0
 gateway 192.168.1.1
 dns-nameservers 8.8.8.8

保存并关闭文件

Interface-file-after-modification-debian11

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

$ sudo systemctl restart networking.service

执行 IP 命令验证 IP 地址

$ ip add show

ip-address-view-debian11

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

我的开源项目

酷瓜云课堂-开源培训系统

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值