How to assign IP address to a linux machine

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.

 

How to assign IP address to a linux machine

 

Method 1:

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

mii-tools

or

ifconfig

or

/sbin/ifconfig

or

ip a

Multiple ways to assign IP address in Redhat / Centos Linux

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.

cd /etc/sysconfig/network-scripts/
vim ifcfg-eth0
TYPE=Ethernet
DEVICE=eth0
HWADDR=00:1f:c6:68:0a:60
ONBOOT=yes
USERCTL=no
NETMASK=255.255.255.0
IPADDR=192.168.1.8
GATEWAY=192.168.1.1

save the configuration file in vi editor using ‘:wq’ or ‘:x’ .
Restart the network service by using the below command

/etc/init.d/network restart
ifconfig

Multiple ways to assign IP address in Redhat / Centos Linux

 

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.

Method 2:

Using ifconfig command we can assign an IP address to an linux machine / server.

Syntax:

#ifconfig <Ethernet-interface> <IP> netmask <subnet> up

How to assign IP address to a linux machine

If you restart the network service the assigned IP will be removed. It can also said as assigning temporary IP.

How to assign IP address to a linux machine

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

ifconfig eth0

Note:

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″.

Method 3:

Using “setup” command we can assign IP address to a linux machine or server.

setup

setup99

Navigate your arrow key to Network Configuration section and press enter

How to assign IP address to a linux machine

Select the specific Ethernet interface and press enter.

/root/Desktop/thetechnicalstuff/IP address/setup2.jpg

Enter the desire IP address, Netmask and default gateway details.

How to assign IP address to a linux machine

Navigate to “Quit” and press enter.

How to assign IP address to a linux machine

Navigate to “Quit” and press enter to exit.

How to assign IP address to a linux machine

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

How to assign IP address to a linux machine

 

Method 4:

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.

How to assign IP address to a linux machine

Type system-config-network-tui from command prompt and press enter. Then follow the procedure given in “Method 3″.

Method 5:

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.

How to assign IP address to a linux machine

Select the Ethernet Interface where you are going to assign IP address

How to assign IP address to a linux machine

Select the Static Option and enter IPaddress, Netmask and Default gateway details.

How to assign IP address to a linux machine

Press “Ok” to proceed further.

How to assign IP address to a linux machine

Network services should be restarted to update the changes.

How to assign IP address to a linux machine

Restart the service and check the IP address using ifconfig command from linux command prompt.

How to assign IP address to a linux machine

Method 6:

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.

Method 7:

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″

Method 8:

Using ifconfig we can assign temporary IP address to an ethernet interface or we can assign Multiple IP address to an single ethernet interface.

Syntax:

#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

How to assign IP address to a linux machine

Note:

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.




Leave a Reply

Your email address will not be published. Required fields are marked *