您的位置:首页 > 运维架构 > Linux

Linux 中的 5 个 SSH 别名例子 | Linux 中国

2018-04-10 13:35 106 查看
我们可以用 定义在你的 .bashrc 文件里的别名 或函数来大幅度缩减花在命令行界面(CLI)的时间。但这不是最佳解决之道。最佳办法是在 ssh 配置文件中使用 SSH 别名 。
-- Surendra Anne

致谢
编译自 | https://www.linuxnix.com/5-ssh-alias-examples-using-ssh-config-file/ 
 作者 | Surendra Anne
 译者 | ch-cn 🌟 🌟 共计翻译:4 篇 贡献时间:574 天

作为一个 Linux 用户,我们常用 ssh 命令[1] 来登入远程机器。ssh 命令你用得越多,你在键入一些重要的命令上花的时间也越多。我们可以用 定义在你的 .bashrc 文件里的别名[2] 或函数来大幅度缩减花在命令行界面(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

调试时连接到系统。

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

缩短到:

  1. ssh xyz

在本篇中,我们将看到如何不使用 bash 别名或函数实现 ssh 命令的缩短。ssh 别名的主要优点是所有的 ssh 命令快捷方式都存储在一个单一文件,如此就易于维护。其他优点是 对于类似于 SSH 和 SCP 的命令 我们能用相同的别名。

在我们进入实际配置之前,我们应该知道 /etc/ssh/ssh_config/etc/ssh/sshd_config 和 ~/.ssh/config 文件三者的区别。以下是对这些文件的解释。

/etc/ssh/ssh_config 和 ~/.ssh/config 间的区别

系统级别的 SSH 配置项存放在 /etc/ssh/ssh_config,而用户级别的 SSH 配置项存放在 ~/.ssh/config 文件中。

/etc/ssh/sshconfig 和 /etc/ssh/sshdconfig 间的区别

系统级别的 SSH 配置项是在 /etc/ssh/ssh_config 文件中,而系统级别的 SSH 服务端配置项存放在 /etc/ssh/sshd_config 文件。

在 ~/.ssh/config 文件里配置项的语法

~/.ssh/config 文件内容的语法:

  1. 配置项

  2. 配置项 1 2

例 1: 创建主机(www.linuxnix.com)的 SSH 别名

编辑 ~/.ssh/config 文件写入以下内容:

  1. Host tlj

  2. User root

  3. HostName 18.197.176.13

  4. port 22

保存此文件。

以上 ssh 别名用了

☉ tlj 作为一个别名的名称
☉ root 作为将要登入的用户
☉ 18.197.176.13 作为主机的 IP 地址
☉ 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 密钥登到系统要用 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 用了 tlj、 linuxnixlinuxnix.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 别名复制文件到远程系统

语法:

  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:~ >

若我们已经将 ssh 主机设置好一个别名,由于 ssh 和 scp 两者用几乎相同的语法和选项,scp 也可以轻易使用。

请在下面尝试从本机 scp 一个文件到远程机器。

例 5: 解决 Linux 中的 SSH 超时问题。默认情况,如果你不积极地使用终端,你的 ssh 登入就会超时

SSH 超时问题[3] 是一个更痛的点意味着你在一段时间后不得不重新登入到远程机器。我们能在 ~/.ssh/config 文件里边恰当地设置 SSH 超时时间来使你的会话不管在什么时间总是激活的。我们将用 2 个能保持会话存活的 SSH 选项来实现这一目的。之一是 ServerAliveInterval 保持你会话存活的秒数和 ServerAliveCountMax 在(经历了一个)给定数值的会话之后初始化会话。

  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

在下篇中我们将会看到一些其他的退出方式。


via: https://www.linuxnix.com/5-ssh-alias-examples-using-ssh-config-file/

作者:SURENDRA ANNE[5] 译者:ch-cn 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: