This article explains assigning a static IP to your Linux machine through the command line. If you’re assigning a public IP address, you should’ve purchased it from your ISP. Assigning the IP address in Linux requires you to edit the network configuration file. The network interface files are located at different places according the Linux OS variant. This article will cover both Red Hat and Debian variants. You need to logged in as the root user to edit these files, or you should have sudo permissions.
Red Hat variants
Some of the Red Hat variant Linux OSes are CentOS, Fedora, SUSE Linux etc. If you’re using YUM to install packages then you have a Red Hat based OS. Use the VI editor and edit the following file
vi /etc/sysconfig/network-scripts/ifcfg-eth0
A machine having multiple Ethernet cards will have files named ifcfg-eth1, ifcfg-eth2 and so on. Add or edit the following lines
ONBOOT=yes
BOOTPROTO=none
IPADDR=192.168.0.2
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
By default BOOTPROTO is set to dhcp which should be changed, setting ONBOOT to yes will activate this network adapter when Linux boots and the care should be taken when entering the NETMASK value. Because unlike Windows OSes which automatically assign Subnet masks based on the class of the IP address manual assigning is required in Linux. Even though the interface will work if classless NETMASK is entered, the system might have problems in communicating with other network devices as they have a classful subnet mask. Refer to the Wikipedia article on Classful network to check in which class your IP address lies.
Debian variants
If you use dpkg to install packages then you’re using a Debian based Linux OS. Using VI editor open the following file.
vi /etc/network/interfaces
If you’re using DHCP (which is the default) the following is shown
auto eth0
iface eth0 inet dhcp
To assign an IP address statically delete those lines and enter the following
iface eth0 inet static
address 192.168.0.2
netmask 255.255.255.0
gateway 192.168.0.1
Here again you should keep an eye when entering the netmask.
Assigning the DNS IP addresses
Assigning DNS addresses is similar in both types of Linux OSes. Edit the following file using the VI editor
vi /etc/resolv.conf
The nameserver keyword is used to mention the DNS service IP address. If more than one DNS IP is to be specified use multiple entries
nameserver 8.8.8.8
nameserver 8.8.4.4
Restarting the interface and verifying the settings
For the changes to apply the interface has to be shutdown and brought up
ifdown eth0
ifup eth0
ifconfig
The ifconfig command should display the IP address and netmask assigned by you. Ping different IP addresses to check connectivity. To check the DNS ping a hostname or a domain name
ping google.com
You should get a reply.
Stefan says
I have a related question.
I have installed Ubuntu on several computers on a LAN. Somehow all of these computers have been assigned 192.168.x.x ip addresses, and these addresses do not collide.
I did *not* assign these static private subset ip addresses myself.
So I wonder how these are these static ip addresses in the private subnet determined? Does each Ubuntu computer simply scan, say by pinging, for others computers in the subnet. Here I am speculating!
That is, if a computer doesn’t get a response from 192.168.1.13, perhaps the computer assumes that there is no computer with that assigned ip adress???? That approach doesn’t seem rock solid as two computers could look for 192.168.1.13 at the same time and both claim that static ip address as their own when they don’t get a response.
Of course, I am just speculating! I actually do not know how Ubuntu assigns these static ip addresses in the private subnet, but I’d like to understand it better.
Any ideas how Ubuntu computes and assigns these static ip addresses in the private subnet?
A.Jesin says
Hello Stefan, how did you check the IP address ? Using the ifconfig command ?
Check out the following file
/etc/network/interfaces
If you find a line the line “iface eth0 inet dhcp” it means your computer has been configured to request IP address from a DHCP server. DHCP is a protocol which automatically assigns IP addresses to computers in the same physical segment.
I’ll explain briefly how DHCP works
This process is called DORA (Discover Offer Request Acknowledge)
Most broadband modems/routers are configured as DHCP servers, so if you have one of these in your network setup it would’ve assigned IP addresses for all your computers.
You can find out your DHCP server’s IP address by looking into the file
/var/lib/dhcp/dhclient.leases
To learn more about DHCP read the Wikipedia article Dynamic Host Configuration Protocol