How to setup Static IP Address on Ubuntu Server 16.04 over LAN (Also works on other Linux Dist.)



Open up /etc/network/interfaces file

    sudo nano /etc/network/interfaces
 # The primary network interface  
 auto ens18  
 iface ens18 inet dhcp  
now change your primary interface 'ens18' from dhcp to static
 # The primary network interface  
 auto ens18  
 iface ens18 inet static  
Note: the primary interface ens18 will be different in your system(for example 'eth0'). Now add the following options just below the above lines
 address 192.168.0.151  
 netmask 255.255.255.0  
 network 192.168.0.0  
 broadcast 192.168.0.255  
 gateway 192.168.0.1  
 dns-nameservers 192.168.0.1  
now the configuration is look like this
 # The primary network interface  
 auto ens18  
 iface ens18 inet static  
 address 192.168.0.151  
 netmask 255.255.255.0  
 network 192.168.0.0  
 broadcast 192.168.0.255  
 gateway 192.168.0.1  
 dns-nameservers 192.168.0.1  
where the address is static ip, and at network put your router's ip address and replace '1' with '0' in in right-most place of IP address. For example 192.168.0.1 => 192.168.0.0
In broadcast put 255 at first place of your router's IP, and in gateway and dns-nameservers put your gateway (router) IP address. Now press Ctrl + O to save the file and Ctrl + X to exit. Now we need to edit the /etc/resolv.conf file.

    sudo nano /etc/resolv.conf

at this file put your gateway address in the 'nameserver' option.
 nameserver 192.168.0.1  
now save the file and restart the networking components

    sudo /etc/init.d/networking restart

Note: if by running above command the changes does not take place then just reboot the machine.

and thats it.