Linux中的5个SSH别名例子

系统 Linux
作为一个 Linux 用户,我们常用 ssh 命令 来登入远程机器。ssh 命令你用得越多,你在键入一些重要的命令上花的时间也越多。我们可以用 定义在你的 .bashrc 文件里的别名 或函数来大幅度缩减花在命令行界面(CLI)的时间。但这不是最佳解决之道。最佳办法是在 ssh 配置文件中使用 SSH 别名 。

[[225419]]

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

  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 密钥登到系统要用 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 用了 tljlinuxnixlinuxnix.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 主机设置好一个别名,由于 sshscp 两者用几乎相同的语法和选项,scp 也可以轻易使用。

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

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

SSH 超时问题 是一个更痛的点意味着你在一段时间后不得不重新登入到远程机器。我们能在 ~/.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

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

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2019-05-14 11:00:07

LinuxSSH别名

2013-11-13 13:55:16

Linux命令grep

2017-06-22 08:58:51

Hadoopssh+IPssh+

2018-10-12 10:40:45

LinuxBash命令

2018-01-22 09:12:18

Linuxbashssh

2021-05-21 09:05:56

Linux创建永久别名

2014-03-17 17:27:51

Linux mvLinux 命令

2019-06-25 08:42:13

Linux命令指令

2023-06-06 19:01:38

缩写ShellLinux

2015-09-28 11:22:55

命令行补全Linux

2018-04-27 10:55:52

2018-01-16 10:08:25

Linuxbashshell

2013-12-06 17:39:49

Linuxps命令

2018-08-03 11:07:52

dd命令备份Linux系统

2020-10-15 19:10:05

LinuxAPI函数

2019-02-21 19:00:49

LinuxSSH图形界面工具

2009-12-04 19:33:53

2009-09-03 14:04:12

LinuxSSH程管理

2018-08-29 20:00:11

Linux命令行命令行别名

2010-06-23 17:16:33

Linux Bash
点赞
收藏

51CTO技术栈公众号