自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux中的5個SSH別名例子

系統(tǒng) Linux
作為一個 Linux 用戶,我們常用 ssh 命令 來登入遠程機器。ssh 命令你用得越多,你在鍵入一些重要的命令上花的時間也越多。我們可以用 定義在你的 .bashrc 文件里的別名 或函數(shù)來大幅度縮減花在命令行界面(CLI)的時間。但這不是最佳解決之道。最佳辦法是在 ssh 配置文件中使用 SSH 別名 。

[[225419]]

作為一個 Linux 用戶,我們常用 ssh 命令 來登入遠程機器。ssh 命令你用得越多,你在鍵入一些重要的命令上花的時間也越多。我們可以用 定義在你的 .bashrc 文件里的別名 或函數(shù)來大幅度縮減花在命令行界面(CLI)的時間。但這不是***解決之道。***辦法是在 ssh 配置文件中使用 SSH 別名 。

這里是我們能把 ssh 命令用得更好的幾個例子。

ssh 登入到 AWS(譯注:Amazon Web Services,亞馬遜公司旗下云計算服務平臺)實例的連接是一種痛。僅僅輸入以下命令,每次也完全是浪費你時間。

  1. ssh -p 3000 -i /home/surendra/mysshkey.pem ec2-user@ec2-54-20-184-202.us-west-2.compute.amazonaws.com

縮短到:

  1. ssh aws1

調試時連接到系統(tǒng)。

  1. ssh -vvv the_good_user@red1.taggle.abc.com.au

縮短到:

  1. 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 2

例 1: 創(chuàng)建主機(www.linuxnix.com)的 SSH 別名

編輯 ~/.ssh/config 文件寫入以下內容:

  1. Host tlj
  2. User root
  3. HostName 18.197.176.13
  4. port 22

保存此文件。

以上 ssh 別名用了

  1. tlj 作為一個別名的名稱
  2. root 作為將要登入的用戶
  3. 18.197.176.13 作為主機的 IP 地址
  4. 22 作為訪問 SSH 服務的端口

輸出:

  1. sanne@Surendras-MacBook-Pro:~ > ssh tlj
  2. Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-93-generic x86_64)
  3. * Documentation: https://help.ubuntu.com
  4. * Management: https://landscape.canonical.com
  5. * Support: https://ubuntu.com/advantage
  6. Get cloud support with Ubuntu Advantage Cloud Guest:
  7. http://www.ubuntu.com/business/services/cloud
  8. Last login: Sat Oct 14 01:00:43 2017 from 20.244.25.231
  9. root@linuxnix:~# exit
  10. logout
  11. Connection to 18.197.176.13 closed.

例 2: 不用密碼用 ssh 密鑰登到系統(tǒng)要用 IdentityFile 。

例:

  1. Host aws
  2. User ec2-users
  3. HostName ec2-54-200-184-202.us-west-2.compute.amazonaws.com
  4. IdentityFile ~/Downloads/surendra.pem
  5. port 22

例 3: 對同一主機使用不同的別名。在下例中,我們對同一 IP/主機 18.197.176.13 用了 tljlinuxnix、linuxnix.com 三個別名。

~/.ssh/config 文件內容

  1. Host tlj linuxnix linuxnix.com
  2. User root
  3. HostName 18.197.176.13
  4. port 22

輸出:

  1. sanne@Surendras-MacBook-Pro:~ > ssh tlj
  2. Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-93-generic x86_64)
  3. * Documentation: https://help.ubuntu.com
  4. * Management: https://landscape.canonical.com
  5. * Support: https://ubuntu.com/advantage
  6. Get cloud support with Ubuntu Advantage Cloud Guest:
  7. http://www.ubuntu.com/business/services/cloud
  8. Last login: Sat Oct 14 01:00:43 2017 from 220.244.205.231
  9. root@linuxnix:~# exit
  10. logout
  11. Connection to 18.197.176.13 closed.
  12. sanne@Surendras-MacBook-Pro:~ > ssh linuxnix.com
  13. Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-93-generic x86_64)
  14. * Documentation: https://help.ubuntu.com
  15. * Management: https://landscape.canonical.com
  16. * Support: https://ubuntu.com/advantage
  1. Get cloud support with Ubuntu Advantage Cloud Guest:
  2. http://www.ubuntu.com/business/services/cloud
  3. Last login: Sun Oct 15 20:31:08 2017 from 1.129.110.13
  4. root@linuxnix:~# exit
  5. logout
  6. Connection to 138.197.176.103 closed.
  7. [6571] sanne@Surendras-MacBook-Pro:~ > ssh linuxnix
  8. Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-93-generic x86_64)
  9. * Documentation: https://help.ubuntu.com
  10. * Management: https://landscape.canonical.com
  11. * Support: https://ubuntu.com/advantage
  12. Get cloud support with Ubuntu Advantage Cloud Guest:
  13. http://www.ubuntu.com/business/services/cloud
  14. Last login: Sun Oct 15 20:31:20 2017 from 1.129.110.13
  15. root@linuxnix:~# exit
  16. logout
  17. Connection to 18.197.176.13 closed.

例 4: 用相同的 SSH 別名復制文件到遠程系統(tǒng)

語法:

  1. scp <文件名> <ssh_別名>:<位置>

例子:

  1. sanne@Surendras-MacBook-Pro:~ > scp abc.txt tlj:/tmp
  2. abc.txt 100% 12KB 11.7KB/s 00:01
  3. sanne@Surendras-MacBook-Pro:~ >

若我們已經(jīng)將 ssh 主機設置好一個別名,由于 sshscp 兩者用幾乎相同的語法和選項,scp 也可以輕易使用。

請在下面嘗試從本機 scp 一個文件到遠程機器。

例 5: 解決 Linux 中的 SSH 超時問題。默認情況,如果你不積極地使用終端,你的 ssh 登入就會超時

SSH 超時問題 是一個更痛的點意味著你在一段時間后不得不重新登入到遠程機器。我們能在 ~/.ssh/config 文件里邊恰當?shù)卦O置 SSH 超時時間來使你的會話不管在什么時間總是激活的。我們將用 2 個能保持會話存活的 SSH 選項來實現(xiàn)這一目的。之一是 ServerAliveInterval 保持你會話存活的秒數(shù)和 ServerAliveCountMax 在(經(jīng)歷了一個)給定數(shù)值的會話之后初始化會話。

  1. ServerAliveInterval A
  2. ServerAliveCountMax B

例:

  1. Host tlj linuxnix linuxnix.com
  2. User root
  3. HostName 18.197.176.13
  4. port 22
  5. ServerAliveInterval 60
  6. ServerAliveCountMax 30

在下篇中我們將會看到一些其他的退出方式。 

責任編輯:龐桂玉 來源: Linux中國
相關推薦

2019-05-14 11:00:07

LinuxSSH別名

2013-11-13 13:55:16

Linux命令grep

2018-10-12 10:40:45

LinuxBash命令

2017-06-22 08:58:51

Hadoopssh+IPssh+

2018-01-22 09:12:18

Linuxbashssh

2021-05-21 09:05:56

Linux創(chuàng)建永久別名

2014-03-17 17:27:51

Linux mvLinux 命令

2018-04-27 10:55:52

2019-06-25 08:42:13

Linux命令指令

2015-09-28 11:22:55

命令行補全Linux

2023-06-06 19:01:38

縮寫ShellLinux

2018-01-16 10:08:25

Linuxbashshell

2018-08-03 11:07:52

dd命令備份Linux系統(tǒng)

2013-12-06 17:39:49

Linuxps命令

2020-10-15 19:10:05

LinuxAPI函數(shù)

2019-02-21 19:00:49

LinuxSSH圖形界面工具

2009-12-04 19:33:53

2009-09-03 14:04:12

LinuxSSH程管理

2018-10-08 09:35:59

2014-09-01 11:08:34

LinuxSSH
點贊
收藏

51CTO技術棧公眾號