Linux setup default gateway with route command

https:/‮ual.www/‬tturi.com
Linux setup default gateway with route command

To set the default gateway in Linux, you can use the route command.

To set the default gateway to 192.168.1.1, you can use the following command:

sudo route add default gw 192.168.1.1

This will add a route to the default destination (0.0.0.0) through the specified gateway (192.168.1.1).

To make the change permanent, you can add the route command to a startup script or configuration file. For example, you can add it to the /etc/rc.local file:

echo "Adding default route to 192.168.1.1" | logger -t rc.local
route add default gw 192.168.1.1

Keep in mind that the default gateway is used to route traffic to destinations outside the local network. It is important to set the default gateway correctly to ensure that your system can access the Internet or other remote networks.

If you need to change the default gateway, you can use the route del default gw command to delete the existing default route, and then use the route add default gw command to add a new one.

For example, to change the default gateway from 192.168.1.1 to 192.168.2.1, you can use the following commands:

sudo route del default gw 192.168.1.1
sudo route add default gw 192.168.2.1

Make sure to update any other scripts or configuration files that rely on the default gateway to reflect the change.

Created Time:2017-10-30 10:17:42  Author:lautturi