Setting a Static IP in Linux with nmcli

  1. If it uses NetworkManager:
    cat /etc/netplan/01-network-manager-all.yaml :
    # Let NetworkManager manage all devices on this system
    network:
     version: 2
     renderer: NetworkManager
  2. Determine the Default Gateway:
    Use the ip route command to find out the default gateway of your network, which is essential for configuring your static IP.
    ip route | grep default | awk '{print $3}'
  3. Setting a Static IP Address:
    Replace "WifiName" with your actual connection name. Here’s how you can set a static IP address, gateway, DNS, and switch the method to manual:
    nmcli con mod "WifiName" ipv4.addresses 192.168.1.10/24
    nmcli con mod "WifiName" ipv4.gateway 192.168.1.1
    nmcli con mod "WifiName" ipv4.dns "8.8.8.8"
    nmcli con mod "WifiName" ipv4.method manual
  4. Applying the Changes:
    After setting the static IP, you need to restart the network connection to apply these changes:
    nmcli con down "WifiName"
    nmcli con up "WifiName"
  5. Reverting to DHCP (Dynamic IP):
    If you encounter any issues or decide to return to using DHCP, you can reset the configuration as follows:
    nmcli con mod "WifiName" ipv4.method auto
    nmcli con down "WifiName"
    nmcli con up "WifiName"

留下评论

通过 WordPress.com 设计一个这样的站点
从这里开始