CentOS/RHEL: Routing Configuration Examples
I would like to add defalut gateway on my CentOS 7/6.5 or RHEL 7/6.5 Linux, How Do I add defalut gateway under centos or RHEL linux operating system? How To add a route to a network? How to delete a route to a specific network? How to delete default gateway on your linux system? this post will guide you how to add/delete default gateway/a route.
You can use ip or route command to show/ manipulate the ip routing table. the below examples will show you out some example using route command to add/delete default gateway, add/delete a route with a temporary way, it means that the changes will be lost if the system is shutdown or reboot. if you want to configure static routes or persistent routes, pls have a look the previous post in my site. (CentOS/RHEL:How To configure Static Routes or Persistent Routes)
1# To add default gateway into route table, type the below command:
route add default gw <gateway ip address> route add default gw 10.0.2.1 #===> set default gateway ip address as 10.0.2.1
Outputs:
[root@devops sysconfig]# route add default gw 10.0.2.2 [root@devops sysconfig]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default 10.0.2.2 0.0.0.0 UG 1024 0 0 enp0s3
2# To delete default gateway from route table, issue the following command:
route del default gw <gateway ip address>
route del default gw 10.0.2.1 #===>remove default gateway
Outputs:
[root@devops sysconfig]# route del default gw 10.0.2.2 [root@devops sysconfig]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface
3# Adding a route to a host, type:
route add -host <hostname or host ip address> gw <gateway ip address> route add -host 10.0.2.16 gw 10.0.2.2
Outputs:
[root@devops Desktop]# route add -host 10.0.2.16 gw 10.0.2.2 [root@devops Desktop]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default 10.0.2.2 0.0.0.0 UG 1024 0 0 enp0s3 10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3 10.0.2.16 10.0.2.2 255.255.255.255 UGH 0 0 0 enp0s3
4# deleting a route to a host, type:
route del -host <hostname or host IP address> gw <gateway ip address> route del -host 10.0.2.16 gw 10.0.2.2
Outputs:
[root@devops Desktop]# route del -host 10.0.2.16 gw 10.0.2.2 [root@devops Desktop]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default 10.0.2.2 0.0.0.0 UG 1024 0 0 enp0s3 10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3
5# Adding a route to a network
If you want to add a route for a specific network, you need pass the option “-network” to “route “command, type:
route add -net <network/mask> gw <gateway ip address> route add -net 192.168.1.0/24 gw 10.0.2.1
Outputs:
[root@devops Desktop]# route add -net 192.168.1.0/24 gw 10.0.2.1 [root@devops Desktop]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default 10.0.2.2 0.0.0.0 UG 1024 0 0 enp0s3 10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3 192.168.1.0 10.0.2.1 255.255.255.0 UG 0 0 0 enp0s3
6# Deleting a route to a specific network
To delete a route to a network, using “route del” command with “-net” option, type:
route del -net <network/mask> gw <gateway ip address> route del -net 192.168.1.0/24 gw 10.0.2.1
outputs:
[root@devops Desktop]# route del -net 192.168.1.0/24 gw 10.0.2.1 [root@devops Desktop]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default 10.0.2.2 0.0.0.0 UG 1024 0 0 enp0s3 10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3
done….