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
A machine having multiple Ethernet cards will have files named ifcfg-eth1, ifcfg-eth2 and so on. Add or edit the following lines
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.
If you use dpkg to install packages then you’re using a Debian based Linux OS. Using VI editor open the following file.
If you’re using DHCP (which is the default) the following is shown
iface eth0 inet dhcp
To assign an IP address statically delete those lines and enter the following
iface eth0 inet static
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
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
Restarting the interface and verifying the settings
For the changes to apply the interface has to be shutdown and brought up
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
You should get a reply.