當(dāng)我們在安裝 Debian Linux 時,在安裝過程中它試圖從調(diào)制解調(diào)器(或 DHCP 服務(wù)器)獲得 DHCP ip 地址(如果可用的話)。但是在您的系統(tǒng)上總是建議有一個靜態(tài) IP 地址。靜態(tài) IP 地址的主要好處是,它將在整個重新引導(dǎo)過程中保持持久。
在本指南中,我們將演示如何在 Debian 11 (Bullseye) 上設(shè)置靜態(tài) ip 地址。
在 Debian 11 中,我們可以通過 GUI (圖形用戶界面) 或命令行 (cli) 設(shè)置靜態(tài) ip 地址。
通過 GUI 設(shè)置靜態(tài) IP 地址
登錄到您的 Debian Desktop 環(huán)境, 選擇菜單 Settings –> Choose Network

點擊 Wired Settings,然后我們會看到下面的窗口

*注意:?如果要禁用 IPv6,請進入 IPv6 Tab 頁并選擇 Disable 選項
單擊 IPv4 Tab 頁

上面的窗口顯示已經(jīng)配置了自動 DHCP,因此要配置靜態(tài) IP 的話,請選擇 Manual,然后指定 IP 地址、子網(wǎng)掩碼、網(wǎng)關(guān)和 dns 服務(wù)器 IP 等詳細信息。
注意:?若要指定 dns 服務(wù)器 IP,請先關(guān)閉自動 dns 服務(wù)器 IP。

單擊 Apply 保存更改
然后,通過切換禁用和啟用接口,以便為接口分配新的 IP 地址。

再次單擊 wired settings,以驗證是否分配了新的靜態(tài) ip 地址。

完美,以上確認(rèn)新的靜態(tài) IP 地址配置成功。
通過命令行設(shè)置靜態(tài) IP 地址
Open the terminal and identify the interface on which we will configure static IP address. Run below ip command
打開終端并識別網(wǎng)絡(luò)接口,執(zhí)行以下 ip 命令
$ ip add show

運行 nmcli 命令獲取連接名稱
$ nmcli connection
Once we get the connection name, run below nmcli command to assign static ipv4 address,
獲取連接名后,運行下面的 nmcli 命令分配靜態(tài) ipv4 地址
$ nmcli con mod 'eth0' ipv4.address 192.168.1.151/24
運行下面的命令設(shè)置網(wǎng)關(guān)
$ nmcli con mod 'eth0' ipv4.gateway 192.168.1.1
將配置從 DHCP 更改為手動,這樣 IP 將是靜態(tài)和持久的,運行
$ nmcli con mod 'eth0' ipv4.method manual
使用如下命令設(shè)置 DNS 服務(wù)器 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

完美,以上輸出確認(rèn)靜態(tài) IP 地址已在 eth0 接口上成功配置。
在最小安裝的 Debian 11 上設(shè)置靜態(tài) 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’.
每當(dāng)我們安裝最小的 Debian 11 時,我們只有 CLI 控制臺,并且沒有任何 nmcli 實用程序。因此,要配置靜態(tài) IP 地址,我們需要編輯 /etc/network/interfaces 文件。
查看文件的默認(rèn)內(nèi)容
$ cat /etc/network/interfaces

編輯該文件并設(shè)置靜態(tài) IP 地址,如下所示
$ sudo vi /etc/network/interfaces
刪除 allow-htplug enp0s3 行,修改 dhcp 參數(shù)為 static。下面是我的示例文件,根據(jù)您的環(huán)境更改接口名稱和 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
保存并關(guān)閉文件

要使上述更改生效,必須重新啟動網(wǎng)絡(luò)服務(wù)
$ sudo systemctl restart networking.service
執(zhí)行 IP 命令驗證 IP 地址
$ ip add show

以上就是本文的全部內(nèi)容,請在下面的評論區(qū)分享你的反饋和疑問。
版權(quán)聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶自發(fā)貢獻,該文觀點僅代表作者本人。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如發(fā)現(xiàn)本站有涉嫌抄襲侵權(quán)/違法違規(guī)的內(nèi)容, 請發(fā)送郵件至2705686032@qq.com 舉報,一經(jīng)查實,本站將立刻刪除。文章鏈接:http://m.z1146.cn/zx/yunwei/4204.html