Wednesday, March 25, 2015

Simple routing in Linux

The Linux kernel has pretty capable network routing capabilities.

To see the current routing table:
route -n

The default gateway will have the "UG" flags shown in the output. Red Hat and CentOS systems usually have the routing table stored in /etc/sysconfig/network-scripts/ by network device (e.g., route-eth1, route-eno1). To change or modify the routing table, you must be root.

To add a default gateway from the command line:
/sbin/route add default gw ip-address eth0

To add a static route, use the ip command and specify the destination and interface:
/sbin/ip route add 192.168.1.0/24 via 192.168.2.254 dev eno1

To see how packets will be routed to an ip address:
/sbin/ip route get ip-address