How To Add or Change Default Route or Default Gateway in Ubuntu, Linux?


Systems connected to the network will generally access to the internet. In order to access to the internet they need some network configuration like gateway or default gateway. In this tutorial we will examine how to add or change default gateway in Ubuntu, Debian, CentOS, Fedora, Mint, Kali operating systems.

List Routing Table

Routing table is used to route IP network communication. Hosts generally uses default route to send packages which will redirect them accordingly to transmit destination. We will start by listing current routing table. We will use ip route show command like below.

$ ip route show
List Routing Table
List Routing Table

Our default gateway line is

default via 192.168.122.1 dev ens3
  • default means this line is default gateway
  • via 192.168.122.1 specifies next hop which is default gateway IP address
  • dev ens3 is the interface we want use to access default gateway

Remove Existing Default Gateway

Removing default gateway is easy if we list routing table because routing table line is used with del command like below. But keep in mind if you are connecting system remotely from different network which means if you are using default route you connection will be lost.

$ ip route del default via 192.168.122.1 dev ens3
  • ip route del is our key line which deletes specified default gateway
  • default via 192.168.122.1 dev ens3 is the same as routing table

Add New Default Gateway

As stated previously default gateway is used to send packages in order to transmit to the destination. We can add new default gateway with the ip route add command like below.

$ ip route add default via 192.168.1.1 dev ens3
  • ip route add will add provided default gateway
  • default means target network is all which is default
  • via 192.168.1.1 is our default gateway network address
  • dev ens3 is network interface for default gateway
LEARN MORE  Linux fuser Command Tutorial With Examples

Check

List routing table again and ping some of remote networks will give the status of default gateway

$ ip route show 
default via 192.168.1.1 dev ens3  
10.0.3.0/24 dev lxcbr0  proto kernel  scope link  src 10.0.3.1  
192.168.122.0/24 dev ens3  proto kernel  scope link  src 192.168.122.211

 

How To Add or Change Default Route or Default Gateway in Ubuntu, Linux? Infografic

How To Add or Change Default Route or Default Gateway in Ubuntu, Linux? Infografic
How To Add or Change Default Route or Default Gateway in Ubuntu, Linux? Infografic

 

1 thought on “How To Add or Change Default Route or Default Gateway in Ubuntu, Linux?”

Leave a Comment