Setting a static IP on Ubuntu: a beginner's guide

Many situations occur when a static IP address needs to be set. For example, if you have programs that use the IP address say for remote login, then you might hardcode the IP address. But if the IP address is dynamic it can change, and thus you need an IP that does not change and hence you would need to set a static IP for your system. This tutorial deals with setting a static IP for Ubuntu OS.

1 Manually configuring the interface

This step involves manually configuring the network interface by making changes to the "interfaces" file. Here I use the "gedit" file editor but it is possible to use other editors like "nano", "vi", etc. Type the following command in the terminal:

sudo gedit /etc/network/interfaces

If it asks for password enter that and a file will open that looks like:

# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
iface etho inet dynamic

There might be more than one network card and so be sure which network card you want to change. First open another terminal and run the command:

ifconfig

This gives the IP address of your system along with "Bcast" address and "mask". Note the IP, Bcast and the mask of your system. This is done so that you can go back to the original IP when needed. Let's assume the values we have got for "eth0" network interface (that is my ethernet connection) is:

address: 192.168.28.37
Bcast:   192.168.28.255
Mask:    255.255.255.0

So, when the interfaces file is configured for a static IP on "eth0", it must have a general format like below:

# interfaces(5) file used by ifup(8) and ifdown(8)
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)

Below is a screenshot of my "interfaces" file. Note that Bcast also means Gateway, and mask also means netmask. Here we assume that our new static IP to set is 192.168.3.215.

So please make changes according to your "interfaces" file. Save and close it.

2 Configuring DNS information

Please note that this step isn't mandatory. If you do not want to change the DNS information, then skip this step. Here we change the IP of DNS server to what we prefer. There are a lot of DNS server IPs, that can be found and used. First open the file "resolv.conf" using the following command:

sudo gedit /etc/resolv.conf

Initially it will look something like this:

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 127.0.1.1
search Home

Now we can add many nameservers and change the existing one to suit our needs. Make changes and save the file. The following screenshot shows the resolv.conf file changed to have two nameservers:

3 Making the changes permanent

Once the changes have been made to one or both the files (interfaces and resolv.conf), we will have to apply those changes to reflect them. Previously, we could restart the network to make the changes happen, but due to some reason, newer versions of Ubuntu doesn't apply those changes. So, we will have to reboot the whole system. To do so, use the command:

sudo reboot

After the restart, the output of network details should show the changes. Open a terminal and run the command:

ifconfig

On examining the output , the eth0 interface should have the new IP, Bcast and mask. In this case, the output might look similar to this:

eth0      Link encap:Ethernet  HWaddr xx:xx:xx:xx:xx:xx  
          inet addr:192.168.3.215  Bcast:192.168.3.255  Mask:255.255.255.0
          .....

This confirms that the changes have been applied.

4 Configuring other interface like "eth1"

There might be other interfaces, like "eth1" that might require configuration. In this case, some changes are required to interface file as shown. The changes here are:

  • adding eth1 in the "auto" line
  • ading the configuration for eth0

Make changes as shown in the image below with the required IP, Bcast and mask

So, this way changes can be made to many different interfaces.

5 Configuring another IP for same interface

Here we have set one IP address for "eth0". But, what if we want to set another IP? Here we have set the IP as 192.168.3.215. Now for the same interface if we want to set another IP say 192.168.3.217, then we can do it by changing the "interfaces" file. Here "eth0:1" indicates that we are assigning a second IP for the "eth0" interface.The changes are represented in the image below.

6 Configuring IP address using the Desktop Application

Most of the configuration done above can be carried out in the desktop application called "network". To go ahead with that, start by opening the "network" application from the dash as shown in the image below.

Click on "network" to open it. It will look similar to the one below:

In that window, click on the "options" button at the bottom right corner and in the window that opens, click on the "IPv4 settings" tab. The window is shown below

Initially the setting is set to "Automatic(dhcp)", which is dynamic IP. To make it static, select the dropdown menu and click on manual as shown in the image below:

Once manual is selected the text box below it is activated. Click the "Add" button on the right and on the left hand text box, enter the IP, the mask(netmask) and the gateway (Bcast) addresses. Also add the DNS server IP if you wish to change that too. Once done click on save. After this editing, it should look similar to the image below:

Once saved, we will have to restart the system as before in order for the changes to be applied.

Conclusion

Now we can change the IP of an interface to static when we need to. We can have multiple static IP addresses for the same interface and multiple IPs of different interfaces. There can also be a mixture of static and dynamic IPs for different interfaces. It is also possible to change the IP using a desktop application. Please remember to take a screenshot or make a note of the initial IP config, which you might need, when you wish to go back to the initial settings. Also remember that giving IPs same as the ones already existing on the same network can lead to IP conflicts and might lead to network failure too.

Share this page:

Suggested articles

13 Comment(s)

Add comment

Comments

By: webmastir

So "network 192.168.1.0" no longer needs to be specified in the interfaces config? I think I've always done that for some stupid reason (bad habit?).

By: thctlo

Sorry but this is a bad howto..  

If you edit manualy, make sure you remove the resolvonf package.

second, remove the "network manager" package. if you dont do this, it will messup your config.

als no /etc/hosts check,,  if you switch from dhcp to static, you also must change the /etc/hosts to match your hostname and ip.

And in my options this should not be on the howto forge ( sorry, nice try, but this wil give you a very bad setup )

 

By: Gula

I guess is wrong to put dns-servers on resolv.conf. Ubuntu hold this also in the /etc/network/interface file, not? Thats why in the resolv.conf - file: DO NOT TOUCH

By: jack

hello

 

network connection is managed by nm-connection-editor. /etc/network/interfaces is not used anymore.

 

Jack

By: Tomas

Editing the resolv.conf file is a bit pointless because changes will be overwritten after reboot (as the message implied). You may want to stress this a bit more and show readers how to properly add nameservers in the /etc/network/interfaces:

iface eth0 inet static

 address 10.1.2.3

netmask 255.255.255.0

gateway 10.1.2.1

dns-nameservers 178.79.174.162

dns-nameservers 185.10.203.37 

 

By: Dan Saint-Andre

I want a static IP when I'm on my home-office/small-office network, but I need a dynamic IP when I'm walk-about with my laptop.  I wish there was some way to automatically process:  IF SSID="string" THEN run("script").  This script could then do whatever based on where I'm connected.

By: markit

The correct way to set DNS is NOT to modify /etc/resolv.conf that is also a link, so you broke things.

You have to add in /etc/network/interfaces a line like

dns-nameservers 8.8.8.8

in the eth0 section (i.e. below gateway line for instance)

By: ariel

thanks. the graphic interface worked for me .

By: Priyank Patel

I am trying to assign Public Ip address to mu Ubuntu PC. I already got connection from Public IP router. I am trying to assign Ip address: 199.33.115.224, subnet: 255.255.255.192, gateway: 199.33.115.193. But when i assigned the Address to network. It's show me the unmanaged network. SO, its not connected to network properly. Please, provide me a proper direction to assign public Ip address to my pc.

Thank you for help.

By: Ton Lagerway

HI,

Looking at setting up a Raspberry-pi VPN network and surveying my network parameters - your commands produce the following.Although there is some common ground, there is also considerable difference. Please advise:

sudo gedit /etc/network/interfaces

 

# interfaces(5) file used by ifup(8) and ifdown(8)

 

auto lo

 

iface lo inet loopback

 

 

 

ifconfig

 

enp3s0f1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

 

inet 192.168.1.102 netmask 255.255.255.0 broadcast 192.168.1.255

 

inet6 fd80:1382:3798:a000:cd28:9473:751d:759a prefixlen 64 scopeid 0x0<global>

 

inet6 fd80:1382:3798:a000:6868:a0df:eda4:255f prefixlen 64 scopeid 0x0<global>

 

inet6 fe80::8947:42e1:8a88:66c9 prefixlen 64 scopeid 0x20<link>

 

ether 88:d7:f6:2f:a1:2b txqueuelen 1000 (Ethernet)

 

RX packets 39159 bytes 46740672 (46.7 MB)

 

RX errors 0 dropped 0 overruns 0 frame 0

 

TX packets 28441 bytes 2694283 (2.6 MB)

 

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 1079 bytes 88667 (88.6 KB)

 

RX errors 0 dropped 0 overruns 0 frame 0

 

TX packets 1079 bytes 88667 (88.6 KB)

 

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

 

 

 

sudo gedit /etc/resolv.conf

 

 

 

# This file is managed by man:systemd-resolved(8). Do not edit.

 

#

 

# This is a dynamic resolv.conf file for connecting local clients to the

 

# internal DNS stub resolver of systemd-resolved. This file lists all

 

# configured search domains.

 

#

 

# Run "systemd-resolve --status" to see details about the uplink DNS servers

 

# currently in use.

 

#

 

# Third party programs must not access this file directly, but only through the

 

# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,

 

# replace this symlink by a static file or a different symlink.

 

#

 

# See man:systemd-resolved.service(8) for details about the supported modes of

 

# operation for /etc/resolv.conf.

 

 

 

nameserver 127.0.0.53

 

search home

 

 

By: Lupadhy

Why should we configure another IP for same interface ? Will it be helpful in case of ipadress conflict e.g. say I switched on my laptop and there is some other network connected device like mobile which has already aquired ip which I has mentioned as static IP for my laptop. Please guide and sorry for being novice in networking concepts .

By: till

Additional IP addresses are e.g. used for servers when you want to assign a dedicated Ip to a specific service or website or when you run multiple virtual machines on your computer where each virtual machine is connected to a different external IP.

By: Blake

Yeah, this would be great.  But I have to configure stupid yaml file.  

Which is not working.  WHY is so ******** hard to set a static IP in Linux?