資料來源:公眾號【網路技術乾貨圈】
作者:圈圈
ID:wljsghq
網路設備的設定與管理是每位網路工程師的必修課,而思科(Cisco)作為網路設備領域的巨頭,其設備的命令列介面(CLI)是無數工程師日常工作的核心工具。
無論是設定路由器、交換機,或是排除網路故障,熟練思科設備的常用指令不僅能提升工作效率,還能讓你在複雜網路環境中游刃有餘。
本文將為你詳細整理思科設備中最常用的指令,涵蓋基礎操作、設定管理、故障排查等多個面向,力求讓你一看就懂,一學就會!
初識思科CLI:進入命令列世界的第一步
思科設備的命令列介面(CLI)是網路工程師與設備互動的主要方式。透過CLI,你可以設定設備、管理網路、監控狀態,甚至進行故障診斷。思科IOS(Internetwork Operating System)是思科設備的核心作業系統,其指令結構清晰但功能強大。以下是進入CLI的基礎命令和模式,你需要牢記這些「敲門磚」:
-
1進入特權模式
從使用者模式(User EXEC Mode)進入特權模式(Privileged EXEC Mode),這是執行進階配置和檢視裝置狀態的前提。
Router> enableRouter#
進入特權模式後,提示符號從>變為#,表示你已獲得更高權限。
-
1進入全域設定模式
configure terminal(簡寫:conf t)
從特權模式進入全域設定模式(Global Configuration Mode),允許對裝置進行全域設置,如主機名稱、IP位址等。
Router# configure terminalEnter configuration commands, one per line. End with CNTL/Z.Router(config)#
-
1退出當前模式
exit退回上一級模式,end直接回到特權模式。
Router(config)# exitRouter#Router(config)# endRouter#
-
1儲存配置
write memory(或簡寫wr)
將目前執行設定(running-config)儲存至啟動設定(startup-config),確保裝置重新啟動後設定不遺失。
Router# write memoryBuilding configuration...[OK]
小貼士:初學者常忽略儲存配置,導致設備重新啟動後配置遺失。每次完成重要配置後,就養成用wr保存的習慣!
設備基本配置
設定思科設備的第一步通常是設定基本參數,例如主機名稱、密碼、IP位址等。這些命令簡單但實用,是裝置管理的基石。
-
1設定主機名
hostname <name>
為設備設定一個唯一的名稱,以便於識別和管理。
Router(config)# hostname Core-RouterCore-Router(config)#
設定後,提示符號會變成Core-Router#,更直覺。
-
1配置介面IP位址
interface <interface-type> <interface-number>ip address <ip-address> <subnet-mask>no shutdown
為指定介面配置IP位址並啟動介面。
(為GigabitEthernet0/0配置IP):
Core-Router(config)# interface GigabitEthernet0/0Core-Router(config-if)# ip address 192.168.1.1 255.255.255.0Core-Router(config-if)# no shutdown
注意:no shutdown命令用於啟動接口,否則接口將保持關閉狀態,無法通訊。
-
1設定管理存取密碼
指令(配置Console密碼):
line console 0password <password>login
為控制台存取設定密碼,增強裝置安全性。
Core-Router(config)# line console 0Core-Router(config-line)# password cisco123Core-Router(config-line)# login
-
1啟用SSH遠端存取
命令:
ip domain-name <domain>crypto key generate rsaline vty 0 4transport input sshlogin local
啟用SSH,允許遠端安全管理設備。
範例:
Core-Router(config)# ip domain-name example.comCore-Router(config)# crypto key generate rsaThe name for the keys will be: Core-Router.example.comChoose the size of the key modulus [512]: 2048Core-Router(config)# line vty 0 4Core-Router(config-line)# transport input sshCore-Router(config-line)# login localCore-Router(config)# username admin password cisco123
場景應用:假設你接手了一台新的思科路由器,需要快速設定主機名稱、介面IP和SSH遠端存取。只需依照上述步驟操作,幾分鐘內即可讓裝置「上線」並支援安全管理。
查看與監控
在網路管理中,即時監控設備狀態和網路運作情況至關重要。以下是常用的檢視指令,幫助你快速掌握設備資訊:
-
1查看運行配置
show running-config(或show run)
顯示目前設備的運作配置,包含所有已套用的設定。
Core-Router# show running-configBuilding configuration...Current configuration : 1024 bytes...
-
1查看介面狀態
show ip interface brief
以表格形式顯示所有介面的IP位址、狀態和協定訊息,適合快速檢查介面是否正常運作。
Core-Router# show ip interface briefInterface IP-Address OK? Method Status ProtocolGigabitEthernet0/0 192.168.1.1 YES manual up upGigabitEthernet0/1 unassigned YES unset administratively down down
-
1查看路由表
show ip route
顯示設備的路由表,了解封包的轉送路徑。
Core-Router# show ip routeCodes: C - connected, S - static, R - RIP, ...C 192.168.1.0/24 is directly connected, GigabitEthernet0/0
-
1查看設備硬體資訊
show version
顯示設備的硬體型號、IOS版本、運作時間等資訊。
Core-Router# show versionCisco IOS Software, C2900 Software (C2900-UNIVERSALK9-M), Version 15.1(4)M4...
小貼士:show指令是思科設備中最常用的診斷工具,熟練使用這些指令可以讓你快速定位問題。例如,網路連線中斷時,先用show ip interface brief檢查介面狀態,再用show ip route確認路由是否正確。
故障排查神器
網路故障排查是網路工程師的核心技能,思科設備提供了多種強大的診斷指令,幫助你快速定位問題根因。
-
1 Ping命令
ping <ip-address>
測試與目標IP的連通性,檢查網路是否可達。
Core-Router# ping 192.168.1.2Type escape sequence to abort.Sending 5, 100-byte ICMP Echos to 192.168.1.2, timeout is 2 seconds:!!!!!Success rate is 100 percent (5/5), round-trip min/avg/max = 1/2/4 ms
解釋:!表示成功,.表示失敗。如果ping不通,可能需要檢查介面狀態或路由。
-
1 Traceroute指令
traceroute <ip-address>
追蹤資料包的路徑,定位網路延遲或中斷點。
Core-Router# traceroute 8.8.8.8Type escape sequence to abort.Tracing the route to 8.8.8.81 192.168.1.2 4 msec 4 msec 4 msec2 10.0.0.1 8 msec 8 msec 8 msec...
-
1查看日誌
show logging
顯示設備日誌,包含系統事件、錯誤訊息等。
Core-Router# show loggingSyslog logging: enabled...%LINK-3-UPDOWN: Interface GigabitEthernet0/0, changed state to up
-
1調試命令(謹慎使用)
debug <protocol>(如debug ip packet)
即時顯示協定或介面的詳細運行信息,用於深入排查問題。
Core-Router# debug ip packetIP packet debugging is on
注意:調試會佔用設備資源,使用後記得關閉(undebug all或u all)。
場景應用:假設某台伺服器無法存取外部網絡,你可以先用ping測試連通性,再用traceroute檢查路徑,最後用show ip route確認路由是否正確。如果仍未解決,請啟用debug查看詳細資料包資訊。
進階配置
當你掌握了基礎指令後,可以嘗試更複雜的配置,例如VLAN、靜態路由、ACL等。以下是幾個進階命令的介紹:
-
1配置VLAN(適用於交換器)
命令:
vlan <vlan-id>name <vlan-name>interface <interface-type> <interface-number>switchport mode accessswitchport access vlan <vlan-id>
在交換器上建立VLAN並將介面指派到指定VLAN。
範例:
Switch(config)# vlan 10Switch(config-vlan)# name SALESSwitch(config-vlan)# exitSwitch(config)# interface FastEthernet0/1Switch(config-if)# switchport mode accessSwitch(config-if)# switchport access vlan 10
-
1配置靜態路由
ip route <destination-network> <subnet-mask> <next-hop>
手動新增靜態路由,指定封包的轉送路徑。
Core-Router(config)# ip route 10.0.0.0 255.255.255.0 192.168.1.2
-
1配置存取控制清單(ACL)
命令:
access-list <acl-number> permit|deny <source> <wildcard>interface <interface-type> <interface-number>ip access-group <acl-number> in|out
控製網路流量,限製或允許特定IP存取。
範例(拒絕192.168.2.0網段存取):
Core-Router(config)# access-list 101 deny ip 192.168.2.0 0.0.0.255 anyCore-Router(config)# access-list 101 permit ip any anyCore-Router(config)# interface GigabitEthernet0/0Core-Router(config-if)# ip access-group 101 in
小貼士:進階配置需要對網路拓樸有清楚的了解,建議在配置前畫出網路圖,確保每一步操作符合實際需求。
實用技巧
-
1命令補全與幫助
按鍵Tab可自動補全指令。
輸入?取得指令提示,例如:show ?列出所有show指令。
-
1批量配置
使用do指令在設定模式下執行特權模式指令,例如:
Core-Router(config)# do show running-config
-
1備份與復原配置
-
備份: copy running-config tftp(將設定儲存到TFTP伺服器)。 -
恢復: copy tftp running-config。
-
1避免常見錯誤
-
配置前確認介面狀態( show ip interface brief)。 -
調試命令使用後及時關閉( undebug all)。 -
儲存配置( wr)以防重啟遺失。
发表回复