- Linux Set Up Routing with ip Command
- How to view or display Linux routing table
- How to set a route to the locally connected network eth0 on Linux
- Set a default route
- Delete route from table
- How do I verify routing configurations?
- Linux Set Up Routing with ip command and save it to a configuration file
- How to add a static route on Ubuntu or Debian
- Linux Route Add Command with Examples
- 1) Show the routing table
- 2) Add a new route
- 3) Reject and Deleting a route
- Conclusion
- How to Permanently add Static Route in Linux
- Adding temporary static routes
- Adding permanent static routes
- Conclusion
- 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
Linux Set Up Routing with ip Command
C an you explain the ip command to setup routing on Linux based systems? How do I use the ip command to configure the routing table of the Linux kernel?
The ip command can be used for the following tasks on Linux:
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | Yes |
Requirements | ip command |
Est. reading time | 10m |
- Show / manipulate routing
- Show / manipulate devices
- Policy routing
- Tunnels
How to view or display Linux routing table
Type the following command:
$ ip route show
OR
$ ip route list
Sample Outputs:
Each entry is nothing but an entry in the routing table (Linux kernel routing table). For example, the following line represents the route for the local network. All network packets to a system in the same network are sent directly through the device ra0:
- 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 ➔
Our default route is set via ra0 interface i.e. all network packets that cannot be sent according to the previous entries of the routing table are sent through the gateway defined in this entry i.e 192.168.1.1 is our default gateway.
How to set a route to the locally connected network eth0 on Linux
Type the following command to sent all packets to the local network 192.168.1.0 directly through the device eth0:, enter:
# ip route add 192.168.1.0/24 dev eth0
OR route traffic via 192.168.2.254 gateway for 192.168.2.0/24 network:
# ip route add 192.168.2.0/24 via 192.168.2.254 dev eth0
Set a default route
All network packets that cannot be sent according to the previous entries of the routing table are sent through the following default gateway:
# ip route add default via 192.168.1.254
Delete route from table
Type the following command
# ip route delete 192.168.1.0/24 dev eth0
Let us delete default route too:
# ip route add default via 192.168.1.254 dev eth0
Linux add a default route/static route or delete a route using the ip command.
How do I verify routing configurations?
Use the ping command or host command commands to make sure you can reach to your gateway:
ping Your-Gateway-Ip-Here
ping Your-DNS-Server-IP-Here
ping 192.168.1.254
ping www.cyberciti.biz
host www.cyberciti.biz
Linux Set Up Routing with ip command and save it to a configuration file
All routing settings made with the ip tool (or route command) are lost when you reboot Linux server. See our previous article about configuring static routes in a Debian/Ubuntu or CentOS/Red Hat Enteprise Linux systems.
How to add a static route on Ubuntu or Debian
Here is a sample for eth0 displayed using the cat command cat /etc/network/interfaces
Источник
Linux Route Add Command with Examples
Routing is the way that computers communicate on a local network or over the internet. To communicate over a network, computers need to know which gateway it should send traffic. A gateway could be a router in your network.
The route command is used in Linux to shows and change the ip routing table.
In this tutorial, I will explain how to use the route command to add static routes via gateway to send traffic.
1) Show the routing table
Before editing the routing table, it is good to see the default table with the existing routes on your Linux Kernel.
Use route command with the -n option to displays the current kernel IP routing table. This command helps to identify the locally connected networks with its route.
The following command displays the routing table:
You may also use netstat -nr command to print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.
Display the routing table with netstat command:
Now we have ip command to show routes
2) Add a new route
As we say above, to manipulate the routing table, we use the route add command ( add is the option) to manually indicate a static path that the packets will use through the network. It is used to assign a temporary static route which will change only if the administrator manually modifies the values of the new route.
There are many possibilities for using the route add command.
We should indicate the destination network, subnet mask of the network, and the gateway to use.
Alternatively, you can now use ip route command, syntax as follows:
For example, let’s add a new route network 10.0.0.0/8 and via gateway IP address 192.168.1.1:
Now we can display the routing table using route -n :
You can add netmask option if you want to add netmask value. If not provided then route command finds automatically the value of netmask.
To add a route to a specific host use -host option. We must only indicate the gateway to use.
For example, let’s add a specific host ‘10.0.0.10’ and then display the routing table
In the above examples, the flag column has differents values and that can be incomprehensible.
The possible flags and its definition:
U — route is up
H — target is a host
G — use gateway
R — reinstate route for dynamic routing
D — dynamically installed by daemon or redirect
M — modified from routing daemon or redirect
A — installed by addrconf
C — cache entry
! — reject route
3) Reject and Deleting a route
We can manipulate the routing table not only to add statics routes but also to delete or reject a route that we insert early.
To delete a destination network from the routing table use route del commad:
For example to delete the route to our 10.0.0.0/8 network:
To ban an IP address but it is still on the routing table. Note that we still use the add option parameter but at the end of the command, we have to use reject option. We can conserve our gateway or not but we don’t mention the output interface.
In the following command we will reject our host ‘10.0.0.10’:
Display routing table after the changes:
The routes added using the above commands is not persistent over a reboot, so we have add static routes to the config file to keep it permanent.
To add persistent static routes for any Linux distribution, you can use the generic file /etc/rc.local.
For Ubuntu and Debian use file named ‘/etc/network/interfaces’ and on RHEL/CentOS use ‘/etc/sysconfig/network-scripts/route-ethX’.
Conclusion
In this tutorial, we learned how to add static routes in Linux using route add command. We must note that with that method, the route will not change unless you modify it. It is important to delete routes that we don’t need.
Источник
How to Permanently add Static Route in Linux
Static routing is the process of manually entering the routes to the routing table to reach a particular destination. Basically two commands are used in Linux to add routes. The first command is the old traditional route add and second is the IP route command.
Earlier we learned how to route add in Linux, in this tutorial, here I will show you how to add permanent static routes in Linux distributions such as Centos and Ubuntu.
Adding temporary static routes
In order to add a static route temporarily on your Linux machine, you can use either route or ip command.
You can list the current routing table as follows.
If you want to add a route to the network 198.161.1.0 through gateway 192.168.206.1, you can execute the following command.
Alternatively, you can use ip command as follows:
Adding permanent static routes
In the above section, we saw how to add routes in Linux. But the system will forget those routes on next reboot.
So we have to add routes to config file to make it persistent.
On RHEL or CentOS, you need to modify the interface file in ‘/etc/sysconfig/network-scripts’.
For example, here, we have to add routes on network interface ens192. Hence, the file we need to modify will be ‘/etc/sysconfig/network-scripts/route-ens192’.
The basic syntax of static route in this file is:
target-address — Destination network address.
gateway-address — Router address to reach the destination address.
dev — Indicates which interface to reach the destination, interface naming schemes are eno[1-N], ens[1-N], and or eth[0-N].
So, in order to add the above route in ‘route-ens192’ file, append the following line.
‘10.9.8.0/24 via 10.9.8.100 dev ens192’
Finally, verify that the new routes are visible in the routing table using the following command.
On Ubuntu Linux, if you want to add a permanent static route to your system, you have to add route entry to the ‘/etc/network/interfaces’. Using your favorite editor (nano, vim. ) to open the network interface file and insert the following lines to it.
‘up route add -net 10.9.7.0/24 gw 10.9.8.100 dev ens160’. For example:
In order to make the change take effect, you need to restart the networking service by running:
Verify that the route has been configured correctly:
From Ubuntu 18.04 LTS and higher version, the operating system uses the netplan YAML files for network configuration — it is located in the ‘/etc/netplan’ directory. For example:
In order to add persistent static route, you have to add the following lines to the netplan configuration file ‘/etc/netplan/01-network-manager-all.yaml’
Apply the change by running:
You can verify the route configuration by running command route -n or ip route . Here is the result:
Conclusion
In this tutorial, we learned how to add a permanent route by adding to respective config files in Linux. Thanks for reading and please leave your suggestion in the below comment section.
Источник
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.
Источник