How to Add or Change the Default Gateway on Linux: 9 Steps

Table of contents:

How to Add or Change the Default Gateway on Linux: 9 Steps
How to Add or Change the Default Gateway on Linux: 9 Steps

Video: How to Add or Change the Default Gateway on Linux: 9 Steps

Video: How to Add or Change the Default Gateway on Linux: 9 Steps
Video: 27 WeChat hidden features & tips & tricks you'd never know 2024, December
Anonim

The default gateway is the IP address of the router you are using. When a router is installed, the operating system usually detects it automatically. However, sometimes you have to change the default gateway, especially if you are using more than one network adapter or router on the network.

Step

Part 1 of 2: Using Terminal

Add or Change the Default Gateway in Linux Step 1
Add or Change the Default Gateway in Linux Step 1

Step 1. Open Terminal

From the sidebar, open Terminal or press Ctrl+Alt+T.

Add or Change the Default Gateway in Linux Step 2
Add or Change the Default Gateway in Linux Step 2

Step 2. View your current default gateway

You can find out which default gateway is assigned to which interface by typing route and pressing Enter. The address next to "default" is the default gateway, while the interface using it appears to the right of the table.

Add or Change the Default Gateway in Linux Step 3
Add or Change the Default Gateway in Linux Step 3

Step 3. Delete the current default gateway

Network conflicts will arise if more than one default gateway is installed. Remove the existing default gateway if you are going to replace it.

Type sudo route delete my default Adapter IP_address. For example, to delete the default gateway 10.0.2.2 on the eth0 adapter, type sudo route delete default gw 10.0.2.2 eth0

Add or Change the Default Gateway in Linux Step 4
Add or Change the Default Gateway in Linux Step 4

Step 4. Type

sudo route add default gw Adapter IP_address.

For example, to change the eth0 adapter's default gateway to 192.168.1.254, type sudo route add default gw 192.168.1.254 eth0. You will be prompted for a password before you can run this command.

Part 2 of 2: Editing Configuration Files

Add or Change the Default Gateway in Linux Step 5
Add or Change the Default Gateway in Linux Step 5

Step 1. Open the configuration file from the editor application

Type sudo nano /etc/network/interfaces to create a file in Nano. The purpose of editing the configuration file is to save the changes each time the system is restarted.

Add or Change the Default Gateway in Linux Step 6
Add or Change the Default Gateway in Linux Step 6

Step 2. Find the appropriate section

Locate the section on the adapter for which you want to change the default gateway. For wired connections, this section is usually eth0.

Add or Change the Default Gateway in Linux Step 7
Add or Change the Default Gateway in Linux Step 7

Step 3. Add

gateway IP_address to the section. For example, type gateway 192.168.1.254 to make the default gateway 192.168.1.254.

Add or Change the Default Gateway in Linux Step 8
Add or Change the Default Gateway in Linux Step 8

Step 4. Save changes, then exit

Press Ctrl+X then Y to save it and exit.

Add or Change the Default Gateway in Linux Step 9
Add or Change the Default Gateway in Linux Step 9

Step 5. Re-enable the network

Re-enable it by typing sudo /etc/init.d/networking restart.

Recommended: