ip route add network command for Linux explained

How do I use ip route networking command to add a static route on Linux operating systems? Can you explain how to use the ip command?

Introduction: The ip command is used to assign an address to a network interface and/or configure network interface parameters on Linux operating systems. This command replaces old good and now deprecated ifconfig command on modern Linux distributions. A static route is nothing but a way of specifying traffic that must not go through the default gateway. One can use the ip command for adding a static route to a different network that cannot be accessed through your default gateway. For example, VPN gateway or VLNAN might need to use the ip command.


ip route add network command examples

The syntax is pretty simple:
ip route add {NETWORK/MASK} via {GATEWAYIP}
ip route add {NETWORK/MASK} dev {DEVICE}
ip route add default {NETWORK/MASK} dev {DEVICE}
ip route add default {NETWORK/MASK} via {GATEWAYIP}

Add a static route on Linux

You must login as root user with the help of su command or sudo command:
$ su -
$ sudo -i
Once become a root user, setup a temporary route using the ip command:
# ip route add via dev eth0
Verify it:
# ip r
Here is another example where I am setting up route for my VPN gateway:
# ip link set dev tun0 up mtu 1500
# ip addr add dev tun0 broadcast
# ip route add via
# ip route add via
# ip route add via

Again view route with the ip command:
# ip r
Linux ip route add network command examples

Warning: Do not stop networking service over ssh session.

How to add a permanent static route using ip command on Linux

Edit config file such as /etc/sysconfig/network-scripts/route-eth0 on a CentOS/RHEL/Fedora Linux for interface eth0 using a text editor such as nano command or vim command:
# vim /etc/sysconfig/network-scripts/route-eth0
Append the following text: via dev eth0
Save and exit (close) the file in a vim text editor. Finally, restart your network service on a CentOS/RHEL/Fedora Linux so they take effect:
# systemctl restart network.service

Restarting network service on a CentOS/RHEL/Fedora Linux

Restarting network service on a CentOS/RHEL/Fedora Linux

Test new routing with the help of ping command or ip command:
# ping
# ip r

For more info see:

A note about ip command and persistence static routing on a Debian/Ubuntu

Edit your /etc/network/interfaces file for say eth0:
# vi /etc/network/interfaces
Update it as follows:

auto eth0
iface eth0 inet static
## static ip config START ##
up /sbin/ip route add via dev eth0
down /sbin/ip route delete via dev eth0
## static ip config END ##

Restart networking service when using a Debian or Ubuntu Linux
# systemctl restart networking


This page showed you how to configure static routes using the ip command line options. For more info see ip command man page.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

1 comment… add one
  • JP Jun 13, 2017 @ 8:50

    Thanks I wanted to add a temporary static route for VPN server. This solved my problems using “ip route add” command. Love from Mexico.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.