Linux Basics - Set a Static IP on Ubuntu

This tutorial explains how to set up a static IP address on an Ubuntu system from the command line. It covers network configuration for all current versions of Ubuntu and includes instructions for configuring a static IP address, setting the hostname, and configuring name resolution.

Network configuration on Ubuntu 22.04, Ubuntu 20.04, and Ubuntu 18.04

The network configuration in Ubuntu is made with a tool called netplan. It replaced the traditional /etc/network/interfaces file.

Configure a Static IP Address with Netplan on Ubuntu

Here are the steps to configure a static IP address with Netplan. The Netplan configuration files are in the directory /etc/netplan/. The default configuration file is /etc/netplan/01-netcfg.yaml.

Open the network config file with an editor. The netplan configuration filename differs depending on the Ubuntu version.

Ubuntu 22.04 and Ubuntu 20.04:

sudo nano /etc/netplan/00-installer-config.yaml

Ubuntu 18.04:

sudo nano /etc/netplan/01-netcfg.yaml

The configuration syntax is in Python programming language (.yaml format), so the indentation of the lines is important!

The content of the file is the same on Ubuntu 22.04 - 18.04.

Here is an example of a static IPv4 address 192.168.1.100 on the first network interface ens33 and gateway IP 192.168.1.1. The server will use the free Google DNS servers 8.8.8.8 and 8.8.4.4 to for name resolving.

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
ens33:
dhcp4: no
dhcp6: no
addresses: [192.168.1.100/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8,8.8.4.4]

Or, as Screenshot from an Ubuntu server:

Ubuntu 22.04 Network Configuration File

An IPv6 address can be added in the addresses line, separated by a comma. Example:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
ens33:
dhcp4: no
dhcp6: no
addresses: [192.168.1.100/24, '2001:1::1/64']
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8,8.8.4.4]

You must wrap the IPv6 address into single quotes. You will get a syntax error otherwise.

To apply the changes, run the following:

sudo netplan apply

Or use it with the --debug switch to get some useful output if parsing of the netplan config file was successful.

sudo netplan --debug apply

Configure a DHCP address with Netplan

Here is the configuration to get the network configuration for IPv4 and IPv6 from a DHCP server.

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
ens33:
dhcp4: yes
dhcp6: yes

To apply the changes, run:

sudo netplan apply

More Netplan config options

Netplan is a complex new configuration system configuring network cards, virtual devices, VLANs and bridges in Ubuntu 18.04. See the man page for more examples and an in-depth explanation of the syntax.

Network configuration on Ubuntu 12.04 - 17.04 (incl. Ubuntu 16.04 LTS)

Step 1: Configure the network interface

In this step, you will manually configure your network interface by editing the following files using your preferred text editor(nano gedit vi). For this example, I'm using the "nano" editor. You can edit the appropriate file by entering the following command into the terminal:

You can copy and paste directly from this line.

sudo nano /etc/network/interfaces

Enter your root password, once your preferred editor opens the file you can see this on older Ubuntu versions:

auto lo eth0
iface lo inet loopback
iface eth0 inet dynamic

Ubuntu Systems with systemd (like Ubuntu 16.04 and newer), the network interface is named ens33 instead of eth0 now and the word 'dynamic' has been replaced with 'dhcp'.

A configuration where the IP address get's assigned automatically by DHCP will look like this:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto ens33
iface ens33 inet dhcp

Statically configured network cards will have a section like this on older Ubuntu versions:

auto lo eth0
iface lo inet loopback
iface eth0 inet static
        address xxx.xxx.xxx.xxx(enter your ip here)
        netmask xxx.xxx.xxx.xxx
        gateway xxx.xxx.xxx.xxx(enter gateway ip here,usually the address of the router)

Here is an example for an older Ubuntu Release:

auto lo eth0
iface lo inet loopback
iface eth0 inet static
	address 192.168.1.100
	netmask 255.255.255.0
	gateway 192.168.1.1

And here an example for Ubuntu 16.04 and newer:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# test

# The primary network interface
auto ens33
iface ens33 inet static
address 192.168.1.100
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4

And here the complete network configuration file from an Ubuntu 16.04 system.

Interfaces configuration file

If you use "nano" editor to edit the configuration file, type Ctrl+x to save changes.

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?

<---Type "y"

File Name to Write: interfaces

<---ENTER

Step 2: Configure the DNS servers

Changes in /etc/resolv.conf are required only on Systems with Ubuntu < 14.04, for newer Ubuntu versions the nameservers get configured in the /etc/network/interfaces file or the netplan config file.

a) Ubuntu 20.04

Open the netplan configuration file with an Editor. I will use the nano editor in this example:

sudo nano /etc/netplan/00-installer-config.yaml

I've marked the DNS server IP addresses bold:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
ens33:
dhcp4: no
dhcp6: no
addresses: [192.168.1.100/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8,8.8.4.4]

b) Ubuntu 18.04

Open the netplan configuration file with the nano editor:

sudo nano /etc/netplan/01-netcfg.yaml

I've marked the DNS server IP addresses bold:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
ens33:
dhcp4: no
dhcp6: no
addresses: [192.168.1.100/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8,8.8.4.4]

c) Ubuntu versions 14.04 and 16.04

Open the /etc/network/interfaces file again and add a line dns-nameservers 8.8.8.8 right after the gateway line.

sudo nano /etc/network/interfaces
auto lo
iface lo inet loopback
iface ens33 inet static
        address xxx.xxx.xxx.xxx(enter your ip here)
        netmask xxx.xxx.xxx.xxx
        gateway xxx.xxx.xxx.xxx(enter gateway ip here,usually the address of the router)
        dns-nameservers 8.8.8.8

The nameservers 8.8.8.8 and 8.8.4.4 are provided by Google for public use, so you can use them in your network configuration.

If you use "nano" editor, type Ctrl+x to save changes.

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?

<---Type "y"

File Name to Write: interfaces

<---ENTER

d) Ubuntu versions < 14.04

In this step, you will manually configure your dns configuration file.

sudo nano /etc/resolv.conf

Once your editor opens the file you want to enter the following information...

nameserver xxx.xxx.xxx.xxx(enter your dns server ip)
nameserver xxx.xxx.xxx.xxx(enter your alt dns server ip)

If you use "nano" editor, type Ctrl+x to save changes.

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?

<---Type "y"

File Name to Write: resolv.conf

<---ENTER

Here is an example:

nameserver 8.8.8.8
nameserver 8.8.4.4

Step 3: Restart networking

Manually restart your network interface with the new settings.

For Ubuntu 20.04 and 18.04, use the netplan command to apply changes and restart the network. The command is:

sudo netplan apply

For Ubuntu versions 14.04 and 16.04 we use systemctl instead:

systemctl restart [email protected]

At this point you can check if the settings are correct:

ifconfig

If everything is correct you will get this result.

eth0      Link encap:Ethernet  direcciónHW 00:33:27:46:2v:34
          Direc. inet:192.168.1.101  Difus.:0.0.0.0  Másc:255.255.255.0
 ...

For Ubuntu < 14.04 use the networking init script:

sudo /etc/init.d/networking restart

This should return a result that looks like the following:

*Reconfiguring network interfaces… [OK]

Configure the Hostname

The hostname of an Ubuntu Server or Desktop is being configured in the files /etc/hostname and /etc/hosts. The /etc/hostname file sets the actual system hostname while /etc/hosts is used for the local name resolution.

In this example, I will change the hostname of my system to obelix.example.com.

First, edit the /etc/hostname file

sudo nano /etc/hostname

The hostname file contains the local part of the hostname only. The local part here is "obelix". Change the content of the /etc/hostname file to:

obelix

and save the file. The hostname file as it looks in nano after editing:

Configure Ubuntu Hostname

Then open the /etc/hosts file with an editor:

sudo nano /etc/hosts

and change the line that starts with the IP address of the system like this:

192.168.1.100   obelix.example.com     obelix

Here a screenshot of the /etc/hosts file.

Ubuntu hosts file

The format is like this:

[IP Addesss] [full hostname incl. domain] [local part of the hostname]

Finally, restart the system to apply the hostname change.

Share this page:

Suggested articles

36 Comment(s)

Add comment

Comments

By: Pilgrim at: 2012-04-16 10:16:51

This command is deprecated on new debian and ubuntu:

/etc/init.d/networking restart

solution is a use command:

ifdown eth0 && ifup eth0

See #565187 netbase: Command deprecated and not explained

By: Anonymous at: 2013-12-10 04:52:06

Bless you.  I saw that error and was noticing I had to completely restart to get the command to take.  I was not liking that and once I scrolled down to see if someone saw the same, I found your post.  Thank you so much!

By: Katz at: 2012-04-16 15:18:18

What´s the ubuntu version, 11.10 or 12.04?

By: Freaki at: 2012-09-15 02:22:27

I dunno about you but I don't like using the resolv.conf, I prefer to set the the nameserver to read my routers, and typically I'll use google's 8.8.8.8 or 8.8.4.4

 Heres a small guide I found if anyone is interested.

 http://ubuntuserverhelp.com/setting-up-a-static-ip/

By: Ranting at: 2012-10-09 20:06:21

I used to use the command line method before Ubuntu 12.04 but now prefer the network manager method of setting a static IP.

This is only because after using this method then opening the connection manager to check what was showing (I know I could have checked on cli but dont like to be limited!) all the settings were reset to dhcp and my modifications were reset!

I didn't really fancy removing the network manager. Anybody else feel like they are being forced to use graphical methods more and more with newer Ubuntu releases?

By: Ranting at: 2013-11-26 22:46:29

These instructions are slightly dated now and a lot of other instructions I'm seeing fail to address Network Manager managing the connection.
 
This article about setting a static IP in Ubuntu the proper way was very useful for me.
 
It's useful to note that the instructions on many sites and askubuntu answers do not apply to wireless devices!
 

 

 

By: Anonymous at: 2013-04-28 14:34:35

Hmm any idea why you don't just do everything within the /etc/network/interfaces configuration folder like here: http://draalin.com/setting-up-a-static-ip-address-in-ubuntu/

 Or does it make more sense to actually split things up like you did within the resolv.conf and interfaces configuration folder?

By: brubakes at: 2013-10-19 14:27:37

Just what I needed.  Thanks!

By: Chad at: 2015-08-13 14:23:27

Hey this is not working on 14.04 Server Edition.

 

auto eth0iface eth0 inet static        address 173.244.42.82        netmask 255.255.255.240        gateway 173.244.42.81        # dns-* options are implemented by the resolvconf package, if installed        dns-nameservers 8.8.8.8

 

 

resolv.conf

 

nameserver 8.8.8.8nameserver 8.8.4.4

ifconfig

 

eth0      Link encap:Ethernet  HWaddr 00:25:90:3a:c2:36            inet addr:173.244.42.82  Bcast:173.244.42.95  Mask:255.255.255.240          inet6 addr: fe80::225:90ff:fe3a:c236/64 Scope:Link          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1          RX packets:406 errors:0 dropped:174 overruns:0 frame:0          TX packets:182 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000           RX bytes:40001 (40.0 KB)  TX bytes:28133 (28.1 KB)          Interrupt:16 Memory:fb5e0000-fb600000

By: till at: 2015-08-13 14:26:00

It is working fine fro me on Ubuntu 14.04 server. Your ifconfig output shows that the IP settings got applied correctly. If you have problems with your IP setup, then please post in the forum to get help.

By: shabeer ahmad at: 2015-09-30 07:20:51

i have followed the above procedure for setting up interfaces of my ubuntu 14.04 for my eth0 interface not showiing ip address after giving ifconfig command

By: TempleClause at: 2016-02-06 16:39:53

You should specify what to do for version 14.04... 

For me it's unclear what to do since < 14.04 and > 14.04 does not match 14.04 in either case....

By: anonymus at: 2016-04-13 21:14:39

Thanks very very match my friend!! This issues it's verry helpfulyy.

By: Mukund at: 2016-04-27 06:00:41

Hi,

Can you guide me to set the static IP in Ubuntu 16.04

By: till at: 2016-04-27 06:03:02

The instructions above are valid for Ubuntu 16.04 as well.

By: ilayaraja at: 2016-04-29 04:41:09

I changed my IP in my ubuntu by using this method.

thanks.

By: sanwarul hoq at: 2016-06-02 13:47:28

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?... any kind of talk will ....

By: Anonymous at: 2016-09-12 19:29:33

I did exactly as instructed, and did not get the desired result, in fact, nothing changed!

By: JonhDevNet at: 2016-09-16 21:52:14

I also tried in version 16.04 but did not work.

I will keep trying.

By: Lucas at: 2016-10-17 19:26:26

Before I ask, I would just like to let you know this is a great and detailed article and it taught and helped me so much. I am running Ubuntu 16.04, and followed all the steps. But I cannot find "eth0      Link encap:Ethernet  direcciónHW 00:33:27:46:2v:34

          Direc. inet:192.168.1.101  Difus.:0.0.0.0  Másc:255.255.255.0 ..."

The closest thing I can find is "eth0      Link encap:Ethernet  HWaddr 00:23:5a:18:18:ab  

          inet addr:192.168.1.114  Bcast:192.168.1.255  Mask:255.255.255.0

          UP BROADCAST MULTICAST  MTU:1500  Metric:1

"

Also, I could not access the internet until I disconnected from the Ethernet network! Is this normal?

By: Tom S at: 2016-12-23 16:31:08

Thanks

By: Ratboy at: 2017-03-04 19:16:50

hi, i tried this to but now my internet does't work anymore. if i do ifconfig i dont get that anymore  eth0      Link encap:Ethernet  direcciónHW 00:33:27:46:2v:34

          Direc. inet:192.168.1.101  Difus.:0.0.0.0  Másc:255.255.255.0

and befor i did get 

lo link encap (and a lot more)now i only get that.

can it be that my internet doesn't use eth0 but ens160?

 

 

By: Steffen at: 2017-05-08 16:19:40

This doesn't work anymore when you're on a version of Ubuntu that uses systemd.

By: till at: 2017-05-08 16:29:57

The tutorial works fine on Ubuntu versions with systemd, use the commands that are labeled for "Ubuntu 14.04 and newer" which show you how to restart networking with systemd.

By: Pedromiguel Pinto at: 2017-05-31 14:19:04

En Ubuntu 16.04 existe un bug y es que no basta con reiniciar la interfaz de red, es necesario reiniciar el servidor para que tope los cambios en la interfaz, recomiendo sea cual sea la versión y si es posible reiniciar el sistema operativo después de modificar la interfaz de red.

Saludos

By: TiTex at: 2017-10-28 05:48:58

why not just use nmcli or nmtui ?

By: Jason at: 2017-10-29 13:29:36

The suggested steps for Ubuntu 16.04 do not work. Nothing changes...

By: till at: 2017-10-29 17:03:37

The steps work fine here on Ubuntu 16.04, just verified that. If nothing changes on your server then you might have a typo somewhere. You might also try to restart the system to apply the changes when you are sure that you have no typos in the config.

By: Tech Gii at: 2017-12-28 03:42:06

Using netplan I could not create a bridge for kvm virtulization.  Request for a small tutorial on this.  Its really sad that after years of experience in Linux,  I could not use netplan may be its complex or I do not know exact things.

 

By: tontze at: 2018-02-18 08:53:25

Is there a tutorial how to setup static wifi with netplan in ubuntu server 17.10 ? 

By: Jurek at: 2018-05-03 11:22:33

Final 18.4 Ubuntu server adds clouds to network configuration. What is the relation between cluster configuration files in /etc/cloud/cloud.cfg.d and /etc/netplan?

By: Mr Panda Sauce at: 2018-08-13 19:47:39

It sucks that Ubuntu changed its networking infrastructure. I wish they kept the /etc/networking.d restart. that way new users could also learn about it. now its based on yaml >:

By: sethgwapo at: 2019-10-10 09:04:25

wow this is cool thanks for sharing! xD

By: allzz at: 2020-05-19 14:45:53

Nice article! Clear and concise, very helpful! Thanks!

By: zenadm1n at: 2021-04-28 13:31:06

Instead of "apply" run the following to check your syntax with auto-rollback the configuration.

$ sudo netplan try

By: Max at: 2023-02-25 14:48:08

In netplan, the filename doesn't matter.  It  just needs to be in the correct directory and end in .yaml.  Any conficts should be removed, so if you are creating a static IP and the existing yaml file is for DHCP, just rename the prior, existing, file so it doesn't end in .yaml.  Create the new file ... I like to use the filename template of

`$ ls /etc/netplan/enp*/etc/netplan/enp3s0-static.yaml/etc/netplan/enp7s0f0-SAN-static.yaml.disable/etc/netplan/enp7s0f1-mgnt-static.yaml.disable`so that the filename says which device, which LAN and if it is static or dhcp.