How to set a static IP address in Ubuntu

0
263

[ad_1]

fatmawati achmad zaenuri / Shutterstock.com

After collecting your connection name, subnet mask, and default gateway, you can set a static IP address on the terminal using the nmcli command. Or, on the GNOME desktop, open your connection settings and click the + icon, then enter the information for your static IP address there.

Your home network relies on IP addresses to route data between devices, and sometimes when reconnecting to the network, a device’s address can change. Here’s how to give an Ubuntu Linux computer a permanent IP address that will survive reboots.

Static and dynamic IP addresses

Everything on your home network, whether over a wired or Wi-Fi connection, has an IP address. IP stands for Internet Protocol. An IP address is a sequence of four numbers separated by three periods. Each IP address that is unique within that network.

IP addresses act as numerical labels. Your router uses these labels to send data between the correct devices. Usually your router assigns IP addresses. You know which IP addresses are in use and which ones are free. When a new device connects to the network, it requests an IP address, and the router assigns one of the unused IP addresses. This is called DHCP, or Dynamic Host Configuration Protocol.

When a device is rebooted or powered off and on, it may receive its old IP address once again, or it may be assigned a new IP address. This is normal for DHCP and does not affect the normal operation of your network. But if you have a server or some other computer that you need to be able to access by its IP address, you’ll run into trouble if your IP address doesn’t survive shutdowns or reboots.

Attaching a specific IP address to a computer is called assigning a static IP adress. A static IP address, as its name suggests, is not dynamic and does not change even if the computer is turned off and on.

Setting a static IP address in Ubuntu

We are demonstrating this technique on Ubuntu, but it should work on any Linux distribution. the nmcli The network administrator tool was released in 2004, so it should be present in almost any standard distribution.

Let’s take a look at the network connections that already exist on the computer. we are using the connection command with the show plot.

nmcli connection show

Use nmcli to enumerate network connections

This displays some information about each connection. We only have a single connection configured.

The details of a single network connection displayed by nmcli

The output is wider than the terminal window. This is the information shown to us.

NAME           UUID                                 TYPE     DEVICE 
netplan-enp0s3 1eef7e45-3b9d-3043-bee3-fc5925c90273 ethernet enp0s3
  • Name: Our network connection is called “netplan-enp0s3”.
  • UUID: The universally unique identifier that Linux uses to reference this connection internally.
  • Write: This is an ethernet connection.
  • Device: This connection uses the network interface “enp0s3”. It is the only network card on this computer.

We can use the ip command to discover the IP address that this computer is using.

ip addr

The output of the ip addr command showing the IP address of the computer

In the output we can see the entry “enp0s3” and its current IP address, 192.168.86.117. The “/24” is a shorthand way of saying that this network uses a subnet mask of 255.255.255.0. Make a note of this number, we will have to use it later.

We need to choose the IP address that we are going to set as our static IP address. Obviously you can’t use an IP address that is already in use by other device. A safe way to proceed is to use your current IP address. We know for a fact that nothing else is using that IP address.

If we want to use a different IP address, try pinging it. Let’s test if the IP address 192.168.86.128 is in use. If everything else on your network uses DHCP and you get no response to the ping command, it should be safe to use.

ping 192.168.86.128

Use ping to determine if an IP address is in use

Even if another device had previously used that IP address, it will be given a new IP address the next time it boots up. Nothing responds to ping requests. We are clear to go ahead and configure 192.168.86.128 as our new static IP.

We also need to know the IP address of your default gateway, which will usually be your broadband router. We can find this using the ip command and the route option, which we can abbreviate as “r”.

ip r

Using the ip command to find the IP address of the default gateway

The entry that starts with “default” is the route to the default gateway. Your IP address is 192.168.86.1. Now we can start issuing commands to configure our static IP address.

The first command is long.

sudo nmcli con add con-name "static-ip" ifname enp0s3 type ethernet ip4 192.168.86.128/24 gw4 192.168.86.1

Creating a new connection with the nmcli command

Taken in small pieces, it’s not as bad as it sounds. we are using sudo. the nmcli the arguments are:

  • with: Abbreviation for “connection”.
  • add: We’ll go to add a connection.
  • with-name “static-ip”: The name of our new connection will be “static-ip”.
  • ifname enp0s3: The connection will use the network interface “enp0s3”.
  • ethernet type: We are creating an ethernet connection.
  • ip4 192.168.86.128/24: The IP address and subnet mask in classless inter-domain routing notation. This is where you should use the number you made a note of earlier.
  • gw4 192.168.86.1: The IP address of the gateway we want this connection to use.

In order for our connection to work, we need to provide a few more details. Our connection exists now, so we’re not adding anything, we’re modifying the configuration, so we use the mod plot. The setting we are changing is the IPv4 DNS settings. 8.8.8.8 is Google’s primary public DNS server IP address and 8.8.4.4 is Google’s alternate DNS server.

Note that there is a “v” in “ipv4”. In the above command, the syntax was “ip4” without “v”. The “v” should be used when modifying the configuration, but not when adding connections.

nmcli con mod "static-ip" ipv4.dns "8.8.8.8,8.8.4.4"

Using the nmcli Command to Configure DNS Servers for a Connection

To make our IP address static, we need to change the method which the IP address gets its value. The default value is “auto”, which is the DHCP setting. We have to put it in “manual”.

nmcli con mod "static-ip" ipv4.method manual

Using the nmcli command to set a static IP address

And now we can start or “open” our new connection.

nmcli con up "static-ip" ifname enp0s3

Using the nmcli Command to Start a Network Connection

We don’t get any error messages, which is great. let’s use nmcli to look at our connections one more time.

nmcli con show

The details of two network connections displayed by nmcli

Here is the output:

NAME           UUID                                 TYPE     DEVICE 
static-ip      da681e18-ce9c-4456-967b-63a59c493374 ethernet enp0s3 
netplan-enp0s3 1eef7e45-3b9d-3043-bee3-fc5925c90273 ethernet --

Our static IP connection is up and using device “enp0s3”. The existing connection “netplan-enp0s3” is no longer associated with a physical network interface because we removed “enp0s3” from it.

Using the GNOME desktop and applications

Click the icons on the far right of the system bar to display the system menu, then click the “Wired” menu option. If you are using a wireless connection, click the name of your Wi-Fi network.

Available connections are displayed. A dot indicates which one is in use. Our new connection is the active connection. Click the “Wired Settings” or “Wi-Fi Settings” menu option.

The system menu with the panel "wired" expanded

The details of the active connection are displayed. We can see that our new “static ip” connection has the IP address, default gateway, and DNS servers that we configured for it. You can switch between the available connections by clicking directly on their names.

Click the gear icon to enter the “Static IP” connection settings.

The wired connection section in the Network tab of the Settings app

A dialogue box opens. Click on the “IPv4” tab.

The IPv4 tab of the connection settings dialog

Since we set our new IP address to be static, the “Manual” radio button is selected. You can change this back to DHCP by selecting the “Auto (DHCP)” radio button and clicking the green “Apply” button.

To create a new connection using the “Settings” application, click the “+”Icon on the “Networks” page, above the list of wired connections.

The wired connection section in the Network tab of the Settings app

A dialog box appears. We need to provide a name for this connection.

Give a name to a new connection profile in the dialog "New profile"

We will call our new connection “static-2”. Click on the “IPv4” tab.

Provide IPv4 connection details to a new connection profile in the dialog "New profile"

Select the “Manual” radio button and fill in the “Address”, “Netmask” and “Gateway” fields. Also fill in the DNS field and then click the green “Apply” button. Note the comma between the DNS entries.

Our new connection appears in the “Wired” connection panel.

A newly added connection in the wired connection section of the Network tab of the Settings app

connection convenience

Using the nmcli command or the GNOME desktop and applications, you can jump between network connections very easily and quickly.

It’s more convenient to have a selection of connection profiles and move between them as needed, rather than having one that you continually edit. If something goes terribly wrong with the connection you’re editing or adding, you can always fall back to one of the existing connections.

RELATED: How to use bmon to monitor network bandwidth on Linux

[ad_2]