How To and Why Set Up a Static IP Address on GNU/Linux
Summary
This tutorial explains what a static IP address is, and why to use
one. It also outlines how to set up a static IP address on
GNU/Linux.
The steps in this tutorial have been tested on kubuntu (12.04, 14.04,
16.04, 20.04, 22.04) and mythbuntu (12.04, 14.04), and should work
with many other GNU/Linux distributions.
This tutorial assumes that your computer is on a private home network
that is connected to the Internet via a router.
Update 2023-02-25: Add method using nmcli command for
Ubuntu 22.04 and later.
Update 2021-08-18: Add note regarding Ubuntu 20.04 also
requiring edit of /etc/resolve.conf to resolve Internet hostnames.
Update 2021-01-08: Add "Appendix A: Router IP Address
Reservation" section.
Contents
- What is a Static IP Address?
- Why Use a Static IP Address?
- Acquiring Network Information
- Setting Up Static IP Address
- References
- Appendix A: Router IP Address Reservation
What is a Static IP Address?
A static Internet Protocol Address (IP address) is a fixed location
at which a computer can be contacted on a network.
This fixed location must be unique on the network so that network
traffic meant for the computer is sent only to that computer. This
is similar to the way your home address is unique, so that your
postal mail is delivered only to your home address.
Why Use a Static IP Address?
A static IP address is needed when a computer is to provide a
service to other computers. Examples of services are a web page
server, a MythTV backend server, and a calendar and address
server.
In order for other computers to reliably contact the service on the
computer, the computer must have a unique static (unchanging) IP
address.
Advantages of a static IP address are:
- Can provide services for other computers because computer is accessible at a known, unchanging IP address
- Faster boot because no time spent waiting for a dynamic IP address
Disadvantages of a static IP address are:
- More difficult to move computer to other networks because the computer network configuration will require changing
- Requires more knowledge of networking to support
If other computers do not rely on services from your computer, or you often move your computer and use it on different networks, then staying with a dynamically assigned IP address is probably best.
Acquiring Network Information
Before we can set up a static IP address, we need to determine some
information about your computer and the network to which the
computer is connected.
The items that we will need to set up a static IP address are:
- Network interface name
- Network your computer is on
- Gateway address
- Network mask
- Broadcast address
- A range of addresses set aside for static IP addresses
Fortunately much of this information can be readily determined.
- Open a terminal window.
In the K Desktop Environment (kubuntu) use menu option: K -> System -> Konsole (Terminal)
In XFCE (mythbuntu) use menu option: Applications -> System -> Xfce Terminal
TIP: Opening a terminal in other desktop environments
You can often open a terminal window using the following steps:
a) Press Alt+F2 to open a run command prompt
b) Enter "xterm" without the quotes
- Query for the networking information.
Use either method.
- Method A: ip command
ip route
Sample output:
default via 192.168.1.1 dev enp4s0 onlink 169.254.0.0/16 dev enp4s0 scope link metric 1000 192.168.1.0/24 dev enp4s0 proto kernel scope link src 192.168.1.54
- Method B: netstat command (if net-tools package installed)
sudo netstat -rn
Sample output:
Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 enp4s0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 enp4s0 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 enp4s0
- Method A: ip command
- Determine the networking details.
From the either of the above commands we can learn the following:
enp4s0 - network interface name (other examples are enp0s31f6 and eth0)
192.168.1.1 - gateway address and address to use for Domain Name Server address (same as home router IP address)
192.168.1.0 - network provided by the home router (the first three numbers match the gateway)
/24 or 255.255.255.0 - network mask (a class C network for up to 254 devices)
192.168.1.255 - broadcast address (replace the last 0 in the network with a 255)
The above values are colour coded to help later when configuring the network interface.
TIP: Other Private Class C Networks
Private class C networks start with 192.168. Many home routers commonly use 192.168.1.x addresses, but values from 0 to 254 can be used (not just 1).
For example 192.168.0.254 is a valid class C network address on the private network 192.168.0.0.
- Choose a static IP address.
To learn the range of IP addresses set aside by the router for static IP addresses, refer to your router documentation or login to your home router to check these settings. In general these are IP address outside of the range that is dynamically assigned by your home router.
From my experience with Linksys and D-Link routers, addresses with a last number of 51 to 64 have been outside the range of dynamically assigned addresses and are safe to use as a static IP address.
Choose a number that will be unique on your network (no two computers can use the same IP address).
For example, I chose the following static IP addresses for two different computers on my home network.
192.168.1.53 - My desktop computer
192.168.1.55 - My MythTV personal video recorder
Setting Up Static IP Address
We are now ready to set up the static IP address on the computer.
For this example I used the network information determined above and
the following IP address:
192.168.1.53 - static IP address.
Use either of the following methods.
Method A: nmcli - Network Manager Command Line Interface (Ubuntu 22.04+)
I use this method for Ubuntu 22.04 and later.
- View current connection details.
nmcli connection show
- Assign the static ip address.
sudo nmcli con add type ethernet con-name 'static-ip' \ ifname enp4s0 ipv4.method manual \ ipv4.addresses 192.168.1.53/24 gw4 192.168.1.1
- Add the routing address.
sudo nmcli con mod static-ip ipv4.dns 192.168.1.1
- Bring up the networking interface.
sudo nmcli con up id 'static-ip'
- Confirm the change to the new networking values.
Use either:
ip a
or:
nmcli device show enp4s0
- Test you can access the Internet.
In a terminal window enter:
ping www.google.com
Use CTRL+C to cancel the ping commmand.
Sample output follows:
PING www.google.com (74.125.196.99) 56(84) bytes of data. 64 bytes from yk-in-f99.1e100.net (74.125.196.99): icmp_req=1 ttl=46 time=95.3 ms 64 bytes from yk-in-f99.1e100.net (74.125.196.99): icmp_req=2 ttl=46 time=94.9 ms ^C --- www.google.com ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1001ms rtt min/avg/max/mdev = 94.974/95.164/95.355/0.362 ms
Method B: /etc/network/interfaces File
I use this method for Ubuntu 20.04 and earlier.
- Create a backup of your existing network
configuration.
In a terminal window, enter the following commands:
cd /etc/network
sudo cp -p interfaces interfaces.backup
- Edit the /etc/network/interfaces file to
define a static IP address.
TIP: Use any text editor you prefer.
Many text editors are available.
kate is commonly used with KDE desktops
gedit is often used on GNOME desktops
leafpad is a graphical editor I install and use on XFCE desktops
nano is an editor often available on all GNU/Linux distributions.
Edit the file with a command similar to the following:
sudo nano /etc/network/interfaces
and make the content like the following lines.
Remember to replace the values with the ones you determined above.
---------- begin /etc/network/interfaces ----------
# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface # Note: These two lines are the only lines needed for a # dynamically assigned (DHCP) IP address. auto lo iface lo inet loopback # The primary network interface auto enp4s0 iface enp4s0 inet static address 192.168.1.53 network 192.168.1.0 netmask 255.255.255.0 broadcast 192.168.1.255 gateway 192.168.1.1 metric 0 dns-nameservers 192.168.1.1
---------- end /etc/network/interfaces ----------
Save file and exit editor. - Release the current IP address from the network
interface.
sudo ip addr flush enp4s0
- Restart the networking service.
Either use one of the following commands or reboot:
sudo /etc/init.d/networking restart
Or:
sudo systemctl restart networking.service
- Verify the IP address is correct.
sudo ifconfig enp4s0
Following is a sample of output from this command:
enp4s0 Link encap:Ethernet HWaddr bc:ae:c5:e1:4b:f1 inet addr:192.168.1.53 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::beae:c5ff:fee1:4bf1/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:54384 errors:0 dropped:0 overruns:0 frame:0 TX packets:32697 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:77236077 (77.2 MB) TX bytes:2463869 (2.4 MB)
- Test you can access the Internet.
In a terminal window enter:
ping www.google.com
Use CTRL+C to cancel the ping commmand.
Sample output follows:
PING www.google.com (74.125.196.99) 56(84) bytes of data. 64 bytes from yk-in-f99.1e100.net (74.125.196.99): icmp_req=1 ttl=46 time=95.3 ms 64 bytes from yk-in-f99.1e100.net (74.125.196.99): icmp_req=2 ttl=46 time=94.9 ms ^C --- www.google.com ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1001ms rtt min/avg/max/mdev = 94.974/95.164/95.355/0.362 ms
NOTE: Cannot ping the google server?
If the ping command cannot contact the google server and indicates packet loss, then something is wrong with the network configuration.
If you are unable to resolve the problem, you can restore the backup(s) you made earlier in /etc/network/interfaces.backup and /etc/resolve.conf.backup and reboot to return to your previous network configuration.
NOTE: Some Distros Use systemd-resolved
Some distros like Ubuntu 20.04 include systemd-resolved as a local DNS server. This means you will need to also edit the /etc/resolve.conf file to be able to resolve hostnames on the Internet.
Make a backup of /etc/resolve.conf:
sudo cp -p /etc/resolve.conf /etc/resolve.conf.backup
Edit file /etc/resolve.conf:
sudo nano /etc/resolve.conf
Comment out the existing nameserver line and add in a new name nameserver line with your gateway / router's IP address. For example:
# Generated by NetworkManager
#nameserver 127.0.0.53
nameserver 192.168.1.1
Save file and exit editor.
Reboot.
See also How to Resolve “Temporary failure in name resolution” Issue.
References
While setting up a static IP address, I found the following references useful:
- Wikipedia: IP Address
- Setting up a static IP address in Ubuntu
- Set static IP Ubuntu 16.04
- Set Static IP Address on Ubuntu 22.04 Command Line
Appendix A: Router IP Address Reservation
Many modern routers support IP address reservation. If your router supports this feature then you have another method to set a static IP address for a computer. With this method there are no file changes required on the Linux computer.
- Determine the MAC address for the computer.
Retrieve network information for the Ethernet card.
ifconfig
Following is a sample of output from this command:
In the above output the MAC address for the ethernet card is dc:a6:32:8e:d2:c3enp4s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.107 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::d836:16f4:af1d:5fa8 prefixlen 64 scopeid 0x20<link> ether dc:a6:32:8e:d2:c3 txqueuelen 1000 (Ethernet) RX packets 114 bytes 16015 (15.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 74 bytes 10490 (10.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
- Reserve a static IP address for a computer in the
router's configuration.
For this example we use static IP address: 192.168.1.53
The steps vary for each router and usually involve opening a web browser, logging into the router, locating the section for assigning/reserving static IP addresses, and then creating a record to pair a MAC address with a static IP address.
Following are high level instructions I used with my TP-LINK Archer C7 Wireless AC1750 Dual Band Gigabit Router:
- Open a web browser and visit router home page: http://192.168.1.1
- Login with the router admin account and password.
- Locate the section to set a static IP address.
For my router this is under DHCP -> Address Reservation.
- Add new record for MAC Address and static IP address
MAC Address Reserved IP Address b8-27-eb-7f-bf-62 192.168.1.53
- Reboot router.
Many routers require a reboot or power cycle of the router to activate router configuration changes.
- Reboot computer.
This enables computer to be assigned the static IP address that is configured in the router.