Static IP Configuration on Linux
An IP address is a way to identify your system on a network or to give a destination or source while communicating over the internet.
Unlike most desktop machines where dynamic IPs can be used, when you are setting up a server or a remote host machine, as a SysAdmin, it is usually needed or recommended to set up a static IP to ensure that the destination is always known. This tutorial is about how to set up a static IP, for Linux systems.
For this tutorial, we will be using the ip
, and netplan
commands and not the phased out program ifconfig
.
Show your IP in linux with ip tool
To see your current IP, we can run the following command:
ip addr show
This would print all the running interfaces, and whatever is the current IP.
Switching an network Interface On/Off in Linux
Sometimes we need to put up an interface for connection or close(down) an interface to avoid any mishaps, we can run the following commands accordingly:
ip link set <ifName> up
ip link set <ifName> down
Check IP routing table in Linux
To see the IP routing used, you run:
ip route show # ip r show works the same
Generating static IP in Linux
If using netplan
, there are configuration YAML files in the directory /etc/netplan
. Usually, one or more files exist in the specified directory. The name may differ depending on installation and setup type, but the most common one is 01-netcfg.yaml
. The format of the file name is as follows; NN-interfaceName.yaml
. To assign a static IP, open the configuration file, using your preferred text editor. For example, the file on my system is 01-network-manager-all.yaml
.
Open in vim
, we will find something like the following.
Before we start editing the YAML file, let us understand the syntax and how to write the netplan
configs.
- Each file starts with a
network
that has at least 2 more elements underneath.
- The first is the version of the network config format.
- The second is the device type. Device type can be one of
ethernets
, bonds
, bridges
, or vlans
.
- There is also a renderer line and out-of-the-box
Ubuntu
uses either networkd
, or NetworkManager
. It is basically the software to be used as the backend for the configurations.
Under the device types, we can specify one or more interfaces. In our example, let's say we are modifying the usb0
interface. To assign a static IP, we need to do the following:
- Disable DHCP, by writing a line
dhcp4: no
.
- Specify static IP, under
addresses:
. One or more IPv4 and IPv6 addresses can be specified for that interface.
- Specify gateway.
- Under
nameservers:
, set IP addresses of the nameservers.
We get a final YAML
file as the following:
When editing a YAML
file, ensure to keep in mind YAML
indents, and code standards, to avoid any mishaps or errors.
Once done editing, run the following command:
sudo netplan apply
To verify the changes, we run the following:
ip a show dev usb0
We get the following output
Modify Static IP configuration in Linux (GUI)
Ubuntu can configure a static IP, using the settings application. Once in the settings application, we head to the Wi-Fi or Networks section. In my case, since I am connected to ethernet, we head to Network as follows:
We click on the settings icon after the switch under USB ethernet
, and head to the IPv4
tab for IPv4
settings, and IPv6
for IPv6
settings. Since we want to set a static IP for IPv4, we head to IPv4
, select Manual
, and input our parameters. Once done, click on apply, and then to verify, just run ip a
, in the terminal.
Conclusion
We have just covered in this tutorial how to manually edit the IP address of a system, and make sure it persists through boots, and other operations, via the command line, as well as through a GUI desktop settings app.