萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 網路安全 >  >> 網路應用技術 >> 應用程式教學

RouterOS (ROS) 軟路由常用 CLI 命令速查手冊:接口、路由、防火牆、NAT、監控、備份一覽

RouterOS (MikroTik) 功能強大但 CLI 學習曲線陡峭。本文彙總運維高頻命令,按功能模塊分類,附參數解釋與實戰示例,適合打印貼在機房/桌邊速查。

基礎導航與幫助

  • ? — 當前上下文可用命令列表與簡要說明。
  • 命令 ? — 顯示該命令所有參數與取值範圍。
  • Tab — 命令/參數補全 (極佳體驗)。
  • 命令 縮寫 — 如 interface 可縮寫 in,ip address 可 ip ad。

接口與 IP 配置

  • /interface print — 列出所有接口 (名稱、類型、MAC、MTU、運行狀態)。
  • /interface set [find name=ether1] name=WAN — 重命名接口 (建議語義化:WAN、LAN、DMZ、WLAN)。
  • /ip address add address=192.168.88.1/24 interface=LAN comment="LAN Gateway" — 配置 IP/掩碼/接口/備註。
  • /ip address print — 查看所有 IP 配置。

路由與多線接入 (PCC/ECMP/策略路由)

  • /ip route add dst-address=0.0.0.0/0 gateway=203.0.113.1 check-gateway=ping distance=1 comment="ISP1 Default" — 添加默認路由,啟用 Ping 健康檢查、距離值主備。
  • /ip route add dst-address=0.0.0.0/0 gateway=198.51.100.1 distance=2 comment="ISP2 Backup" — 備用默認路由。
  • /ip route print detail — 顯示路由表詳情 (距離、範圍、目標、網關、介面、標記)。
  • PCC 負載均衡核心: /ip firewall mangle 打標記 + /ip route 指定 routing-mark,實現流量分流。

防火牆 / NAT / 端口映射 (核心高頻)

  • /ip firewall filter add chain=forward action=drop connection-state=invalid comment="Drop Invalid" — 丟棄無效狀態連接 (必須放鏈路最前)。
  • /ip firewall filter add chain=input protocol=icmp action=accept comment="Allow Ping" — 允許 Ping 管理介面。
  • /ip firewall filter add chain=input in-interface=WAN action=drop comment="Block WAN Access" — 禁止 WAN 直接訪問路由器管理平面 (SSH/WinBox/Webfig/API)。
  • /ip firewall nat add chain=srcnat out-interface=WAN action=masquerade comment="LAN to Internet NAT" — 源地址轉換 (MASQ/NAPT),內網上網必備。
  • /ip firewall nat add chain=dstnat protocol=tcp dst-port=80 in-interface=WAN action=dst-nat to-addresses=192.168.88.10 to-ports=80 comment="Port 80 -> Web Server" — 端口映射 (DNAT),外網訪問內網 Web 服務器。
  • /ip firewall nat print stats — 顯示 NAT 規則命中統計 (包/字節數)。

實時監控與診斷

  • /interface monitor-traffic ether1,ether2,ether3 once — 瞬時流量快照 (rx/tx bps/pps)。
  • /interface monitor-traffic ether1 interval=1 — 實時滾動顯示 (Ctrl+C 退出)。
  • /ip firewall connection print — 當前連接追蹤表 (src/dst IP:port、協議、狀態、超時、標記、字節/包計數)。
  • /ip firewall connection print where dst-address~"192.168.88.10" and protocol=tcp — 過濾特定連接。
  • /tool torch interface=WAN port=80,443 — 實時抓包顯示源/目 IP、端口、協議、流量 (類輕量 tcpdump)。
  • /tool sniffer start interface=WAN filter-ip-protocol=tcp filter-port=80 / /tool sniffer packet print — 完整抓包分析 (可導出 .pcap)。
  • /tool ping 8.8.8.8 count=5 size=64 / /tool traceroute 8.8.8.8 — 連通性/路徑診斷。

資源與日誌

  • /system resource print — CPU/內存/磁盤/運行時間/版本/板級信息。
  • /system resource monitor — 實時 CPU/內存/磁盤 I/O 圖表。
  • /log print — 系統日誌 (可 where topics~"error" 過濾)。
  • /log print follow — 實時尾部跟蹤日誌。

用戶會話與備份

  • /user active print — 當前登入管理會話 (WinBox/Webfig/SSH/API)。
  • /system backup save name=config_20241201.dont-encrypt — 完整備份 (含證書/腳本/用戶/日誌),不加密 便於異機恢復/版本控制。
  • /export file=config_20241201.rsc — 文本格式配置導出 (可讀、可 diff、可 Git 管理,**強烈推薦作為主備份**)。
  • /system reset-configuration keep-users=yes no-defaults=yes run-after-reset=config_20241201.rsc — 一鍵重裝並導入配置 (灰度發布/災備演練利器)。

系統控制

  • /system reboot / /system shutdown — 重啟/關機。
  • /system package update check-for-updates / /system package update install — 固件升級。

熟練這套 CLI,ROS 軟路由運維效率提升 10 倍,告別點點點低效模式。

copyright © 萬盛學電腦網 all rights reserved