Linux

Linux 網路設定 ifconfig 指令使用方法教學與範例

介紹如何在 Linux 系統中使用 ifconfig 指令查詢與修改網路設定值。

ifconfig 是一個基本的 Linux 網路設定工具,可以用來查詢與修改系統上各網路介面的設定,以下是 ifconfig 指令的使用方法與範例。

安裝 ifconfig 指令工具

若在 Ubuntu 或 Debian 系列的 Linux 中,可以透過 apt 安裝 net-tools 套件,即可安裝 ifconfig 指令:

# 安裝 ifconfig 工具(Ubuntu/Debian)
sudo apt install net-tools

若在 Red Hat 系列的 Linux 中,則可使用 dnf 安裝 net-tools 套件:

# 安裝 ifconfig 工具(CentOS/RHEL)
sudo dnf install net-tools

查詢網路介面

執行 ifconfig 指令不帶任何參數,可以顯示目前主機上已經啟用的網路介面:

# 查看目前啟用的網路介面
ifconfig
enp0s3: flags=4163  mtu 1500
        inet 192.168.5.4  netmask 255.255.255.0  broadcast 192.168.5.255
        inet6 fe80::1f6d:bf6f:90bb:a074  prefixlen 64  scopeid 0x20
        ether 08:00:27:aa:26:96  txqueuelen 1000  (Ethernet)
        RX packets 215540  bytes 314084328 (314.0 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 41380  bytes 2629272 (2.6 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 268  bytes 24582 (24.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 268  bytes 24582 (24.5 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

若要查看主機上所有的網路介面(包含未啟用的網路介面),可以加上 -a 參數:

# 查看所有網路介面
ifconfig -a

若要查看指定網路介面的狀態與資訊,可以直接在參數列中指定網路介面名稱,例如查看 enp0s3 網路介面的資訊:

# 查看 enp0s3 網路介面
ifconfig enp0s3

只顯示簡略資訊

若要讓 ifconfig 僅顯示簡略的資訊,可以採用 -s 參數:

# 僅顯示簡略資訊
ifconfig -s
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
enp0s3    1500      107      0      0 0           170      0      0      0 BMRU
lo       65536      159      0      0 0           159      0      0      0 LRU

設定網路 IP 位址、遮罩

ifconfig 指令也可以用來設定網路介面的 IP 位址與網路遮罩,系統管理者在測試或除錯時很常使用。

ifconfig 設定網路介面 IP 位址與遮罩的語法為:

ifconfig 網路介面 IP位址 netmask 遮罩

例如將 enp0s3 網路介面的 IP 位址設定為 192.168.5.100,網路遮罩設定為 255.255.255.0,則可執行:

# 設定 enp0s3 網路介面的 IP 位址與遮罩
sudo ifconfig enp0s3 192.168.5.100 netmask 255.255.255.0

IP 別名

IP 別名(IP aliasing)可以讓一張網路卡綁上多個 IP 位址,若要用 ifconfig 設定 enp0s3 這張網路卡的第一個 IP 別名(第二個 IP 位址),則可執行:

# 設定 IP 別名
sudo ifconfig enp0s3:0 192.168.5.101 netmask 255.255.255.0

這裡的 enp0s3:0 代表 enp0s3 的第一個 IP 別名,若要增加多個 IP 別名,則可使用 enp0s3:1enp0s3:2 等名稱。

設定預設閘道

通常在更改網路設定之後,都會需要調整預設閘道設定,這部分可以使用 route 指令來處理,例如將預設閘道設定為 192.168.5.254 則可執行:

# 設定預設閘道
sudo route add default gw 192.168.5.254

若要移除預設閘道設定,則可執行:

# 移除預設閘道設定
sudo route delete default gw 192.168.5.254

啟用、停用網路介面

若要啟用指定的網路介面,可以使用 ifconfig 指定網路介面並搭配 up 參數,例如啟用 enp0s3 這一張網路介面:

# 啟用 enp0s3 網路介面
sudo ifconfig enp0s3 up

若要停用指定的網路介面,則可使用 down 參數:

# 停用 enp0s3 網路介面
sudo ifconfig enp0s3 down

更改網路卡 MAC 位址

每一張網路卡都有一個固定的 MAC 位址,若要更改這個 MAC 位址,可以使用以下指令:

# 設定 enp0s3 網路卡 MAC 位址為 0c:72:5c:0f:80:73
ifconfig enp0s3 hw ether 0c:72:5c:0f:80:73

設定 MTU 值

若要更改網路介面的最大傳輸單元(MTU)值,可以使用以下指令:

# 設定 enp0s3 網路介面的 MTU 值為 8000
sudo ifconfig enp0s3 mtu 8000

Promiscuous 模式

當網路介面啟用 promiscuous 模式時,可以截取所有的網路封包資料,通常用於資訊安全相關的操作。

若要讓網路介面啟用 promiscuous 模式,可以在指定網路介面後,加上 promisc 參數:

# 啟用 enp0s3 網路介面的 promiscuous 模式
sudo ifconfig enp0s3 promisc

若要停用 enp0s3 網路介面的 promiscuous 模式,可以執行:

# 停用 enp0s3 網路介面的 promiscuous 模式
sudo ifconfig enp0s3 -promisc

查詢 IP 位址指令稿

以下的 bash 指令稿可以用來取得指定網路介面的 IP 位址:

# 取得 enp0s3 網路介面的 IP 位址
ifconfig enp0s3 | grep "inet " | awk '{print $2}'
192.168.5.4

參考資料

Share
Published by
Office Guide

Recent Posts

Python 使用 PyAutoGUI 自動操作滑鼠與鍵盤

本篇介紹如何在 Python ...

9 個月 ago

Ubuntu Linux 以 WireGuard 架設 VPN 伺服器教學與範例

本篇介紹如何在 Ubuntu ...

9 個月 ago

Linux 網路設定 ip 指令用法教學與範例

本篇介紹如何在 Linux 系...

9 個月 ago

Windows 使用 TPM 虛擬智慧卡保護 SSH 金鑰教學與範例

本篇介紹如何在 Windows...

10 個月 ago

Linux 以 Shamir’s Secret Sharing 分割保存金鑰教學與範例

介紹如何在 Linux 中使用...

11 個月 ago

Linux 以 Cryptsetup、LUKS 加密 USB 隨身碟教學與範例

介紹如何在 Linux 系統中...

11 個月 ago