路由器 IP 位址與端口配置是網路工程師核心技能。本文以 **Cisco IOS CLI 為標準範本** (通用於支援 CLI 的企業級路由器:Cisco、H3C、Juniper、MikroTik RouterOS、OpenWrt 等),詳細說明**主機名、特權密碼、介面 IP/子網掩碼、配置持久化 (NVRAM)、配置備份與還原 (TFTP/SCP)** 完整流程,並解析 **IOS 四大操作模式** 與提示符對應,建立專業配置思維。
| 模式 | 提示符範例 | 進入方式 | 主要功能 | 典型命令 |
|---|---|---|---|---|
| 1. 用戶模式 (User EXEC) | `Router>` `cisco2600>` | Console/Telnet/SSH 連線後,輸入密碼 (若設定) | 查看基本狀態、非破壞性測試 (ping、traceroute) | `show version`、`show interfaces`、`ping`、`traceroute` |
| 2. 特權模式 (Privileged EXEC) | `Router#` `cisco2600#` | 用戶模式輸入 `enable` + 特權密碼 (enable secret) | **完整查看權限** (show running-config、debug)、檔案操作 (copy)、重啟、進入配置模式 | `show running-config`、`debug ip packet`、`copy run start`、`reload`、`configure terminal` |
| 3. 全域配置模式 (Global Config) | `Router(config)#` `cisco2600(config)#` | 特權模式輸入 `configure terminal` (conf t) | **全域性配置**:主機名、密碼、IP 路由、VLAN、DHCP、ACL、Routing Protocol 等 | `hostname`、`enable secret`、`ip routing`、`vlan`、`router ospf`、`access-list` |
| 4. 介面配置模式 (Interface Config) | `Router(config-if)#` `cisco2600(config-if)#` | 全域配置模式輸入 `interface | **特定介面配置**:IP/掩碼、頻寬、封裝、VLAN、Port-channel、IPv6 等 | `ip address`、`shutdown`/`no shutdown`、`bandwidth`、`encapsulation`、`channel-group` |
💡 **模式切換鍵:** `end` / `Ctrl+Z` → 回特權模式 | `exit` → 回上一層 | `disable` → 回用戶模式。
Router> enable
Password: ******
Router# configure terminal
Router(config)# hostname core-rtr-01
core-rtr-01(config)#
core-rtr-01(config)# enable secret Strong#Pass2024!
# 使用 enable secret (MD5/SHA256 加密),勿用 enable password (明文/弱加密)
core-rtr-01(config)# interface gigabitethernet 0/0
core-rtr-01(config-if)# description WAN_Link_to_ISP
core-rtr-01(config-if)# ip address 203.0.113.2 255.255.255.252
core-rtr-01(config-if)# no shutdown
core-rtr-01(config-if)# exit
core-rtr-01(config)# interface serial 0/0/0
core-rtr-01(config-if)# description WAN_Leased_Line_to_Branch
core-rtr-01(config-if)# ip address 198.51.100.1 255.255.255.252
core-rtr-01(config-if)# clock rate 2000000 (DCE 端需設定時鐘速率)
core-rtr-01(config-if)# no shutdown
core-rtr-01(config-if)# exit
💡 **關鍵點:** `ip address
所有配置修改初始僅存於 **RAM (running-config)**,斷電即失。**必須寫入 NVRAM (startup-config)** 才能下次開機生效。
core-rtr-01# copy running-config startup-config
Destination filename [startup-config]? (按 Enter 確認)
Building configuration...
[OK]
💡 **習慣:** 每次配置變更結束,**必須執行 `wr` (write memory 別名) 或 `copy run start`**。
core-rtr-01# copy running-config tftp
Address or name of remote host []? 192.168.100.10 (TFTP Server IP)
Destination filename [core-rtr-01-cfg]? core-rtr-01_20240520.cfg
!!
624 bytes copied in 0.05 secs
core-rtr-01# copy tftp running-config
Address or name of remote host []? 192.168.100.10
Source filename []? core-rtr-01_20240520.cfg
Destination filename [running-config]? (按 Enter)
Loading core-rtr-01_20240520.cfg from 192.168.100.10 (via GigabitEthernet0/0): !
[OK - 624 bytes]
624 bytes copied in 0.08 secs
core-rtr-01(config)# no ip address 192.168.1.1 255.255.255.0 (介面模式下移除 IP)
core-rtr-01(config)# no router ospf 1 (全域模式移除 OSPF 進程)
core-rtr-01(config)# no access-list 101 (移除 ACL 101)
💡 **原則:** `no` + 完整原命令 即可移除對應配置。
| 檢查項目 | 命令 | 預期結果 |
|---|---|---|
| 介面 IP/掩碼/狀態 | `show ip interface brief` | 目標介面 IP 正確、Status Up、Protocol Up |
| 路由表 | `show ip route` | 直連網路 (C)、靜態/動態路由正確、預設路由 (S* 或 透過協定學習) |
| 運行配置 | `show running-config` | 關鍵配置 (hostname、enable secret、介面 IP、路由、ACL、VTY 密碼、SSH) 完整無誤 |
| 啟動配置 | `show startup-config` | 與 running-config 一致 (已 `copy run start`) |
| 連通性測試 | `ping <目標IP>` / `traceroute <目標IP>` | 關鍵鏈路通達、延遲正常 |
| SSH 存取 | `show ip ssh` / 實際 SSH 連線 | SSH v2 啟用、金鑰生成、VTY 僅允許 SSH (transport input ssh) |
掌握 CLI 配置流程、四大模式切換、NVRAM 持久化、TFTP/SCP 備份還原,是網路工程師從「會設定」進階到「專業交付」的分水嶺。**標準化流程、文件化記錄、自動化備份**,讓每一次配置變更都可控、可追溯、可復原。這才是專業網管的鐵律。