- How to remove all default gateways
- proper syntax to delete default route for a particular interface?
- How to Add and Delete Static Route in Linux using IP Command
- Viewing the existing routing table
- Adding a static route using IP command
- Permanently adding static route (RHEL, Fedora, CentOS)
- Permanently adding static route (Ubuntu / Debian)
- Deleting a static route
- How To Delete Route In Ubuntu Linux?
- List Existing Routes
- Remove Specific Route
- Check
- How To Delete Route In Ubuntu Linux? Infografic
How to remove all default gateways
I’m running a custom built Linux machine, so not all Linux commands are available.
I execute network related commands, so I need to set a default gateway right before I run my command, then remove that gateway immediately afterward. To do that I run all my commands in one line:
/sbin/route add default gw 10.10.10.10;my command;/sbin/route del default gw 10.10.10.10;
The problem is, for some reason I once found 2 default gateways on the same machine which caused all my commands to fail because even if I set my default gateway before running my test, it is still messed up and can’t run my test. So is there a way to remove ALL default gateways in one command ? I have a large amount of machines that are increasing and it won’t be practical to plant a script on every machine. I need a command as simple as the following:
/sbin/route del all default;set my default gw;mycommand;/sbin/route del all default;
All I have found so far is a command to remove a default gateway but not all of them /sbin/route del default which won’t work for me. /sbin/route help displays the following
/sbin/route --help Usage: route [] Edit the kernel's routing tables Options: -n Don't resolve names -e Display other/more information -A inet Select address family
proper syntax to delete default route for a particular interface?
On this client, I have started an OpenVPN client (after previously starting an OpenVPN server in the cloud), logged into a remote-access website, and used that site’s web UI to connect to a (proprietary) F5 SSL VPN (which I want to tunnel through the OpenVPN). (More details on the design goal here and the problem configuration here.) This produces
me@client:~$ date ; sudo ifconfig Thu Jan 22 11:48:43 EST 2015 eth0 Link encap:Ethernet HWaddr inet addr:192.168.1.142 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:10224715 errors:0 dropped:0 overruns:0 frame:0 TX packets:6011530 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:12886933501 (12.0 GiB) TX bytes:677423768 (646.0 MiB) Interrupt:20 Memory:f2600000-f2620000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:497 errors:0 dropped:0 overruns:0 frame:0 TX packets:497 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:51273 (50.0 KiB) TX bytes:51273 (50.0 KiB) # Note I get slightly different IP#s for interface=ppp0 each time I run this scenario. ppp0 Link encap:Point-to-Point Protocol inet addr:10.144.15.234 P-t-P:10.144.0.1 Mask:255.255.255.255 UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1 RX packets:6 errors:0 dropped:0 overruns:0 frame:0 TX packets:18 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:3 RX bytes:56 (56.0 B) TX bytes:2418 (2.3 KiB) tun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 inet addr:10.8.0.6 P-t-P:10.8.0.5 Mask:255.255.255.255 UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1 RX packets:4 errors:0 dropped:0 overruns:0 frame:0 TX packets:4 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:100 RX bytes:304 (304.0 B) TX bytes:304 (304.0 B) me@client:~$ date ; sudo route -n Thu Jan 22 11:48:48 EST 2015 Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.144.15.100 128.0.0.0 UG 1 0 0 ppp0 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0 10.144.0.1 0.0.0.0 255.255.255.255 UH 0 0 0 ppp0 128.0.0.0 10.144.15.100 128.0.0.0 UG 1 0 0 ppp0 134.67.15.30 10.8.0.5 255.255.255.255 UGH 1 0 0 tun0
Once at that point, I’m directed (IIUC) to delete the default route being set by the F5VPN (on interface= ppp0 , for debugging. Furthermore, I need to do this quickly, because (and this is the problem with the current misconfiguration) the misconfiguration causes the OpenVPN tunnel to fail quickly, which breaks the situation I want to debug. Hence it is quite infuriating that I cannot seem to find the correct route syntax to do this:
me@client:~$ sudo route del default ppp0 SIOCDELRT: No such process me@client:~$ sudo route del default dev ppp0 SIOCDELRT: No such process me@client:~$ sudo route del -net default dev ppp0 SIOCDELRT: No such process me@client:~$ sudo route del -net default gw 10.144.15.234 dev ppp0 SIOCDELRT: No such process me@client:~$ sudo route del -net default netmask 128.0.0.0 gw 10.144.15.234 dev ppp0 SIOCDELRT: No such process me@client:~$ sudo route del -net default gw 0.0.0.0 dev ppp0 SIOCDELRT: No such process me@client:~$ sudo route del -net default netmask 255.255.255.255 gw 0.0.0.0 dev ppp0 SIOCDELRT: No such process
info route is not helping, nor are my websearches finding helpful doc. (Both cases probably due to my lack of networking experience.) What am I doing wrong?
How to Add and Delete Static Route in Linux using IP Command
Part of the skill set for any Linux user, and particularly a systems administrator, is the ability to perform some network tweaks on a Linux system. This includes adding and deleting routes to enable the system to communicate with other systems o a local network. In this guide, we explore exactly how you can go about adding and deleting routes on a Linux system.
Viewing the existing routing table
Before we embark on adding or deleting routes, it’s prudent to check the existing default routes on a system. To do so, simply launch your terminal and issue the command:
$ ip route show Or $ ip route list
Similar statistics can be displayed using route command,
Also, you can use the good old netstat command, which is usually used for printing interface statistics as well as the routing table to achieve the same result.
With the default routing statistics in mind, let’s now move a step further and add some routes to our system.
Adding a static route using IP command
Suppose you want to take a backup of a Linux machine and push the backup file to another backup server in the subnet 10.0.2.0/24 . However, for one reason or the other, you cannot reach the backup server via the default gateway. In this case, you will have to create a new route for backup server subnet via another IP, say 192.168.43.223 via the interface enp0s3 .
The command for this will be
$ sudo ip route add 10.0.2.0/24 via 192.168.43.223 dev enp0s3
- 10.0.2.0 -> is the network you want to connect to
- /24 -> is the subnet mask
- 192.168.43.223 -> is the IP through which we will reach the server
- enp0s3 -> is the network interface
You can confirm whether new static route add been in route table using “ip route show” command.
To add the specific IP of the backup server, say 10.0.2.15 run the command:
$ sudo ip route add 10.0.2.15 via 192.168.43.223 dev enp0s3
Once again, you can check the routing changes to see if the changes exist using the ip route show command:
Permanently adding static route (RHEL, Fedora, CentOS)
The routes we have just added are temporary and will not survive a reboot. To make the routes persistent, you need to manually add them.
In the /etc/sysconfig/network-scripts/ directory, create an interface file route-interface where the interface attribute is your network interface name. In our case, this will be route-enp0s3 .
$ vim /etc/sysconfig/network-scripts/route-enps03
Next, we will add the routes as shown:
10.0.2.0/32 via 192.168.43.1 10.0.2.15 via 192.168.43.1
Save the file and exit. Then restart NetworkManager Service
$ sudo systemctl restart NetworkManager
Permanently adding static route (Ubuntu / Debian)
For Debian distributions, edit the file /etc/network/interfaces
$ sudo vim /etc/network/interfaces
Append the following line:
up route add -net 10.0.2.0 netmask 255.255.255.0 gw 192.168.43.1 dev enp0s3
Save and exit the file. Finally, for the changes to come into effect, run below commands
$ sudo ifdown enp0s3 && sudo ifup enp0s3
Deleting a static route
To delete a specific route, use the ip route del command. For example, to remove the route address we just added, run the command:
$ sudo ip route del 10.0.2.0/24 via 192.168.43.223 dev enp0s3
To delete a single IP route in a subnet run the command
$ sudo ip route del 10.0.2.15 via 192.168.43.223 dev enp0s3
To delete default route run:
$ sudo ip route del default
To add a default route run below ‘ip route add’ command,
$ sudo ip route add default via dev interface
$ sudo ip route add default via 192.168.43.1 dev eth0
We hope that this tutorial was informative and provided you with insights into how you can go about adding and deleting static route in Linux.
Also Read : 12 ip Command Examples for Linux Users
How To Delete Route In Ubuntu Linux?
I have some route in my routing table. But I want to delete one route from routing table. How can accomplish this?
List Existing Routes
To get detailed information about route that will be removed we list the existing routes in our system.
$ sudo ip route show default via 192.168.122.1 dev ens3 10.0.3.0/24 dev lxcbr0 proto kernel scope link src 10.0.3.1 172.16.0.0/24 via 192.168.122.1 dev ens3 192.168.122.0/24 dev ens3 proto kernel scope link src 192.168.122.211
Remove Specific Route
This command can be run all modern Linux distributions like Kali, Debian, Ubuntu, Fedora, CentOS. We remove the route by giving specific details about route like below. We will use ip route del command and provide related parameters.
$ sudo ip route del 172.16.0.0/24 via 192.168.122.1 dev ens3
- ip route del is the command issues for removal
- 172.16.0.0/24 via 192.168.122.1 dev ens3 is our route to be removed. We give the full detail route so there will no space for error.
Check
We want to check the last status of our routing table. We issue the same command we issued when starting.
$ sudo ip route show default via 192.168.122.1 dev ens3 10.0.3.0/24 dev lxcbr0 proto kernel scope link src 10.0.3.1 192.168.122.0/24 dev ens3 proto kernel scope link src 192.168.122.211
As we can see from the output the route to the 172.16.0.0/24 is deleted from routing table.