How to set up routing table in Linux (Debian)
- User
How to set up routing table in Linux (Debian)
- Assistant
How to set up routing table in Linux (Debian)
Setting up a routing table in Linux (specifically on a Debian-based system) involves defining rules that dictate how data packets should be routed through the network. This is particularly useful for directing traffic, configuring multiple network interfaces, or setting up a VPN. Below are basic steps on how to view your current routing table and add or delete routes. Keep in mind that these changes are temporary and will be reset after a reboot unless you make them permanent by editing network configuration files.
Using the route command: This command is considered deprecated but is still useful.
route -n
Using the ip command (preferred): The ip command is more powerful and provides more detailed information.
ip route show
To add a new route, you can use the ip route add command followed by the routing information. For example, to route traffic destined for the 192.168.2.0/24 network via the gateway at 192.168.1.1 on the eth0 interface, you would use:
sudo ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0
To delete an existing route, you can use the ip route del command followed by the routing information you wish to remove. For example, to remove the route we previously added:
sudo ip route del 192.168.2.0/24
Changes made with the ip route add and ip route del commands are temporary. To make routing changes permanent on a Debian system, you need to update your network configuration files. Debian 10 (Buster) and newer versions typically use systemd-networkd or NetworkManager for network configuration, but you might also encounter ifupdown (/etc/network/interfaces) on older or minimal installations.
ifupdown (/etc/network/interfaces):Edit Interfaces File: Open /etc/network/interfaces in a text editor with root privileges.
sudo nano /etc/network/interfaces
Add Routing Commands: Add up and down commands to the interface configuration to add or remove routes automatically when the interface is brought up or down.
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
up ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0
down ip route del 192.168.2.0/24
Restart Networking Service: Apply the changes by restarting the networking service or rebooting the system.
sudo systemctl restart networking
systemd-networkd:Edit Network Configuration: Find and edit the .network file corresponding to your network interface in /etc/systemd/network/.
Add Routing Policies: Under [Network] section, add routing policies. Refer to systemd.network(5) man page for detailed syntax.
Restart systemd-networkd: Apply changes by restarting systemd-networkd:
sudo systemctl restart systemd-networkd
Review the documentation specific to your networking service (ifupdown, systemd-networkd, or NetworkManager) for more detailed instructions tailored to your setup. Remember that making changes to the network configuration can disrupt connectivity, so proceed with caution.
Not yet included in a published release.