如何直接通过 ssh 连接到特定目录? [英] How can I ssh directly to a particular directory?

查看:50
本文介绍了如何直接通过 ssh 连接到特定目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常需要登录到几台服务器之一,然后转到这些机器上的几个目录之一.目前我做这样的事情:

<前>本地主机 ~]$ ssh 某个主机欢迎来到某个主机!somehost ~]$ cd/some/directory/somewhere/named/Foosomehost Foo]$

我有脚本可以确定我需要进入哪个主机和哪个目录,但我无法找到一种方法来做到这一点:

<前>本地主机 ~]$ go_to_dir Foo欢迎来到某个主机!somehost Foo]$

有没有简单、聪明或任何方法可以做到这一点?

解决方案

您可以执行以下操作:

ssh -t xxx.xxx.xxx.xxx "cd/directory_wanted ;bash --登录"

这样,您将在 directory_wanted 上获得一个登录 shell.


说明

<块引用>

-t 强制伪终端分配.这可用于在远程机器上执行任意基于屏幕的程序,这非常有用,例如实施菜单服务时.

多个 -t 选项强制 tty 分配,即使 ssh 没有本地 tty.

  • 如果您不使用 -t 则不会出现提示.
  • 如果不加;bash 然后连接将关闭并将控制权返回到您的本地机器
  • 如果你不添加 bash --login 那么它就不会使用你的配置,因为它不是一个登录 shell

I often have to login to one of several servers and go to one of several directories on those machines. Currently I do something of this sort:

localhost ~]$ ssh somehost

Welcome to somehost!

somehost ~]$ cd /some/directory/somewhere/named/Foo
somehost Foo]$ 

I have scripts that can determine which host and which directory I need to get into but I cannot figure out a way to do this:

localhost ~]$ go_to_dir Foo

Welcome to somehost!

somehost Foo]$

Is there an easy, clever or any way to do this?

解决方案

You can do the following:

ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted ; bash --login"

This way, you will get a login shell right on the directory_wanted.


Explanation

-t Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services.

Multiple -t options force tty allocation, even if ssh has no local tty.

  • If you don't use -t then no prompt will appear.
  • If you don't add ; bash then the connection will get closed and return control to your local machine
  • If you don't add bash --login then it will not use your configs because its not a login shell

这篇关于如何直接通过 ssh 连接到特定目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆