ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
# 完全用 GNU/Linux 工作 ### 20. 增進 SSH 使用效率 - ssh_config [SSH](http://zh.wikipedia.org/wiki/Secure_Shell) 可說是每台 GNU/Linux 必裝的服務之一,我們可以藉由它,在遠端進行一切的操作。為避免被怪客 (cracker) 入侵,會於提供該服務的機器加上限制,以提高安全性,例如:更改埠口 (Port)、限制 IP 登入或只使用金鑰登入 ... 等。 隨著機器數量及限制增加的同時,使用上的繁瑣及不便也會隨之增加。這時,我們可事先設定各個主機,以便日後的使用。Windows 上的 [PieTTY](http://ntu.csie.org/~piaip/pietty/), [Xshell](http://www.netsarang.com/products/xsh_overview.html) 皆有此功能,而在 Linux 裡凍仁則習慣直接編修 openssh-client 的設定檔 *$HOME/.ssh/config*。 ### 基本使用 設定各個主機 (Host) 的 SSH 設定。 ~~~ $ vi ~/.ssh/config # - master Host master # 代號 Hostname 192.168.11.24 # IP or Domain name Port 2222 # 指定埠口 User jonny # 使用者名稱 identityfile ~/.ssh/id_rsa_24 # 指定金鑰 # - slave Host slave # 代號 Hostname 192.168.11.25 # IP or Domain name Port 2223 # 指定埠口 User jonny # 使用者名稱 identityfile ~/.ssh/id_rsa_25 # 指定金鑰 ~~~ 使用 *$HOME/.ssh/config* 設定檔連接各主機。 ~~~ [ jonny@workstation ~ ] $ ssh slave [ jonny@slave ~ ] $ sftp master Connected to master. sftp> ~~~ ### 進階使用 以下參數在較舊版的 openssh-server 可能不支援。 #### 認證代理 當本機上的同一把金鑰曾在多台 Server 上註冊並啟用此設定,則可於多台 Server 上轉發,也就是可使用本機的金鑰登入第二層、第三層的 Server。 ~~~ ForwardAgent yes ~~~ #### 壓縮頻寬 先藉由 CPU 壓縮後再進行傳輸,適用於效能較好的機器。 ~~~ Compression yes ~~~ #### 減少重複連線的時間 ~~~ ControlMaster auto ControlPath /tmp/ssh-%r@%h:%p ~~~ #### 延長連線時間 ~~~ ControlPersist 1h ~~~ #### 取消連線加密 於區網、可信賴的網路環境及大量檔案傳輸之情境使用此參數可有效減少 CPU 負載以提升傳輸速度。 ~~~ Ciphers arcfour ~~~ 更多詳細說明請參考 [Manpage](http://manpages.ubuntu.com/manpages/lucid/man5/ssh_config.5.html)。 ~~~ man ssh_config ~~~ ### 資料來源 - [教你高效使用SSH 的16 個技巧 _人人IT網](http://rritw.com/a/JAVAbiancheng/ANT/20130828/416208.html) - [SSH Can Do That? Productivity Tips for Working with Remote Servers | Smylers [blogs.perl.org]](http://blogs.perl.org/users/smylers/2011/08/ssh-productivity-tips.html) - [Using an SSH Config File](https://kb.mediatemple.net/questions/1625/Using+an+SSH+Config+File) - [How To Reuse SSH Connection To Speed Up Remote Login Process](http://www.cyberciti.biz/faq/linux-unix-reuse-openssh-connection/) - [凍仁的筆記: ssh 遠端登入免密碼 with RSA](http://note.drx.tw/2010/06/ssh-by-rsa.html) - [凍仁的筆記: 阻擋 sshd 部分使用者連線 (DenyUsers, DenyGroups)](http://note.drx.tw/2008/03/sshdenyusers-denygroups.html) - [凍仁的筆記: scp - 藉由 ssh 的遠端檔案傳輸指令](http://note.drx.tw/2008/03/ubuntuscp-part1.html) - [凍仁的筆記: sshd 停用 root 遠端登入權限](http://note.drx.tw/2008/01/ssh.html)