็†ฑ้–€ๅˆ†้กž
 ่ผ‰ๅ…ฅไธญ…
็›ฎ้Œ„

๐Ÿง Linux Network Configuration from Beginner to Advanced: Interface Naming, IP Setup, Gateway, DNS, and Hostname – Complete Guide

    ๐Ÿง Linux Network Configuration from Beginner to Advanced: Interface Naming, IP Setup, Gateway, DNS, and Hostname – Complete Guide

    Network configuration is one of the most fundamental tasks in Linux system administration. Whether you are deploying servers, managing virtual machines, connecting to cloud platforms, or setting up development environments, understanding interface naming, IP assignment, routing, DNS, and hostname configuration is essential.

    This article provides a complete, encyclopedia-level guide to Linux networking—from basic concepts to practical operations using nmcli, ip, traditional ifcfg files, and systemd-networkd. It includes configuration examples, troubleshooting techniques, and distribution-specific differences.


    ๐Ÿ“Œ 1. Linux Interface Naming (Predictable Network Interface Names)

    Modern Linux distributions use “predictable interface names,” replacing legacy names such as eth0. Examples include:

    • ens160 – PCI Express–based naming
    • enp0s3 – Bus address–based naming
    • eth0 – Legacy naming (still appears in minimal or custom builds)

    ๐Ÿ” List all interfaces

    ip link

    ๐Ÿ” View interface details

    ip addr show ens160

    Virtualization environments such as Proxmox, VMware, or KVM often modify interface numbering depending on virtual hardware order.


    ๐Ÿ“Œ 2. Configuring Static IP: Three Major Methods

    Linux supports three mainstream configuration approaches:

    MethodDistributionsDifficultyCharacteristics
    ifcfg (legacy) RHEL / CentOS / Rocky Linux Medium Traditional config-file method widely used in enterprises
    nmcli / nmtui All NetworkManager-based systems Easy Modern, stable, interactive, preferred for most use cases
    systemd-networkd Ubuntu Server / Debian / containers Medium Efficient, clean, ideal for cloud or container images
    ---

    ๐ŸŸฆ 2-1. Configure Static IP Using nmcli (Recommended)

    nmcli con mod ens160 ipv4.addresses 192.168.1.20/24
    nmcli con mod ens160 ipv4.gateway 192.168.1.1
    nmcli con mod ens160 ipv4.dns "8.8.8.8 1.1.1.1"
    nmcli con mod ens160 ipv4.method manual
    nmcli con down ens160; nmcli con up ens160
    
    ---

    ๐ŸŸฆ 2-2. Configure Static IP Using ifcfg (RHEL/CentOS)

    Configuration file location:

    /etc/sysconfig/network-scripts/ifcfg-ens160

    Example:

    BOOTPROTO=none
    DEVICE=ens160
    IPADDR=192.168.1.20
    PREFIX=24
    GATEWAY=192.168.1.1
    DNS1=8.8.8.8
    DNS2=1.1.1.1
    ONBOOT=yes
    

    Restart networking:

    systemctl restart NetworkManager
    ---

    ๐ŸŸฆ 2-3. Configure Static IP Using systemd-networkd

    File location:

    /etc/systemd/network/10-ens160.network

    Content:

    [Match]
    Name=ens160
    
    [Network]
    Address=192.168.1.20/24
    Gateway=192.168.1.1
    DNS=8.8.8.8 1.1.1.1
    

    Enable the service:

    systemctl enable systemd-networkd --now
    systemctl restart systemd-networkd
    

    ๐Ÿ“Œ 3. Essential Network Commands

    ๐Ÿ” Check current IP

    ip addr

    ๐Ÿ” Test gateway connectivity

    ping 192.168.1.1

    ๐Ÿ” View routing table

    ip route

    ๐Ÿ” DNS testing

    dig google.com
    nslookup google.com

    ๐Ÿ” View listening ports

    ss -tulnp

    ๐Ÿ“Œ 4. DNS (Resolver) Configuration

    NetworkManager-managed systems usually configure DNS via nmcli or DHCP.

    ๐ŸŸช Method 1: Using nmcli

    nmcli con mod ens160 ipv4.dns "8.8.8.8 1.1.1.1"
    nmcli con up ens160
    

    ๐ŸŸช Method 2: Editing resolv.conf (Not Recommended)

    In systems using systemd-resolved or NetworkManager, /etc/resolv.conf may be overwritten. To force manual control:

    chattr +i /etc/resolv.conf

    ⚠ Use only for special scenarios.


    ๐Ÿ“Œ 5. Configuring Default Gateway

    ๐Ÿ”ง Using nmcli

    nmcli con mod ens160 ipv4.gateway 192.168.1.1

    ๐Ÿ”ง Using ip route (temporary)

    ip route add default via 192.168.1.1

    ๐Ÿ”ง Show routing

    ip route show

    ๐Ÿ“Œ 6. Hostname Configuration

    ๐Ÿ” Check hostname

    hostnamectl

    ๐Ÿ”ง Set a new hostname

    hostnamectl set-hostname web01.example.local

    Reopen your shell to apply.


    ๐Ÿ“Œ 7. Troubleshooting: Common Issues

    ❗ No IP Address?

    nmcli device status

    ❗ DNS failures?

    dig @8.8.8.8 google.com

    ❗ Can ping gateway but cannot reach the Internet?

    • Upstream gateway missing NAT
    • Firewall blocking traffic
    • Incorrect DNS configuration

    ❗ Interface name changed unexpectedly?

    ls -l /sys/class/net/

    Usually caused by virtual hardware order changes.


    ๐Ÿ“š Recommended Reading

    ๐Ÿ”— ๅˆ†ไบซ้€™็ฏ‡ LINE Facebook X

    ๆฒ’ๆœ‰็•™่จ€:

    ๅผต่ฒผ็•™่จ€

    ๅญ—็ดš