SSH 使用技巧一则: 创建快捷方式

在管理服务器时,我通常选择使用 SSH 方式。以下是一则 SSH 使用技巧,希望对你有用。

创建快捷方式

当你在执行 ssh 命令登录服务器时,有没有被需要输入命令后面的一长串参数感到厌烦呢?比如,名为 serveradmin@domain.com 的用户要登录到 example.com 主机上,需执行:

ssh serveradmin@domain.com@example.com

你当然可以使用 alias,但 SSH 本身也提供有相应的解决方案──你可以为需要经常访问的远程主机创建快捷方式。

  1. 找找看你的用户主目录下是否有 .ssh,若没有,则使用 mkdir 创建一个;

  2. 使用你喜欢的文本编辑器(如 Vim)来创建 config 配置文件:

    vim ~/.ssh/config
    
  3. 仍以前面的例子来说明,假设我要创建的快捷方式名为 lt,则加入下面的内容,其中 HostName 为主机名,User 为用户名:

    Host lt
    
        HostName example.com
        User serveradmin@domain.com
    
  4. 保存编辑。

现在,你只要执行 ssh lt 就可以了。

Read More:

31 Comments

  1. 1 Rainux Commented @ 2009-04-15 12:04Reply to this comment

    啊,这技巧太好了,谢谢楼主。

  2. 2 songzi Commented @ 2009-04-15 12:05Reply to this comment

    很有用,谢谢分享。

  3. 3 hzxu Commented @ 2009-04-15 12:09Reply to this comment

    如果把用户名密码也写上要如何写?是不是不安全?

  4. 4 Iven Commented @ 2009-04-15 12:16Reply to this comment

    呵呵,不错这个~

  5. 5 Rainux Commented @ 2009-04-15 12:19Reply to this comment

    @hzxu: 你应该用 public key 来进行身份认证,而不是把密码写到配置文件里。

  6. 6 Rainux Commented @ 2009-04-15 12:28Reply to this comment

    在 ~/.ssh/config 里还可以配置其他很多参数,其中比较常用的如 DynamicForward 和 ForwardX11Trusted,意义分别跟 ssh 命令的 -D 和 -X 参数一致。参考 man ssh_config。

    Host xxx
        HostName xxx.xxx.xxx
        User root
        DynamicForward 9527
    
    Host yyy
        HostName yyy.yyy.yyy
        User rainux
        ForwardX11 yes
    
  7. 7 ydddd Commented @ 2009-04-15 12:30Reply to this comment

    哈哈,真是我想要的。

  8. 8 jj # 4. Aliases Commented @ 2009-04-15 12:36Reply to this comment

    export host127=..***.127 alias go127="ssh username@$host127"

    ----my .bashrc----

  9. 9 hmy Commented @ 2009-04-15 12:45Reply to this comment

    不错的技巧

  10. 10 ricky Commented @ 2009-04-15 12:55Reply to this comment

    这个技巧太帅了,偶喜欢

  11. 11 ebird Commented @ 2009-04-15 13:05Reply to this comment

    cssh也支持主机列表…… 不过似乎xshell的输出是实时的……比如yum的时候显示的内容就有点另类……

  12. 12 GunsNRose Commented @ 2009-04-15 13:18Reply to this comment

    的确是个很实用的技巧,收藏

  13. 13 latteye Commented @ 2009-04-15 13:23Reply to this comment

    这种方式其实更多的用在给不同的主机使用不同的配置。比如你有些机器要用代理,有些不用。用这种方式区分开就很方便了。

    ssh 的 外加命令很丰富。

  14. 14 whycrying Commented @ 2009-04-15 13:34Reply to this comment

    $ vi .ssh/config Host 116 HostName 192.168.1.116 User zkc

    $ ssh 116 Bad owner or permissions on /home/xxx/.ssh/config

    =================== 是不是 User 要和文件 .ssh/config 所属的 user 相同呢?

  15. 15 Tenyears Commented @ 2009-04-15 14:11Reply to this comment

    @whycring 不需要

  16. 16 Kardinal Commented @ 2009-04-15 14:37Reply to this comment
    my_accounts=(
    {r00t,root}@{192.168.1.1,192.168.0.1}
    Kardinal@linuxtoy.org
    123@192.168.0.123
    )
    zstyle ':completion:*:my-accounts' users-hosts $my_accounts
    
    >>> ssh                                                                             -- remote host name --
    192.168.0.1    192.168.0.123  192.168.1.1    linuxtoy.org
     -- login name --
    123            Kardinal       r00t           root
  17. 17 Kardinal Commented @ 2009-04-15 14:45Reply to this comment

    麻烦 toy 兄把上面的那个删除

    16 ">>>" 后面的部分给加个 code 标签 -_-!!!

    还找到一种,不清楚什么状况

    local knownhosts
    knownhosts=( ${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[0-9]*}%%\ *}%%,*} )
    zstyle ':completion:*:(ssh|scp|sftp):*' hosts $knownhosts

    再look 下其它的……

  18. 18 Kardinal Commented @ 2009-04-15 14:46Reply to this comment

    据说上面那种 ~/.ssh/config 中要设置 HashKnownHosts no

  19. 19 Toy Commented @ 2009-04-15 15:20Reply to this comment

    @Rainux, @latteye: 是的,这个 config 文件的用途不只本文所介绍的一种。

    @Kardinal: 已删。

  20. 20 Rainux Commented @ 2009-04-15 15:58Reply to this comment

    使用 shell alias 虽然也可以达到类似的目的,但是 .ssh/config 可以作用于其他 ssh 工具,例如 scp。甚至一些使用 openssl library 的第三方工具也可能可以使用它。

  21. 21 walkerxk Commented @ 2009-04-15 16:05Reply to this comment

    很多常用的命令(apt-get dpkg find firefox rsync run scp ssh synaptic unlink wine youtube-dl)都被我用脚本改写了,放在/home/bin下面,我是直接调用那些脚本,然后再调用这些命令,所以这个对我来说意义不大。

  22. 22 stamilo Commented @ 2009-04-15 17:20Reply to this comment

    我的是这样的 :) cat .ssh/config host u2 hostname 192.168.2.2 port 22 Compression yes PreferredAuthentications publickey PubkeyAuthentication yes IdentityFile ~/.ssh/ubuntu user luohy

  23. 23 freeren Commented @ 2009-04-15 18:12Reply to this comment

    ~/.ssh/config This is the per-user configuration file. The file format and configuration options are described in ssh_config(5). Because of the potential for abuse, this file must have strict permissions: read/write for the user, and not accessible by others. It may be group-writable provided that the group in question contains only the user.

  24. 24 bb Commented @ 2009-04-15 21:33Reply to this comment

    @walkerxk: 那你重装系统的是时候是不是很费劲阿

  25. 25 Mond Commented @ 2009-04-15 22:58Reply to this comment

    This is what I do.

    Host xx HoseName xx.xx.xx.edu User mond

    then ssh xx will do the work.

  26. 26 bones7456 Commented @ 2009-04-16 10:55Reply to this comment

    恩,这个也不错,不过我还是用自己的脚本,主要是要经常多级登录 http://li2z.cn/2008/06/29/sssh/

  27. 27 zhichyu Commented @ 2009-04-16 21:06Reply to this comment

    @Rainux: 怎样配置SSH使用public key而不再提示输入password?

  28. 28 zhichyu Commented @ 2009-04-17 10:08Reply to this comment

    找到配置SSH证书的权威指导了:http://sial.org/howto/openssh/publickey-auth/。步骤没有想象中的那么复杂:-)

  29. 29 zhichyu Commented @ 2009-04-17 10:52Reply to this comment

    SSH公钥认证并不是SSL证书认证。不好意思,我的上个comment把概念弄混淆了。

  30. 30 davix Commented @ 2009-04-17 19:16Reply to this comment

    若ssh server端口非22,也可在config裏加上Port 另外,這種方法用在cvs中很方便,用相同的CVSROOT可以指定不同的主機.

  31. 31 wayhome Commented @ 2009-04-18 22:02Reply to this comment

    不错,不过偶更喜欢reStructuredText