How to assign a static ip address in Ubuntu (14.04 or later)

0
202

Step 1: Connect to the internet

Connect to the internet on the computer you want to assign a static ip address for. Visit your favourite website to make sure your connected.

Step 2: Gather ip/netmask/broadcast addresses

You need the following addresses to assign a static Ip address in ubuntu:

A) ip address
B) netmask
C) broadcast
D) gateway
E) network
F) dns-nameservers

We can find the first three by opening the terminal and typing:

ifconfig

The output will look like the following.

“ens33 Link encap:Ethernet HWaddr 00:1e:90:b7:57:db
inet addr:192.168.0.111 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::21e:90ff:feb7:57db/64 Scope:Link
inet6 addr: 2607:fea8:2960:27f:21e:90ff:feb7:57db/64 Scope:Global
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3934 errors:0 dropped:0 overruns:0 frame:0
TX packets:2012 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2551999 (2.5 MB) TX bytes:240206 (240.2 KB)
Interrupt:17

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:2 errors:0 dropped:0 overruns:0 frame:0
TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:190 (190.0 B) TX bytes:190 (190.0 B)

Use this to write down your address, broadcast, and netmask. In my case my address/broadcast/network are as follows:

address 192.168.0.111
netmask 255.255.255.0
broadcast 192.168.0.255

 

Step 3: Gather network/gateway address

Run the following command from the terminal to gather your network address:

route -n

You will see the following output:

“Kernel IP routing table
Destination          Gateway         Genmask
192.168.0.0        192.168.0.1     255.255.255.0”

In this case we can see a gateway address of 192.168.0.1. We will also use this address for our ‘network’ value as well. Write down this address.

 

Step 4: Gather dns-nameservers address:

Run the following command from the terminal:

nslookup 127.0.0.1

127.0.0.1 is your loopback address. It can be found by typing ifconfig and looking for the “inet address” under the “Local Loopback” section. The output will look as follows:

“Server: 192.168.0.1
Address: 192.168.0.1#53
1.0.0.127.in-addr.arpa name = localhost.”

Your dns-nameserver address will be the “Server” address in the output. In this case our dns-nameserver address is 192.168.0.1.

Step 5: Test the new address you want to assign

We now have all of the information we need to set up a static ip.
Here is what we have gathered from steps 1-4:

address = 192.168.0.111
netmask = 255.255.255.0
broadcast = 192.168.0.255
network = 192.168.0.1
gateway = 192.168.0.1
dns-nameservers = 192.168.0.1

The next step is to choose a new ip address that isn’t being used.

Run the following command in the terminal to test the ip connection you want to use. In my case I want to use 192.168.0.112 (you may need to make sure the address you are choosing is within the range of your router):

ping 192.168.0.112 

You may see something that looks like the following

“PING 192.168.0.112 (192.168.0.112) 56(84) bytes of data.
From 192.168.0.111 icmp_seq=1 Destination Host Unreachable
From 192.168.0.111 icmp_seq=2 Destination Host Unreachable
From 192.168.0.111 icmp_seq=3 Destination Host Unreachable”

if this is the case, you know that you are free to use this ip address, as it is unassigned. Write this ip address down, as we will use it in our next step.


Step 6: Making the changes to our interfaces file:

Run the following command in the terminal to modify your interfaces file to set the static ip address

sudo nano /etc/network/interfaces

You may see something 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”

 

A) Change the word “dhcp” to “static”.
B) Under the same line, insert all of the information we have gathered from steps 1-5.
C) Change the address that we gathered in  

The resulting file will look something 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 static
address 192.168.0.111
netmask 255.255.255.0
broadcast 192.168.0.255
network 192.168.0.1
gateway = 192.168.0.1
dns-nameservers 192.168.0.1”

 

Step 7: reboot:

You must reboot your machine for these changes to take effect. Run the following command in the terminal:

sudo reboot

LEAVE A REPLY