HostOnNet Blog

Add IP in CentOS Server via Command Line

Looking for Linux Server Admin or WordPress Expert? We can help.

I installed CentOS 7 on a Hyper-V server. I want to manually assign IP to the server. This is done by editing file

/etc/sysconfig/network-scripts/ifcfg-eth0

The file name vary depending on the network interface you have.

Instead of editing the file via GUI, i prefer SSH in the server, then edit the file. For this, i assigned IP address to the server with command

ip addr add SERVER_IP_HERE dev INTERFACE_NAME

Example

ip addr add 198.204.249.86 dev eth0

Here 198.204.249.86 is IP assigned to the server. eth0 is name of network interface.

Next we need to add route. To route traffic to internet, we need a gateway. In our cause server provider given IP 198.204.249.81 as gateway. To add route, run

ip route add GATEWAY_IP_ADDR dev INTERFACE_NAME
ip route default via GATEWAY_IP_ADDR dev INTERFACE_NAME

Example

ip route add 198.204.249.81 dev eth0
ip route add default via 198.204.249.81 dev eth0

Now i am able to connect to the server with its IP.

CentOS 7

On CentOS 7, there is some changes, when you add IP to interface, use CIDR notation, for example

ip addr add IP_HERE/32 dev INTERFACE_NAME

When you add default route, use following command

ip route add 0.0.0.0/0 dev INTERFACE_NAME

Posted in Linux

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.