Linux中的5個SSH別名例子
作為一個 Linux 用戶,我們常用 ssh 命令 來登入遠程機器。ssh
命令你用得越多,你在鍵入一些重要的命令上花的時間也越多。我們可以用 定義在你的 .bashrc 文件里的別名 或函數(shù)來大幅度縮減花在命令行界面(CLI)的時間。但這不是***解決之道。***辦法是在 ssh
配置文件中使用 SSH 別名 。
這里是我們能把 ssh
命令用得更好的幾個例子。
ssh 登入到 AWS(譯注:Amazon Web Services,亞馬遜公司旗下云計算服務平臺)實例的連接是一種痛。僅僅輸入以下命令,每次也完全是浪費你時間。
ssh -p 3000 -i /home/surendra/mysshkey.pem ec2-user@ec2-54-20-184-202.us-west-2.compute.amazonaws.com
縮短到:
ssh aws1
調試時連接到系統(tǒng)。
ssh -vvv the_good_user@red1.taggle.abc.com.au
縮短到:
ssh xyz
在本篇中,我們將看到如何不使用 bash 別名或函數(shù)實現(xiàn) ssh
命令的縮短。ssh
別名的主要優(yōu)點是所有的 ssh
命令快捷方式都存儲在一個單一文件,如此就易于維護。其他優(yōu)點是 對于類似于 SSH 和 SCP 的命令 我們能用相同的別名。
在我們進入實際配置之前,我們應該知道 /etc/ssh/ssh_config
、/etc/ssh/sshd_config
和 ~/.ssh/config
文件三者的區(qū)別。以下是對這些文件的解釋。
/etc/ssh/ssh_config 和 ~/.ssh/config 間的區(qū)別
系統(tǒng)級別的 SSH 配置項存放在 /etc/ssh/ssh_config
,而用戶級別的 SSH 配置項存放在 ~/.ssh/config
文件中。
/etc/ssh/sshconfig 和 /etc/ssh/sshdconfig 間的區(qū)別
系統(tǒng)級別的 SSH 配置項是在 /etc/ssh/ssh_config
文件中,而系統(tǒng)級別的 SSH 服務端配置項存放在 /etc/ssh/sshd_config
文件。
在 ~/.ssh/config 文件里配置項的語法
~/.ssh/config
文件內容的語法:
配置項 值
配置項 值1 值2
例 1: 創(chuàng)建主機(www.linuxnix.com)的 SSH 別名
編輯 ~/.ssh/config
文件寫入以下內容:
Host tlj
User root
HostName 18.197.176.13
port 22
保存此文件。
以上 ssh 別名用了
tlj
作為一個別名的名稱root
作為將要登入的用戶18.197.176.13
作為主機的 IP 地址22
作為訪問 SSH 服務的端口
輸出:
sanne@Surendras-MacBook-Pro:~ > ssh tlj
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-93-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud
Last login: Sat Oct 14 01:00:43 2017 from 20.244.25.231
root@linuxnix:~# exit
logout
Connection to 18.197.176.13 closed.
例 2: 不用密碼用 ssh 密鑰登到系統(tǒng)要用 IdentityFile
。
例:
Host aws
User ec2-users
HostName ec2-54-200-184-202.us-west-2.compute.amazonaws.com
IdentityFile ~/Downloads/surendra.pem
port 22
例 3: 對同一主機使用不同的別名。在下例中,我們對同一 IP/主機 18.197.176.13 用了 tlj
、 linuxnix
、linuxnix.com
三個別名。
~/.ssh/config 文件內容
Host tlj linuxnix linuxnix.com
User root
HostName 18.197.176.13
port 22
輸出:
sanne@Surendras-MacBook-Pro:~ > ssh tlj
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-93-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud
Last login: Sat Oct 14 01:00:43 2017 from 220.244.205.231
root@linuxnix:~# exit
logout
Connection to 18.197.176.13 closed.
sanne@Surendras-MacBook-Pro:~ > ssh linuxnix.com
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-93-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud
Last login: Sun Oct 15 20:31:08 2017 from 1.129.110.13
root@linuxnix:~# exit
logout
Connection to 138.197.176.103 closed.
[6571] sanne@Surendras-MacBook-Pro:~ > ssh linuxnix
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-93-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud
Last login: Sun Oct 15 20:31:20 2017 from 1.129.110.13
root@linuxnix:~# exit
logout
Connection to 18.197.176.13 closed.
例 4: 用相同的 SSH 別名復制文件到遠程系統(tǒng)
語法:
scp <文件名> <ssh_別名>:<位置>
例子:
sanne@Surendras-MacBook-Pro:~ > scp abc.txt tlj:/tmp
abc.txt 100% 12KB 11.7KB/s 00:01
sanne@Surendras-MacBook-Pro:~ >
若我們已經(jīng)將 ssh 主機設置好一個別名,由于 ssh
和 scp
兩者用幾乎相同的語法和選項,scp
也可以輕易使用。
請在下面嘗試從本機 scp
一個文件到遠程機器。
例 5: 解決 Linux 中的 SSH 超時問題。默認情況,如果你不積極地使用終端,你的 ssh 登入就會超時
SSH 超時問題 是一個更痛的點意味著你在一段時間后不得不重新登入到遠程機器。我們能在 ~/.ssh/config
文件里邊恰當?shù)卦O置 SSH 超時時間來使你的會話不管在什么時間總是激活的。我們將用 2 個能保持會話存活的 SSH 選項來實現(xiàn)這一目的。之一是 ServerAliveInterval
保持你會話存活的秒數(shù)和 ServerAliveCountMax
在(經(jīng)歷了一個)給定數(shù)值的會話之后初始化會話。
ServerAliveInterval A
ServerAliveCountMax B
例:
Host tlj linuxnix linuxnix.com
User root
HostName 18.197.176.13
port 22
ServerAliveInterval 60
ServerAliveCountMax 30
在下篇中我們將會看到一些其他的退出方式。