We use cookies to improve our services and provide a better experience. By continuing to use this site, you agree with our Cookies Policy.

Tutorials
More than 400 step-by-step articles to guide you through online project development.
HomeTutorialsOperating SystemsLinuxAdministration

How to Add an IP Address on Linux Operating Systems?

OpenVZ VPS
USA VPS
Windows Cloud Server
Brazil VPS
KVM VPS
Ubuntu VPS
cPanel KVM
Linux KVM Hosting
Linux Cloud Server
Germany Cloud Servers

If you want to add multiple IP addresses on the same network card, you will have to create a virtual network interface. The methods of assigning IP addresses on CentOS/RHEL 6, CentOS/RHEL 7 and Debian/Ubuntu are different. This tutorial will show you how to add virtual network interfaces on these operating systems.

Follow These Steps to Add an IP Adress on Linux Operating Systems

In CentOS 6 operating system network interfaces are located in /etc/sysconfig/network-scripts directory. The default interface for fresh CentOS 6 OS installation is eth0 or ifcfg-eth0.
You can check current network interfaces by typing ifconfig command:

[root@example network-scripts]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:191.101.14.125 Bcast:191.101.14.100 Mask:255.255.255.224
inet6 addr: fe80::20c:29ff:fe28:fd4c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:237 errors:0 dropped:0 overruns:0 frame:0
TX packets:198 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:25429 (24.8 KiB) TX bytes:26910 (26.2 KiB)
Interrupt:18 Base address:0x2000

Go to /etc/sysconfig/network-scripts directory and check current files.

cd /etc/sysconfig/network-scripts
ls –l

Sample output:
add-ip-address-on-linux
So ifcfg-eth0 is your current network interface, now check the content of this interface:

[root@example network-scripts]# cat ifcfg-eth0
DEVICE=”eth0″
BOOTPROTO=static
ONBOOT=yes
TYPE=”Ethernet”
IPADDR=191.101.14.125
NETMASK=255.255.255.224
GATEWAY=191.101.14.100
HWADDR=00:0C:29:28:FD:4C

The most important parts in this file are device and ipaddr, which define network interface name and IP address.
Now you have to copy this file as ifcfg-eth0:0 and change network interface name and IP address:

cp ifcfg-eth0 ifcfg-eth0:0
nano ifcfg-eth0:0

The file ifcfg-eth0:0 must look like:

DEVICE=”eth0:0″
BOOTPROTO=static
ONBOOT=yes
TYPE=”Ethernet”
IPADDR=191.101.14.126
NETMASK=255.255.255.224
GATEWAY=191.101.14.100
HWADDR=00:0C:29:28:FD:4C

TIP: Do not forget to change gateway if new IP address is from different subnet.

Save the file and restart your network service for the changes to take effect:

/etc/init.d/network restart

Check your current network interfaces:

[root@example network-scripts]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:191.101.14.125 Bcast:191.101.14.100 Mask:255.255.255.224
inet6 addr: fe80::20c:29ff:fe28:fd4c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:237 errors:0 dropped:0 overruns:0 frame:0
TX packets:198 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:25429 (24.8 KiB) TX bytes:26910 (26.2 KiB)
Interrupt:18 Base address:0x2000
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:191.101.14.126 Bcast:191.101.14.100 Mask:255.255.255.224
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:18 Base address:0x2000

Also you can assign a range of IP addresses by creating a single file – ifcfg-eth0-range:

cd /etc/sysconfig/network-scripts/
cp ifcfg-eth0 ifcfg-eth0-range
nano ifcfg-eth0-range

Change IPADDR value to IPADDR_START and IPADDR_END. Your range file should look like:

#DEVICE="eth0"
#BOOTPROTO=none
#NM_CONTROLLED="yes"
#ONBOOT=yes
TYPE="Ethernet"
IPADDR_START=191.101.14.126
IPADDR_END=191.101.14.129
IPV6INIT=no
#GATEWAY=172.16.16.100

Save the file and restart the network:

/etc/init.d/network restart

Check your current network configuration:

[root@example network-scripts]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:191.101.14.125 Bcast:191.101.14.100 Mask:255.255.255.224
inet6 addr: fe80::20c:29ff:fe28:fd4c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:237 errors:0 dropped:0 overruns:0 frame:0
TX packets:198 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:25429 (24.8 KiB) TX bytes:26910 (26.2 KiB)
Interrupt:18 Base address:0x2000
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:191.101.14.126 Bcast:191.101.14.100 Mask:255.255.255.224
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:18 Base address:0x2000
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:191.101.14.127 Bcast:191.101.14.100 Mask:255.255.255.224
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:18 Base address:0x2000
eth0:2 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:191.101.14.128 Bcast:191.101.14.100 Mask:255.255.255.224
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:18 Base address:0x2000
eth0:3 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:191.101.14.129 Bcast:191.101.14.100 Mask:255.255.255.224
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:18 Base address:0x2000

Try to ping your newly added IP addreses – they must respond to ping ICMP connections.

Assigning additional IP addresses in CentOS 7 operating system is different than in previous CentOS versions. First, you have to check your current network configuration by running ip addr command:

root@example] # ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:80:63:19 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.150/24 brd 192.168.1.255 scope global enp0s3
valid_lft forever preferred_lft forever

As you can see from the output above, the network card name is enp0s3 with 192.168.1.150 IP address assigned for it. Navigate to /etc/sysconfig/network-scripts directory and find the file which holds your network interface configuration.

cd /etc/sysconfig/network-scripts
ls –l | grep enp0s3

Sample output:

ifcfg-enp0s3

Add your additional IP address line – IPADDR1=”192.168.1.151” – to the configuration file as displayed:

nano /etc/sysconfig/network-scripts/ifcfg-enp0s3
TYPE=”Ethernet”
BOOTPROTO=”none”
DEFROUTE=”yes”
IPV4_FAILURE_FATAL=”no”
IPV6INIT=”yes”
IPV6_AUTOCONF=”yes”
IPV6_DEFROUTE=”yes”
IPV6_FAILURE_FATAL=”no”
NAME=”enp0s3″
UUID=”933cdc9b-b383-4ddd-b219-5a72c69c9cf0″
ONBOOT=”yes”
HWADDR=”08:00:27:3F:AB:68″
IPADDR0=”192.168.1.150″
IPADDR1=”192.168.1.151″
PREFIX0=”24″
GATEWAY0=”192.168.1.1″
DNS1=”192.168.1.1″
IPV6_PEERDNS=”yes”
IPV6_PEERROUTES=”yes”

NOTE: You can add as many IP addresses as you want.

Save the file and restart the network service:

systemctl restart network

Now check if the IP address was added to your network configuration:

ip addr

Sample output:

: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:3f:ab:68 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.150/24 brd 192.168.1.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 192.168.1.151/24 brd 192.168.1.255 scope global secondary enp0s3
valid_lft forever preferred_lft forever

As you see, the IP has been successfully added, so now you can ping your newly added IP address – it will respond to ICMP connections.

Additional IP addresses on Ubuntu/Debian distribution can be added by creating network alias zone in /etc/network/interfaces file. However, first check current network interfaces by typing ifconfig command:

[root@example ]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:191.101.14.125 Bcast:191.101.14.100 Mask:255.255.255.224
inet6 addr: fe80::20c:29ff:fe28:fd4c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:237 errors:0 dropped:0 overruns:0 frame:0
TX packets:198 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:25429 (24.8 KiB) TX bytes:26910 (26.2 KiB)
Interrupt:18 Base address:0x2000

Edit the interfaces file:

nano /etc/network/interfaces

Append the text as follows:

auto eth0:1
iface eth0:1 inet static
name Ethernet alias LAN card
address 191.101.14.126
netmask 255.255.255.254
broadcast 191.101.14.255
network 191.101.14.0

NOTE: You can add as many IP addresses as you want.

Save the file and restart your network:

/etc/init.d/networking restart

Check your network configuration afterwards:

[root@example interfaces]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:191.101.14.125 Bcast:191.101.14.100 Mask:255.255.255.224
inet6 addr: fe80::20c:29ff:fe28:fd4c/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:237 errors:0 dropped:0 overruns:0 frame:0
TX packets:198 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:25429 (24.8 KiB) TX bytes:26910 (26.2 KiB)
Interrupt:18 Base address:0x2000
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:28:FD:4C
inet addr:191.101.14.126 Bcast:191.101.14.100 Mask:255.255.255.224
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

As you see, the IP has been successfully added, so now you can ping your newly added IP address – it will respond to ICMP connections.

KVM VPS
Linux Cloud Server
Linux KVM Hosting
OpenVZ VPS
cPanel KVM
Germany Cloud Servers
Brazil VPS
Ubuntu VPS
Windows Cloud Server
USA VPS
Rate this Tutorial:
No Comments