When configure a static IP in Ubuntu, you'll need to follow these procedures:
To collect information on the current network
You'll need to collect details of the current network configuration so you can make the necessary changes to your configuration. In this procedure, you'll gather the following:
- Available adapters
- Respective IP information
You may also want to verify your default gateway configuration.
- Log in as an Administrator with sudo rights.
Run the following command to collect the details of the available adapters and respective IP information:
sudo ip a
The results look like the following:1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 00:16:3e:e2:52:42 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 192.168.0.2/24 brd 192.168.0.255 scope global dynamic eth0
valid_lft 3257sec preferred_lft 3257sec
inet6 fe80::216:3eff:fee2:5242/64 scope link
valid_lft forever preferred_lft forever
- Make a note of the following to use in the next procedure:
- Network adapter (in the example above, the network adapter is eth0)
Current IP address (in the example above, the IP address is 192.168.0.2/24)
- Optionally, run the following command to verify your default gateway configuration:
sudo ip route show
The results look like the following:default via 192.168.0.1 dev eth0 proto dhcp src 192.168.0.2 metric 100
- Make a note of the default gateway (in the example above, the default gateway is 192.168.0.1).
To configure the network
- Use the following command to list the files in the /etc/netplan directory:
ls /etc/netplan
- Make a note of the existing configuration file if there is one.
- Run one of the following commands:
- To update the configuration file:
sudo vim /etc/netplan/[config.yaml]
, where[config.yaml]
is the name of the existing configuration file from step 2 above. - To create the configuration file is one doesn't exist:
sudo vim /etc/netplan/01-netcfg.yaml
01-netcfg.yaml
will be the filename of the created configuration file.
- To update the configuration file:
- Open the configuration file and edit the file with one of the following configurations:
- For Ubuntu environments Ubuntu Bionic 18.04 LTS and older:
network:
version: 2
renderer: networkd
ethernets:
[ethX]:
addresses:
- [XXX.XXX.X.XXX/XX]
gateway4:
[XXX.XXX.X.X]
nameservers:
addresses: [X.X.X.X, X.X.X.X]
Where[ethX]
is your network adapter and[XXX.XXX.X.XXX/XX]
is the current IP address from step 1;[XXX.XXX.X.X]
is the default gateway from step 4, and[X.X.X.X, X.X.X.X]
are the nameserver addresses. - For newer Ubuntu environments:
network:
version: 2
renderer: networkd
ethernets:
[ethX]:
addresses:
- [XXX.XXX.X.XXX/XX]
routes:
- to: default
via: [XXX.XXX.X.X]
nameservers:
addresses: [X.X.X.X, X.X.X.X]
Where[ethX]
is your network adapter and[XXX.XXX.X.XXX/XX]
is the current IP address from step 1;[XXX.XXX.X.X]
is the default gateway from step 4, and[X.X.X.X, X.X.X.X]
are the nameserver addresses.
- For Ubuntu environments Ubuntu Bionic 18.04 LTS and older:
Save the file.
To test and apply your changes
- Run the following command:
sudo netplan try
- Do one of the following:
- If this command runs with no errors, confirm you want to apply the settings.
- If the command runs with errors, fix the errors and repeat step 1.
- Run the following command to verify the static IP has been applied:
sudo ip a
You will see results similar to the following:1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 00:16:3e:e2:52:42 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 192.168.0.100/24 brd 192.168.0.255 scope global dynamic eth0
valid_lft 3257sec preferred_lft 3257sec
inet6 fe80::216:3eff:fee2:5242/64 scope link
valid_lft forever preferred_lft forever