Assign a static IP address

This guide explains how to assign a static IP address. This may be helpful in scenarios such as a network with no DHCP server.

Identify which program is managing the interface

New installations of Clear Linux* OS use NetworkManager as the default network interface manager for all network connections.

Note

  • The cloud Clear Linux OS images continue to use systemd-networkd to manage network connections.
  • In earlier Clear Linux OS versions, systemd-network was used to manage Ethernet interfaces and NetworkManager was used for wireless interfaces.

Before defining a configuration for assigning a static IP address, verify which program is managing the network interface.

  1. Check the output of nmcli device to see if NetworkManager is managing the device.

    nmcli device status
    

    If the STATE column for the device shows connected or disconnected, the network configuration is being managed by NetworkManager, then use the instructions for using NetworkManager.

    If the STATE column for the device shows unmanaged, then check if the device is being managed by systemd-networkd.

  2. Check the output of networkctl list to see if systemd-networkd is managing the device.

    networkctl list
    

    If the SETUP column for the device shows configured, the network configuration is being managed by systemd-networkd, then use the instructions for using systemd-networkd.

Using NetworkManager

Network connections managed by NetworkManager are stored as files with the .nmconnection file extension in the /etc/NetworkManager/system-connections/ directory.

A few tools exists to aid to manipulate network connections managed by NetworkManager:

  • nmcli - a command-line tool
  • nmtui - a text user interface that provides a pseudo graphical menu in the terminal
  • nm-connection-editor - a graphical user interface

The method below uses the command line tool nmcli to modify network connection.

  1. Identify the existing connection name:

    nmcli connection show
    

    Sample output:

    NAME                UUID                                  TYPE            DEVICE
    Wired connection 1  00000000-0000-0000-0000-000000000000  802-3-etherneten01
    

    If a connection does not exist, create it with the nmcli connection add command.

  2. Modify the connection to use a static IP address. Replace the variables in brackets with the appropriate values. Replace [CONNECTION_NAME] with the NAME from the command above.

    sudo nmcli connection modify "[CONNECTION_NAME]" \
    ipv4.method "manual" \
    ipv4.addresses "[IP_ADDRESS]/[CIDR_NETMASK]" \
    ipv4.gateway "[GATEWAY_IP_ADDRESS]" \
    ipv4.dns "[PRIMARY_DNS_IP],[SECONDARY_DNS_IP]"
    

    See the nmcli developer page for more configuration options. For advanced configurations, the /etc/NetworkManager/system-connections/*.nmconnection. can be edited directly.

  3. Restart the NetworkManager server to reload the DNS servers:

    sudo systemctl restart NetworkManager
    
  4. Verify your static IP address details have been set:

    nmcli
    

Using systemd-networkd

Network connections managed by systemd-networkd are stored as files with the .network file extension the /etc/systemd/network/ directory.

Files to manipulate network connections managed by systemd-networkd must be created manually.

  1. Create the /etc/systemd/network directory if it does not already exist:

    sudo mkdir -p /etc/systemd/network
    
  2. Create a .network file and add the following content. Replace the variables in brackets with the appropriate values. Replace [INTERFACE_NAME] with LINK from the output of the networkctl list command that was run previously.

    sudo $EDITOR /etc/systemd/network/70-static.network
    
    [Match]
    Name=[INTERFACE_NAME]
    
    [Network]
    Address=[IP_ADDRESS]/[CIDR_NETMASK]
    Gateway=[GATEWAY_IP_ADDRESS]
    DNS=[PRIMARY_DNS_IP]
    DNS=[SECONDARY_DNS_IP]
    

    See the systemd-network man page for more configuration options.

  3. Restart the systemd-networkd service:

    sudo systemctl restart systemd-networkd
    
  4. Verify your static IP address details have been set:

    networkctl status