Linux-Networking

nmcli 在沒有 DHCP 的情況下設置靜態 IP 地址?

  • March 12, 2020

我想為 eth0 介面分配一個靜態 IP 地址,而不需要 DHCP 分配一個動態地址。我嘗試將方法設置為手動,但這並沒有幫助,我最終在介面上獲得了 2 個 IP,一個是靜態的,另一個是動態的。我希望使用 nmcli 在介面上只有一個靜態 IP

/etc/NetworkManager/system-connections/static.nmconnection:

[connection]
id=static
uuid=5da74c14-d9da-4e15-90c9-5f37913d5610
type=ethernet
interface-name=eth0
permissions=
timestamp=1584007539

[ethernet]
mac-address-blacklist=

[ipv4]
address1=172.17.1.222/24,172.17.0.1
dns=172.17.0.221;172.17.0.220;
dns-search=
method=manual

[ipv6]
addr-gen-mode=stable-privacy
dns-search=
method=ignore

ip一個:

eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether dc:a6:32:3b:22:03 brd ff:ff:ff:ff:ff:ff
inet 172.17.1.222/24 brd 172.17.1.255 scope global noprefixroute eth0
  valid_lft forever preferred_lft forever
inet 172.17.12.14/16 brd 172.17.255.255 scope global dynamic noprefixroute eth0
  valid_lft 602857sec preferred_lft 527257sec
inet6 fe80::dea6:32ff:fe3b:2203/64 scope link 
  valid_lft forever preferred_lft forever

要配置設置靜態 IP 配置並使用 禁用 DHCP nmcli,請嘗試以 root 身份執行:

nmcli con mod eth0 ipv4.addresses "172.17.1.222/24"
nmcli con mod eth0 ipv4.gateway "172.17.0.1"
nmcli con mod eth0 ipv4.dns "172.17.0.221 172.17.0.220"
nmcli con mod eth0 ipv4.method manual
nmcli con mod eth0 connection.autoconnect yes

然後重新啟動電腦或重新network-manager啟動

systemctl restart NetworkManager

引用自:https://serverfault.com/questions/1006634