Skip to content

Instantly share code, notes, and snippets.

@cpburnz
Created July 6, 2022 16:12
Show Gist options
  • Save cpburnz/46c266e65abe6e6f41be192286c18341 to your computer and use it in GitHub Desktop.
Save cpburnz/46c266e65abe6e6f41be192286c18341 to your computer and use it in GitHub Desktop.
Ubuntu Server 22.04 Static IP

Static IP Address

In order to set a static IP address on Ubuntu Server 22.04, you need to configure it through netplan.

You can view the current network information using networkctl by running the following command:

networkctl status

This will output something similar to:

●        State: routable                          
  Online state: online                            
       Address: 192.168.0.130 on ens192
                fe80::250:56ff:fea2:b891 on ens192
       Gateway: 192.168.0.1 on ens192
           DNS: 192.168.0.54

Jul 06 11:21:46 myserver systemd-networkd[1548]: ens192: DHCPv6 lease lost
Jul 06 11:24:32 myserver systemd-networkd[1548]: ens192: Re-configuring with /run/systemd/network/10-netplan-ens192.network
Jul 06 11:24:32 myserver systemd-networkd[1548]: ens192: DHCPv6 lease lost
Jul 06 11:24:32 myserver systemd-networkd[1548]: ens192: Re-configuring with /run/systemd/network/10-netplan-ens192.network
Jul 06 11:24:32 myserver systemd-networkd[1548]: ens192: DHCPv6 lease lost
Jul 06 11:50:58 myserver systemd-networkd[1548]: ens192: Re-configuring with /run/systemd/network/10-netplan-ens192.network
Jul 06 11:50:58 myserver systemd-networkd[1548]: ens192: DHCPv6 lease lost
Jul 06 11:50:59 myserver systemd-networkd[1548]: ens192: Re-configuring with /run/systemd/network/10-netplan-ens192.network
Jul 06 11:50:59 myserver systemd-networkd[1548]: ens192: DHCPv6 lease lost
Jul 06 11:50:59 myserver systemd-networkd[1548]: ens192: DHCPv4 address 192.168.0.130/24 via 192.168.0.1

Configure netplan

Let's configure the network device ens192 to use the static IP address 192.168.0.81, DNS server at 192.168.0.54, and gateway at 192.168.0.1. Create the file /etc/netplan/99-static-ip.yaml with the contents:

network:
  version: 2
  renderer: networkd
  ethernets:
    ens192:
      addresses:
      - 192.168.0.81/24
      dhcp4: false
      dhcp6: false
      nameservers:
        addresses:
        - 192.168.0.54
      routes:
      - to: default
        via: 192.168.0.1

Apply the configuration by running the command (WARNING: this will kill any active SSH connections):

sudo netplan apply

Check the Network

Check the new network information:

networkctl status

You should see output similar to:

●        State: routable                          
  Online state: online                            
       Address: 192.168.0.81 on ens192
                fe80::250:56ff:fea2:b891 on ens192
       Gateway: 192.168.0.1 on ens192
           DNS: 192.168.0.54

Jul 06 11:50:58 myserver systemd-networkd[1548]: ens192: Re-configuring with /run/systemd/network/10-netplan-ens192.network
Jul 06 11:50:58 myserver systemd-networkd[1548]: ens192: DHCPv6 lease lost
Jul 06 11:50:59 myserver systemd-networkd[1548]: ens192: Re-configuring with /run/systemd/network/10-netplan-ens192.network
Jul 06 11:50:59 myserver systemd-networkd[1548]: ens192: DHCPv6 lease lost
Jul 06 11:50:59 myserver systemd-networkd[1548]: ens192: DHCPv4 address 192.168.0.130/24 via 192.168.0.1
Jul 06 11:59:17 myserver systemd-networkd[1548]: ens192: Re-configuring with /run/systemd/network/10-netplan-ens192.network
Jul 06 11:59:17 myserver systemd-networkd[1548]: ens192: DHCP lease lost
Jul 06 11:59:17 myserver systemd-networkd[1548]: ens192: DHCPv6 lease lost
Jul 06 11:59:17 myserver systemd-networkd[1548]: ens192: Re-configuring with /run/systemd/network/10-netplan-ens192.network
Jul 06 11:59:17 myserver systemd-networkd[1548]: ens192: DHCPv6 lease lost

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment