AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 48808
Accepted
Simone Carletti
Simone Carletti
Asked: 2009-08-02 04:14:35 +0800 CST2009-08-02 04:14:35 +0800 CST 2009-08-02 04:14:35 +0800 CST

如何为通过 SSH 隧道的 SSH 连接配置快捷方式

  • 772

我公司的生产服务器(FOO、BAR...)位于两个网关服务器(A、B)后面。为了连接到服务器 FOO,我必须使用我的用户名 JOHNDOE 打开与服务器 A 或 B 的 ssh 连接,然后从 A(或 B)我可以访问任何使用标准用户名打开 SSH 连接的生产服务器(我们称之为韦比)。

所以,每次我必须做类似的事情:

ssh johndoe@a
...
ssh webby@foo
...
# now I can work on the server

可以想象,当我需要使用scp或需要快速打开多个连接时,这很麻烦。

我已经配置了一个 ssh 密钥,并且我正在使用 .ssh/config 作为一些快捷方式。

我想知道是否可以创建某种 ssh 配置以便输入

ssh foo

并让 SSH 为我打开/转发所有连接。可能吗?

编辑

womble 的答案正是我一直在寻找的,但现在我似乎无法使用 netcat,因为它没有安装在网关服务器上。

weppos:~ weppos$ ssh foo -vv
OpenSSH_5.1p1, OpenSSL 0.9.7l 28 Sep 2006
debug1: Reading configuration data /Users/xyz/.ssh/config
debug1: Applying options for foo
debug1: Reading configuration data /etc/ssh_config
debug2: ssh_connect: needpriv 0
debug1: Executing proxy command: exec ssh a nc -w 3 foo 22
debug1: permanently_drop_suid: 501
debug1: identity file /Users/xyz/.ssh/identity type -1
debug2: key_type_from_name: unknown key type '-----BEGIN'
debug2: key_type_from_name: unknown key type 'Proc-Type:'
debug2: key_type_from_name: unknown key type 'DEK-Info:'
debug2: key_type_from_name: unknown key type '-----END'
debug1: identity file /Users/xyz/.ssh/id_rsa type 1
debug2: key_type_from_name: unknown key type '-----BEGIN'
debug2: key_type_from_name: unknown key type 'Proc-Type:'
debug2: key_type_from_name: unknown key type 'DEK-Info:'
debug2: key_type_from_name: unknown key type '-----END'
debug1: identity file /Users/xyz/.ssh/id_dsa type 2
bash: nc: command not found
ssh_exchange_identification: Connection closed by remote host
ubuntu ssh ssh-tunnel
  • 8 8 个回答
  • 12757 Views

8 个回答

  • Voted
  1. Best Answer
    womble
    2009-08-02T05:23:57+08:002009-08-02T05:23:57+08:00

    作为凯尔答案的更具体版本,您要放入~/.ssh/config文件中的是:

    host foo
      User webby
      ProxyCommand ssh a nc -w 3 %h %p
    
    host a
      User johndoe
    

    然后,当您运行“ssh foo”时,SSH 将尝试 SSH 到johndoe@a,运行netcat( ),然后通过此隧道nc执行 SSH 到。webby@foo魔法!

    当然,为了做到这一点,需要在网关服务器上安装netcat;该软件包适用于每个主要发行版和操作系统。

    • 36
  2. Kyle Brandt
    2009-08-02T04:50:20+08:002009-08-02T04:50:20+08:00

    您可以在 ~/.ssh/config 文件中使用 ProxyCommand 指令,例如使用 netcat 作为中继:

    host server2
        ProxyCommand ssh server1 nc server2 22
    

    您只需使用“ssh server2”。该指令的手册页信息可在“man ssh_config”中找到

    • 7
  3. Insyte
    2009-08-02T10:10:48+08:002009-08-02T10:10:48+08:00

    我更喜欢另一种方法,即维护到网关服务器的预认证隧道。在~/.ssh/config:

    Host a
        ControlMaster auto
        ControlPath ~/.ssh/control-master/%r@%h:%p
    

    然后在.bashrc:

    s () {
            if ( ssh -O check a 2>&1 > /dev/null 2>&1 )
            then
                    ssh -t a ssh $1
            else
                    if [[ -S ~/.ssh/control-master/insyte@a:22 ]]
                    then
                            echo "Deleting stale socket..."
                            rm ~/.ssh/control-master/insyte@a:22
                    fi
                    echo "Opening master session..."
                    if ssh -Nf a
                    then
                             ssh -t a ssh $1
                    fi
            fi
     }
    

    所以要连接到 foo:

    s foo
    

    第一次连接时,它将根据“a”对您进行身份验证并打开一个持久的后台 ssh 隧道。随后对“s”的调用将通过预先认证的隧道几乎立即打开。

    效果很好。

    • 5
  4. Marcin
    2009-08-02T07:58:57+08:002009-08-02T07:58:57+08:00

    这可以通过做来完成ssh -At johndoe@a ssh webby@foo。该-A命令转发您的 ssh 代理(这样您就可以避免在代理上重新进行身份验证),同时-t确保代理上存在终端。以下 bash 函数可能有用:

    ssh-bounce () {
        local cmd=""
        for i in "$@"; do
            cmd+="ssh -At $i "
        done
        $cmd
    }
    
    • 2
  5. Scott Pack
    2011-12-28T17:00:25+08:002011-12-28T17:00:25+08:00

    这种类型的功能存在于较新版本的 OpenSSH 中,可以通过以下方式使用

    ssh -W server2 server1
    

    server2您的预定目的地在哪里,server1您的代理主机在哪里。您可以通过使用ProxyCommandssh 配置中的选项来简化此操作,例如:

    host = *.example.com
    user = packs
    port = 22
    ProxyCommand ssh -W %h:%p server1
    
    • 2
  6. Paul Price
    2012-08-24T10:48:50+08:002012-08-24T10:48:50+08:00

    当netcat代理上不可用时,试试这个技巧:

    host foo
      User webby      
      ProxyCommand ssh a 'exec 3<>/dev/tcp/foo/22; cat <&3 & cat >&3;kill $!'
    
    host a
      User johndoe
    

    那么你应该可以ssh foo。

    此外,如果您在 a 上安装了最新版本的 ssh(即,使用-W转发标准输入和输出的命令),您可以使用:

    host foo
      User webby
      ProxyCommand ssh -W foo:%p a
    
    host a
      User johndoe
    

    最后,仅仅因为我觉得它很酷(而不是因为它适用于您的特定情况,因为用户名不同),一位朋友的博客文章指出了如何使这种事情动态化并递归链接 SSH 代理(以及一些事情效果不好):

    host */*
      ProxyCommand ssh ${$(dirname %h)/\%%/@} nc ${$(basename %h)#*%%} %p
    

    然后ssh machine1/machine2应该给你一个shell machine2,隧道通过machine1。

    我想知道是否使用自定义sed命令代替dirname并且basename可能无法解决不同用户名的问题?

    • 2
  7. HBruijn
    2018-04-13T04:47:45+08:002018-04-13T04:47:45+08:00

    自 OpenSSH 7.3 (2016-08-01) 以来,该 ProxyJump选项和相应-J的命令行标志已可用于通过一个或多个 SSH 堡垒或“跳转主机”进行更简单的间接访问。

    这消除了对Womble 解决方案nc中的外部命令的依赖。

    ssh -J johndoe@a webby@foo 
    

    将以用户 johndoe 的身份建立从您的工作站到网关服务器的 SSH 连接,a并通过隧道将 SSH 会话通过该连接为用户 Webby 托管 foo。

    为了简化为您和您创建主机定义的~/.ssh/config过程,您可以简单地执行ssh foo

    Host a
        User johndoe
    
    Host foo
        User Webby 
        ProxyJump a
    
    • 2
  8. markdrayton
    2009-08-02T13:52:38+08:002009-08-02T13:52:38+08:00
    weppos:~ weppos$ ssh foo -vv
    [..]
    bash: nc: command not found
    

    Netcat 未安装在a. 当你运行时ssh host "command arg",在你的本地机器上而不是在你的本地机器上command执行。host

    • 0

相关问题

  • 如何在 Ubuntu 上设置简单的防火墙?

  • 设置没有密码的用户

  • 在 Ubuntu 上设置电子邮件服务器

  • 保护新的 Ubuntu 服务器 [关闭]

  • (软)Ubuntu 7.10 上的 RAID 6,我应该迁移到 8.10 吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve