掌握 Linux 網路參數的即時與永久修改方法,是系統管理的基本功。本文彙整介面 IP、預設閘道、DNS、主機名稱的雙重設定方式,並補充傳統 rc.local 手動配置範例。
ifconfig eth0 192.168.0.20 netmask 255.255.255.0
編輯 /etc/sysconfig/network-scripts/ifcfg-eth0,設定 IPADDR、NETMASK、ONBOOT=yes 等參數。
route add default gw 192.168.0.254
同樣在 ifcfg-eth0 中加入 GATEWAY=192.168.0.254。
編輯 /etc/resolv.conf,修改後即時生效且重開機保留:
domain example.com
nameserver 163.1.1.4
nameserver 10.55.0.33
hostname newhost
修改 /etc/sysconfig/network 中的 HOSTNAME=newhost。
適用於不使用網路服務腳本、需完全手控的環境:
# /etc/rc.d/rc.local
/sbin/ifconfig eth0 163.1.5.125 netmask 255.255.0.0 UP
/sbin/ifconfig lo 127.0.0.1 UP
/sbin/route add -host 127.0.0.1 lo
/sbin/route add -net 163.1.5.125 netmask 255.255.0.0 eth0
/sbin/route add default gw 163.1.1.254 eth0
編輯完 /etc/rc.d/rc.local、/etc/resolv.conf、/etc/hosts.conf 後需重開機生效。
order hosts,bind
multi on
163.1.5.125 lgx.linux.net lgx
order hosts,bind:優先查 /etc/hosts,再查 DNS。multi on 允許一個 IP 對應多個主機名。
現代發行版建議使用 nmcli、ip 指令或 Netplan 管理網路,但理解傳統檔案位置與指令仍有助於排查舊系統或嵌入式環境問題。