萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

ifconfig 指令完整指南:網路介面設定與查詢

ifconfig(interface configuration)是 Unix/Linux 傳統網路介面設定工具,雖新版系統建議改用 ip 指令,但 ifconfig 仍廣泛使用於腳本與舊版系統。

查看所有網路介面

ifconfig -a

顯示所有介面(含未啟用),包含介面名稱(如 eth0、enp3s0、lo)、IPv4/IPv6 位址、MAC 位址、MTU、封包統計等。

查看單一介面

ifconfig eth0

設定 IP 位址(臨時,重開機失效)

# 設定 IPv4 位址與子網路遮罩
ifconfig eth0 192.168.1.100 netmask 255.255.255.0

# 啟用/停用介面
ifconfig eth0 up
ifconfig eth0 down

# 設定 MTU
ifconfig eth0 mtu 9000

# 設定別名(虛擬 IP)
ifconfig eth0:0 192.168.1.101 netmask 255.255.255.0

啟用/停用混雜模式

ifconfig eth0 promisc   # 啟用(抓包需用)
ifconfig eth0 -promisc  # 停用

IPv6 位址操作

# 新增 IPv6 位址
ifconfig eth0 inet6 add 2001:db8::1/64

# 移除 IPv6 位址
ifconfig eth0 inet6 del 2001:db8::1/64

輸出欄位說明

欄位意義
inetIPv4 位址
inet6IPv6 位址
netmask子網路遮罩
broadcast廣播位址
etherMAC 位址
RX packets接收封包數
TX packets傳送封包數
RX bytes接收位元組數
TX bytes傳送位元組數
collisions衝突數(乙太網)
ifconfig -a 輸出範例
ifconfig 介面詳細資訊

現代替代指令

新版 Linux(CentOS 7+、Ubuntu 18.04+、Debian 9+)建議使用 ip 指令:

ip addr show        # 相當於 ifconfig -a
ip link set eth0 up # 相當於 ifconfig eth0 up
ip addr add 192.168.1.100/24 dev eth0

但 ifconfig 仍可透過安裝 net-tools 套件取得。

copyright © 萬盛學電腦網 all rights reserved