How to change gateway in Linux

Once I needed to change the default gateway on different servers without restarting the network service or rebooting the server, I will give examples of how I did this.

First look at the current routes:

ip route

On Ubuntu Server I added a new gateway and then removed the old one:

ip route add default via 192.168.5.8
ip route del default via 192.168.5.1

On a server with Proxmox based on Debian, this did not work out, an error was displayed:

RTNETLINK answers: File exists

So I ran the command (this is a more correct option):

ip route replace default via 192.168.5.8

Then I changed the gateway in the network configuration file so that the changes would not be reset after restarting the server:

cd /etc/network/
nano interfaces

If the server uses Netplan, you can change the gateway in its configuration and then run:

netplan try
netplan apply

How to change other network parameters, such as IP address, etc. look at my articles:
Configuring the Network in Linux
How to configure networking with Netplan

Leave a comment

Leave a Reply