How to assign IP address to a linux machine
Question: How to assign IP address to an linux server or linux machine.
Answer: There are many ways to assign an IP address in a linux machine. The multiple methods are explained in this blog with an example.
Assigning IP address to an linux machine is the very basic task to an linux admin. It can be accomplished by multiple different ways. I have explained all the methods in this blog
OK! No more waiting lets start learn how to assign an IP address to an Redhat / Centos / Fedora linux box.
The first method is the correct way of assigning IP address. Navigate to IP address configuration file. The configuration file is stored in the directory “/etc/sysconfig/network-scripts/”. The configuration file is “/etc/sysconfig/network-scripts/ifcfg-eth0″. In my case the ethernet interface name is eth0 if your ethernet interface name is eth1 means then your configuration file would be like “/etc/sysconfig/network-scripts/ifcfg-eth1″.
How to find my ethernet interface name in my linux machine. To find your ethernet interface name fire the below command as root user
All the above four command will show you Ethernet interface name. Whether it is eth0 or eth1 or eth2 and so on. OK! Lets do it lets assign IP address. To assign an IP address follow the below steps.
save the configuration file in vi editor using ‘:wq’ or ‘:x’ .
Restart the network service by using the below command
The above command will display the IP address which you have assigned. Lets explore about some of the variables in IP address configuration file.
TYPE=Ethernet, Which tells that it belongs to type ethernet
DEVICE=eth0, Which tells that the ethernet device is specific for eth0
HWADDR=00:1f:c6:68:0a:60, Which tells that the MAC address of machine.
ONBOOT=yes, It tells that when the network service is restarted IP address will assign automatically. If the network service is restarted with ONBOOT=NO then IP address will not be assign automatically. The ONBOOT variable is equals to “ifup” and “ifdown” command in linux. The ONBOOT=yes is equals to ifup=eth0 command and the ONBOOT=no is equals to ifdown=eth0.
USERCTL=no, It means local user are not allowed to change the IP address.
NETMASK=255.255.255.0, It means subnet mask is /24 or 255.255.255.0 network.
IPADDRESS=192.168.1.8, This is your IP address you are going to assign.
GATEWAY=192.168.1.1, This is the single point exit of all the packates which is generated from IP address 192.168.1.8.
Using ifconfig command we can assign an IP address to an linux machine / server.
#ifconfig <Ethernet-interface> <IP> netmask <subnet> up
If you restart the network service the assigned IP will be removed. It can also said as assigning temporary IP.
To assign IP address using ifconfig below command
ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
To view the IP address use ifconfig command
If you restart the network service (/etc/init.d/network restart) the IP wont be available since because the new IP address details are not update in file (/etc/sysconfig/network-scripts/ifcfg-eth0). To update IP address details in file (/etc/sysconfig/network-scripts/ifcfg-eth0) use the “Method 1″.
Using “setup” command we can assign IP address to a linux machine or server.
Navigate your arrow key to Network Configuration section and press enter
Select the specific Ethernet interface and press enter.
Enter the desire IP address, Netmask and default gateway details.
Navigate to “Quit” and press enter.
Navigate to “Quit” and press enter to exit.
After assigning IP address to make to alive restart the network services(/etc/init.d/network restart) and check the IP address using ifconfig command
Using “system-config-network-tui” we assign IP address to a linux machine or server. system-config-network-tui act’s same as like “setup” command
only the command differs.
Type system-config-network-tui from command prompt and press enter. Then follow the procedure given in “Method 3″.
We can use “system-config-network” command to assign IP address in linux. system-config-network is used to configure IP address in GUI . Let’s see of how to assign IP address in GUI using screen shots.
Select the Ethernet Interface where you are going to assign IP address
Select the Static Option and enter IPaddress, Netmask and Default gateway details.
Press “Ok” to proceed further.
Network services should be restarted to update the changes.
Restart the service and check the IP address using ifconfig command from linux command prompt.
The “neat-tui” command is used to assign the IP address in GUI. All the steps are same as like “Method 5″ except the command neat-tui.
We can assign IP address graphically in once more method. In top of the screen “Menu Lay Out” will be there in that go to System —–> Administration —–> Netwok. Then all the steps are same as “Method 5″
Using ifconfig we can assign temporary IP address to an ethernet interface or we can assign Multiple IP address to an single ethernet interface.
#ifconfig <Ethernet-interface:0> <IP> netmask <subnet> up
Using the below command we can assign multiple IP address to an single ethernet interface
ifconfig eth0:1 192.168.1.20 netmask 255.255.255.0 up
ifconfig eth0:2 192.168.1.21 netmask 255.255.255.0 up
If you restart the network service the eth0:1 and eth0:2 IP address will be removed. To make it permanent add the IP address, netmask and gateway details in file /etc/sysconfig/network-scripts/ifcfg-eth0.