- 11 Best Linux route command examples(How to add route in Linux)
- Linux route command examples
- Example 1: How to Check Linux route command version
- Example 2: How to Display routes in Linux
- Example 3: How to display routes in netstat format in Linux
- Example 4: How to display IP instead of hostname in the Routing table
- Example 5: How to Add a route in Linux
- Example 6: How to Reject a Route in Linux
- Example 7: How to add a default gateway using Linux route command
- Example 8: How to Delete a Route using Linux route command
- Example 9: How to Add a host in Linux
- Example 10: How to Delete a default gateway
- Example 11: How to Check other route options
- route command in Linux with Examples
- Installing route Command
- Working with route command
- Linux route Add Command Examples
- Display your current routing table
- Linux route Add Command Examples
- Linux add a default route using route command
- Linux add a default gateway (route) using ip command
- Verify newly added route ip in the Linux kernel routing table
- Verify new route
- How do I make routing changes persistent across reboots?
- RHEL/CentOS/Fedora/Scientific Linux persistent routing configuration
- Debian / Ubuntu Linux persistence static routing configuration
- Generic method to add persistent static routing on Linux
- Summing up
11 Best Linux route command examples(How to add route in Linux)
Table of Contents
In this article, I will take you through 11 Best Linux route command examples. Linux route command is used to manage Kernel IP routing tables. When we say route then we are only talking about static routes which can be manipulated using route command and not the dynamic routes. You might be frequently using this command to create/delete/manage static route through your network interfaces. We will try to see the examples of How to add route in Linux.
Linux route command examples
Example 1: How to Check Linux route command version
If you want to check the route command version then you need to use route -V command as shown below. As you can from below output, route command is a part of net-tools package version 2.10-alpha . You might want to check How to Install route command on Linux (RedHat/CentOS 7/8) Using 5 Easy Steps to know more about route command installation in Linux.
-V : display Linux route command version and exit
Example 2: How to Display routes in Linux
You can simply check your current route by running Linux route command as shown below. From the below route, you can see that if any packets goes through this System has a destination in the IP address range from 192.168.0.0 to 192.168.0.255 will be routed to 0.0.0.0 gateway which is our local system and hence will not be routed anywhere else.
If any of the packets destination is out of the above range, then it will be forwarded to default gateway which is again 0.0.0.0 . Hence all the packets will end here in the System itself.
Example 3: How to display routes in netstat format in Linux
If you want to check the routing table in netstat format then you need to use -e option with Linux route command as shown below.
-e : use netstat format for displaying the routing table. More info on Linux route command Man Page.
Example 4: How to display IP instead of hostname in the Routing table
By default Linux route command output will show hostname but if you want to show the IP instead of hostname then you need to use route -n command as shown below.
-n : show numerical addresses instead of trying to determine symbolic host names.
Example 5: How to Add a route in Linux
If you want to add a route then you need to use below Linux route command. In this example, we are adding a route to 192.16.36.0 through enp0s3 network interface as shown below.
add : add a new route.
-net : the target is a network.
After adding the route you can check again the route by using route -n command. From the below output, you can check the route is added now.
Example 6: How to Reject a Route in Linux
If you want to add a route to rejection list then you need to use below route command. In this example, we are adding 2.2.2.0 route to the rejection list which will force any lookup to fail for this route. It means any of packets destined for an IP address range from 2.2.2.0 to 2.2.2.255 will not be forwarded anywhere else and will hence be terminated here.
reject : install a blocking route, which will force a route lookup to fail. More info on Linux route command Man Page.
Now check again if the above route is added in the list or not using route -n command.
Example 7: How to add a default gateway using Linux route command
If you want to add a default gateway then you need to use below route command. In this example, we are adding a default gateway 177.16.72.1 in our System to forward our packets through this.
add : add a new route.
gw : route packets via a gateway.
Example 8: How to Delete a Route using Linux route command
You can also delete any route using route command. For that you need to first identify the route which needs to be deleted using route -n command.
Now delete the route by using route delete command as shown in the below output. In this example, we have identified to remove any routing to destination 192.16.36.0-192.16.36.255 address range. So if any packets destined to this IP Range will not get forwarded to any gateway as the route entry is removed now.
delete : delete a route.
-net : the target is a network.
netmask : when adding a network route, the netmask to be used
dev : associate the rule with the device
enp0s3 : network interface
Now check if the route is removed or not by using route -n command as shown in the below output.
Example 9: How to Add a host in Linux
If you want to add a host then you need to use below Linux route command. In this example, we are adding a host 12.123.0.10 through enp0s3 network interface. Below command means that any packets destined for host 12.123.0.10 will now get forwarded to a gateway 192.168.1.1 through network interface enp0s3 .
-host : the target is a host.
Now you can again run route -n command and check if the route is added or not.
Example 10: How to Delete a default gateway
If you want to delete your default gateway then you need to use below Linux route command. In this example, we are deleting our default gateway 177.16.72.1 which we previously added.
del : delete a route.
Example 11: How to Check other route options
If you want to check all the other available options with Linux route command then you need to use route —help command as shown below.
—help : display all the available options.
Источник
route command in Linux with Examples
route command in Linux is used when you want to work with the IP/kernel routing table. It is mainly used to set up static routes to specific hosts or networks via an interface. It is used for showing or update the IP/kernel routing table.
Installing route Command
Many Linux distributions do not have route command pre-installed. To install it use the following commands as per your Linux distribution.
In case of Debian/Ubuntu
In case of CentOS/RedHat
In case of Fedora OS
Working with route command
1. To display the IP/kernel routing table.
It displays the routing table entries.
2. To display routing table in full numeric form.
It is even useful when you have to determine why the route to nameserver has even vanished.
3. To add a default gateway.
This assigns a gateway address on which all the packets that do not belong to the network are forwarded.
Note: In this case the, We wish to choose 169.254.0.0 as the default gateway. You may choose as per your need.
4. To list kernel’s routing cache information.
To route the packets faster, Kernel maintains this routing cache information. The above command will print the cache information. In this case, the cache information is maintained.
5. To reject routing to a particular host or network.
Now if you will ping to the above-mentioned IP it will display “Network is unreachable”.
6. To get details of the kernel/IP routing table using ip command.
This will give the details of the kernel/IP routing table and in this case, we have used IP command.
7. To delete the default gateway.
Caution: This may lead to some malfunctioning of internet. Keep a note of your default gateway before proceeding with the command.
This will remove the default gateway.
8. To get the details of the local table with destination addresses assigned to the localhost.
This will print the details of the local table.
9. To get output related to IPv4.
This will only display the entries with ipv4.
10. To get output related to IPv6.
This will only display the entries with ipv6.
Источник
Linux route Add Command Examples
I am a new Linux user. How do I add a new or default gateway using route command on Linux operating systems? How can I use route command to show or set a new route on Linux based server or desktop system?
In Linux and Unix-like system, a gateway is nothing but device that connects two networks. Often it is called a router or gateway. In most cases your ISP’s modem act as a default router or gateway. You can use any one of the following tool to add, display, delete Linux kernel routing table:
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | Yes |
Requirements | Linux with ip or route command |
Est. reading time | 3 mintues |
- route command : show / manipulate the IP routing table on Linux.
- ip command : show / manipulate routing, devices, policy routing and tunnels on Linux.
Display your current routing table
Open the Terminal or login to server using ssh/console. Type the following command to display routing table:
# route
OR
# route -n
Sample outputs:
Fig.01: Display routing table using route command
Fig.02: ip command in action
Linux route Add Command Examples
I am going to show you both ip and route command. Most modern Linux distro recommend and use the ip command for setting or displaying default gateway IP address on Linux. Let us see some examples.
Linux add a default route using route command
Route all traffic via 192.168.1.254 gateway connected via eth0 network interface:
# route add default gw 192.168.1.254 eth0
Linux add a default gateway (route) using ip command
Route all traffic via 192.168.1.254 gateway connected via eth0 network interface:
# ip route add 192.168.1.0/24 dev eth0
Verify newly added route ip in the Linux kernel routing table
To verify new routing table, enter:
# ip route list
OR
# route -n
Verify new route
Use the ping command to verify connectivity to your router or external network:
# ping your-router-ip-here
# ping your-ISPs-Gateway-ip-here
# ping 192.168.1.254
# ping www.cyberciti.biz
You should able to ping public IP address too:
ping 1.1.1.1
ping 8.8.8.8
And use the dig command or host command to resolve domain names provided that /etc/resolv.conf configured with correct DNS server names:
host www.cyberciti.biz
host google.com
dig www.cyberciti.biz
How do I make routing changes persistent across reboots?
To make route entry persistent in the Linux kernel routing table, you need to modify config file as per your Linux distributions.
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
RHEL/CentOS/Fedora/Scientific Linux persistent routing configuration
Edit /etc/sysconfig/network and set default gateway IP address:
# vi /etc/sysconfig/network
Sample outputs:
You can add additional static route for eth0 by editing /etc/sysconfig/network-scripts/route-eth0 file as follows:
The above config sets static routing for network 10.0.0.0/8 via 10.9.38.65 router.
Debian / Ubuntu Linux persistence static routing configuration
Edit /etc/network/interfaces file, enter:
# vi /etc/network/interfaces
Append the following in eth0 section:
Save and close the file.
Generic method to add persistent static routing on Linux
The following method works with almost all Linux distributions. However, on systemd based Linux distro, see how to enable /etc/rc.local with systemd tutorial for more info.
Edit /etc/rc.d/rc.local or /etc/rc.local , enter
# vi /etc/rc.local
Append the following line:
Summing up
You learned how to add routing on Linux. See
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
Changes to survive a reboot can be set in
/etc/sysconfig/networking/devices/route-ethX
Other profiles for switching between different configurations can be found in
/etc/sysconfig/networking/profiles/default/route-ethX
I have a problem , my default route will go away when i reboot my system, regarding to this matter that all info was issued under interface network-script.
Please advice,
Momo.
add configuration file
Can you please let me know how to add the route command in Linux.
Hi
add the route command in linux
# route add -net 10.10.1.1/24 ethx
Alternate is by ip command
# ip route add 10.10.2.2/24 ethx
after that configure gateway,netmask and ip in route-ethx file located in /etc/sysconfig/network-scripts dir.
it worked ..thanks
Adding the route command in Linux
Route traffic via 192.168.0.5 gateway destined to 192.168.1.16 :
route add -net 192.168.1.16 netmask 255.255.255.240 gw 192.168.0.5
This will handle all the traffice from your client systems destined to the 192.168.1.16 subnet.
The resources that will be accessed are in the range 192.168.1.16 – 192.168.1.30
I have tried it on Suse Linux, and i belive other flavors are similar or close to this .
Thank you very much this works
To add default route for a host:
#route add 192.168.0.1 gw 10.0.0.1
“where 192.168.0.1 is destination host, gw commad to specify gateway and 10.0.0.1 is the gateway to destination host. (Test on Feroda 11)”
Another way to add route at startup (testing on CentOS)
edit file /etc/rc.d/rc.local (add to end of file your route):
/sbin/route add -net 192.168.11.0 netmask 255.255.255.0 gw 10.0.0.1
After reboot check your route table:
/sbin/route
Thanks Alex. You saved my day. On CentOS this was needed
i want to add below command in Linux server.but here it is showing error
[root@plnotxsmsc01 rc.d]# route add -net 10.202.11.0 gw 10.102.13.241
bash: route: command not found
[root@plnotxsmsc01 rc.d]#
[root@plnotxsmsc01 rc.d]# uname -a
Linux plnotxsmsc01 2.6.18-128.el5xen #1 SMP Wed Dec 17 12:01:40 EST 2008 x86_64 x86_64 x86_64 GNU/Linux
[root@plnotxsmsc01 rc.d]#
please try /sbin/route or locate where the route is
route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1
The routing information above is not persistent across reboots. After a reboot, the routing information will be lost and you need to add them in again.
To make the routing information persistent, add the “route add” line as seen above into the /etc/rc.local file.
Sample /etc/rc.local file.
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don’t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1
Yes Alex, your comment was also what I needed.
Thank you so much for that!
how to find remote systems gateway with out login
i want to add below command in Linux server.but here it is showing error
[root@zmocmp1 sbin]# route add -net 10.26.66.0 netmask 255.255.255.192 gw 0.0.0.0
SIOCADDRT: Invalid argument
[root@zmocmp1 sbin]#
and how to make routing information persistent,
please assign valid gateway it will work….
Hi,
I want to do ping6 to another linux machine..I added inet6 address in both the linux machine in the main LAN interface..bt ping6 is not wrking. “Destination Unreachable is coming”. So,For that when i tried to add route to ipv6 thorough a gateway,using the command ” /sbin/route -A inet6 add 2000::/64 gw 2000:db8:0:bbbb::1 ” is is telling invalid command..pls help me with this and how can i rectify it.
Muchas gracias me ayudo bastante, saludos desde México…
awsome explanation!! love it
check the routes #route
route add -net netmask gw
after add permanent route entry
create a file vi /etc/sysconfig/network-scripts/route-eth0
after add route entry this format
ADDRESS0=
NETMASK0=
GATEWAY0=
ADDRESS1=
NETMASK1=
GATEWAY1=
ADDRESS2=
NETMASK2=
GATEWAY2=
En los sistemas RHEL/CentOS/Fedora/Scientific Linux la configuración de la ruta por defecto (default gateway) se guarda en /etc/sysconfig/network y las rutas estáticas en /etc/sysconfig/network-scripts/route-ethX (siendo X el número de la placa de red, verbigracia eth0 para la primer placa red).
En los sistemas Debian / Ubuntu Linux la configuración de la ruta por defecto (default gateway) se guarda en /etc/network/interfaces y las rutas estáticas se pueden agregar bajo a sección eth0 o bien en el archivo /etc/rc.local junto con las definiciones de ejecuciones de archivos locales al inicio del sistema.
Lic. Matias Colli
Perito Judicial en Informática
RHCSA
how to add the route in lubuntu
route add 172.18.0.0 mask 255.255.0.0 192.168.1.100 -p also how to check the route table. the application needs IE siebel framework. need solution
Источник