Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

SecureCRT 連線 Linux 顯示亂碼的根因分析與三步修復法

編輯:關於 Linux

SecureCRT 連線 Linux 伺服器後出現中文亂碼,核心原因只有一個:**終端模擬器、系統 Locale、檔案編碼三者不一致**。本文提供標準化排查流程,從檔案編碼檢測到系統 Locale 調整、SecureCRT 設定同步,一次解決亂碼困擾。

SecureCRT 亂碼示例畫面

核心原則

檔案編碼 = 系統 Locale = SecureCRT 會話編碼
三者任一不匹配即產生亂碼。

排查與修復三步驟

步驟 1:確認檔案實際編碼

  • Vim 內查看::set fileencoding → 輸出如 fileencoding=latin1(即 ISO-8859-1)
  • 指令檢測:file Mysql2.txt → ISO-8859 text, with CRLF line terminators

常見編碼對應:
UTF-8、GBK/GB18030、ISO-8859-1 (latin1)、BIG5

步驟 2:調整系統 Locale 與環境變數

若檔案為 UTF-8,系統應設為 zh_TW.UTF-8 或 en_US.UTF-8;若為 GBK,設為 zh_CN.GBK。
臨時生效(當前 Shell):

export LANG=zh_TW.UTF-8
export LC_ALL=zh_TW.UTF-8

永久生效(全系統):

# CentOS/RHEL
echo 'LANG="zh_TW.UTF-8"' > /etc/locale.conf
# Debian/Ubuntu
dpkg-reconfigure locales  # 選擇 zh_TW.UTF-8

驗證:echo $LANG、locale

步驟 3:同步 SecureCRT 會話編碼

  1. 右鍵會話標籤 → Properties (屬性)
  2. Terminal → Appearance → Character encoding 選擇與步驟 1、2 一致的編碼(如 UTF-8)
  3. 勾選 Set as default 套用至新會話

進階:Vim 內強制重新讀取編碼

若檔案編碼確為 UTF-8 但 Vim 誤判為 latin1:

:e ++enc=utf-8

若需永久轉換檔案編碼:

:set fileencoding=utf-8
:w

常見誤區

  • 只改 SecureCRT 不改系統 Locale → 遠端執行 ls、cat 仍亂碼
  • 只改系統 Locale 不改 SecureCRT → 終端顯示正常,但輸入中文時編碼錯誤
  • 混用 LC_ALL 覆蓋 LANG 導致預期外行為

遵循「三者一致」原則,依序驗證檔案→系統→終端,亂碼問題可在 5 分鐘內徹底解決。

Copyright © Windows教程網 All Rights Reserved