The default gateway in Linux is the IP address of the router that forwards packets when the Linux system accesses other networks (such as the Internet). It is a core configuration for achieving inter-network communication.
Core Function
01
The core value of the default gateway lies in solving the problem of “the local network cannot directly access the target network.” Its specific functions are as follows:
- When a Linux host wants to access a target IP that is not in the local subnet (such as accessing the Internet), the system automatically sends the packet to the default gateway.
- The default gateway (usually a router) is responsible for forwarding the packet to the next network, ultimately reaching the target address.
- If the default gateway is not configured, the Linux host can only communicate with devices within the same local area network and cannot connect to the external network or other subnets.
Working Principle
02
The Linux system determines the forwarding path of packets through the routing table. The default gateway is a key rule in the routing table, and its working logic is as follows:
- The user initiates cross-subnet access (e.g., ping 8.8.8.8).
- The system checks the subnet to which the target IP belongs to determine if it is in the local subnet.
- If it is not in the local subnet, the system queries the routing table to find the “default route” (usually marked as 0.0.0.0/0).
- The IP address pointed to by the default route is the default gateway, and the packet will be sent to this gateway for subsequent forwarding.
Viewing and Configuration Methods
03
In daily operations, the most common task is to view the current default gateway, and it is also necessary to understand how to temporarily or permanently configure the default gateway.
(1) View Current Default Gateway
You can quickly obtain the current system’s default gateway using the following commands:
- Method 1: Use the ip command (recommended, commonly used in modern Linux systems) to execute ip route show or ip r, and the IP after default via in the output is the default gateway. Example:default via 192.168.1.1 dev eth0 proto dhcp src 192.168.1.100 metric 100, where 192.168.1.1 is the default gateway.
- Method 2: Use the route command (high compatibility, still used in some systems) to execute route -n, and the line where Destination is 0.0.0.0 corresponds to the IP in the Gateway column, which is the default gateway.
(2) Temporarily Configure Default Gateway (Invalid After Reboot)
This is suitable for temporary testing scenarios, and the command format is:ip route add default via [Gateway IP] dev [Network Interface Name] Example:ip route add default via 192.168.1.1 dev eth0, which sets the default gateway to 192.168.1.1 and forwards through the eth0 network interface.
(3) Permanently Configure Default Gateway (Effective After Reboot)
The configuration file paths differ among various Linux distributions (such as CentOS, Ubuntu). For mainstream systems, the examples are as follows:
- Ubuntu/Debian series: Edit the YAML configuration file in the /etc/netplan/ directory (e.g., 01-network-manager-all.yaml), and add the gateway4: [Gateway IP] field.
- CentOS/RHEL series: Edit the network interface configuration file (e.g., /etc/sysconfig/network-scripts/ifcfg-eth0), and add the GATEWAY=[Gateway IP] field. Save and restart the network service (systemctl restart network).
Recommended: Programming Tutorial→Luozhu Programming Assistant | Treasure Website Recommendation: A website for systematic programming learning
- Linux Guide (One): Linux Kernel and Linux System
- Linux Guide (Two): Linux Command Line and Graphical User Interface
- Linux Guide (Seven): Directory Introduction, Directory Features and Functions, Common Directories
- Linux Guide (Nine): User Directory, Home Directory
- Linux Guide (Ten): Files, Everything is a File
- Linux Guide (Thirteen): Disks
- Linux Guide (Twenty): Networking
- Linux Commands (Seven): Linux Network Communication Commands