Debian Network SERVICES Ubuntu

How to Assign IP Addresses On-Fly on Debian Based Linux Distributions

How to Assign IP Addresses On-Fly on Debian Based Linux Distributions
5 (100%) 2 vote[s]

This article will guide you on how you can manipulate IP Addresses and routes using the Command Line on Debian-based Linux Distributions, especially on systems without and Graphical User Interface. The standard commands used to manage network configurations on almost every Linux system are: ifconfig, ip and route. While this commands are used to change network settings on-fly, with instant effect over network interfaces, their modifications are not permanent and can’t survive after a system reboot or an interface reset. In order to make the network settings permanent you must manually edit the interfaces file /etc/network/interfaces and use the static method on the interface you want to assign a static IP Address.

PART ONE – Assign IP Settings On-Fly

– List Network Interfaces

In order to temporary and instantly change network settings (IP, Netmask, Gateway) first you must issue one of the following commands to get a list of all your network interfaces:

ifconfig -a
ip link show

list network interfaces

From this commands output you can easily spot the relevant information about every NIC, such as the name of the network card and their state, if they are actually active or down. As you can see eth0 interface is down which is explicitly shown by the ip command. The missing inet addr value on eth0 ifconfig command output suggest that this interface is also inactive.

– Assign IP Addresses with ifconfig command

ifconfig command is a powerful network utility belonging to net-tools package which allows to assign or change IP addresses on fly, or even create an virtual network interface instantly. In order to add IP settings with ifconfig command use the following synax:

ifconfig <name of the interface> <ip address> netmask <netmask value> brodcast <brodcast value>

sudo ifconfig eth0 netmask brodcast

– Create Virtual Network Interface with ifconfig Command

To create a Vitual Network Interface or an Alias Interface use the following syntax:

sudo ifconfig eth0:0 netmask

virtual network interface
– Manipulate Routes with route Command

ifconfig command works hand-in-hand with the route command, which allows us to add a specific default gateway for an interface in order to access other networks outside our local network. Use the following syntax to add a default gateway on an interface:

sudo route add default gw dev eth0:0

After the gateway has been added issue the following commands to list the kernel IP routing table:

route -n
netstat -r

routing table

To add a route to a specific network only use the following command syntax example:

sudo route add -net gw dev eth0:0

To delete a route issue the following command:

sudo route del default gw dev eth0:0
sudo route del -net gw dev eth0:0

– Assign IP Addresses with IP command

IP Command is a new and powerful network command which includes several network utilities within and tends to replace the old and deprecated ifconfig command.

To list network interfaces IP Addresses issue the following IP command syntax:

ip addr show

ip addr show

– Create Virtual Network Interface with IP command

In order to create a virtual network interface and assign an IP address the same time with IP command issue the next syntax:

sudo ip addr add dev eth0 label eth0:0

To delete the address along with the virtual network interface run the following IP syntax:

sudo ip addr del dev eth0 label eth0:0

– Manipulate routes with IP command

To get a list of the Kernel routing table using IP command issue the following syntax:

ip route show

ip route show

To add a default gateway use the following example syntax:

sudo ip route add default via dev eth0:0

To replace a default gateway:

sudo ip route replace default via dev eth0:0

To add a gateway to a specific network use the following syntax:

sudo ip route add via dev eth0:0

To delete a default gateway or a specific gateway to a destination network issue the below commands:

sudo ip route del default via dev eth0:0
sudo ip route del via dev eth0:0

To delete IP settings over an network interface issue the following command:

ip addr flush eth0

– Activate and Deactivate Network Interfaces

If you want to disable a network card on-fly and keep the previous setting when you will enable it again run the below command:

sudo ip link set eth0 down

To activate the same interface with its previous settings run the same command, but replace down statement with up:

sudo ip link set eth0 up

To disable a network interface and delete all previous configurations made with ip or ifconfig network utilities issue the following command:

sudo ifdown eth0

To enable it again with its permanent settings then issue the next command:

sudo ifup eth0

PART TWO – Activate Kernel Routing (IP Forwarding)

One of the most powerful abilities of Linux systems stands on kernel capabilities to move packages from one interface to another or forward them between networks, in other words to route traffic and act as a router. To achieve this goal your machine must own at least two network interfaces connected on two different networks and the kernel IP Forwarding function activated.

IP Forwarding is disabled by default on all Debian based distributions. To check the status of this function issue the following commands:

cat /proc/sys/net/ipv4/ip_forward
sysctl net.ipv4.ip_forward

If the returned value of this commands is 0 then your system doesn’t act as a router.

To enable IP Forward on-fly run the following command:

sudo sh -c "echo ‘1’ > /proc/sys/net/ipv4/ip_forward"

In order to permanently activate IP Forwarding, edit /etc/sysctl.conf file with an text editor, uncomment the following line net.ipv4.ip_forward = 1 and apply changes using sysctl system utility:

sudo nano /etc/sysctl.conf

ip forward

sudo sysctl -p

This are just a few network settings that you can tamper with from command line, especially on servers, settings which take place instantly without the need to reboot the system or modify important network files on your machine.You don’t have to worry in case you are misconfiguring network settings by using this commands because their changes are not permanent and you can always revert changes by issuing sudo ifdown ethX && sudo ifup ethX commands or reboot the system, but pay extra attention on running this types of commands remotely and the only way you can access your server is through SSH protocol.

About the author


Leave a Comment