bash 如何直接 ssh 到特定目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/626533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I ssh directly to a particular directory?
提问by Frosty
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?
有没有简单,聪明或任何方法来做到这一点?
回答by rogeriopvl
You can do the following:
您可以执行以下操作:
ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted ; bash"
This way, you will get a shell right on the directory_wanted.
这样,您将在 directory_wanted 上获得一个 shell。
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.
-t
强制伪终端分配。这可用于在远程机器上执行任意基于屏幕的程序,这非常有用,例如在实现菜单服务时。多个
-t
选项强制 tty 分配,即使 ssh 没有本地 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
- 如果您不使用,
-t
则不会出现提示。 - 如果您不添加,
; bash
则连接将关闭并将控制权返回给您的本地计算机
回答by Chris Lutz
You could add
你可以添加
cd /some/directory/somewhere/named/Foo
to your .bashrc
file (or .profile
or whatever you call it) at the other host. That way, no matter what you do or where you ssh
from, whenever you log onto that server, it will cd
to the proper directory for you, and all you have to do is use ssh
like normal.
到另一台主机上的.bashrc
文件(或.profile
任何您称之为的文件)。这样,无论您做什么或ssh
来自哪里,无论何时登录该服务器,它都会cd
为您找到正确的目录,您所要做的就是ssh
像往常一样使用。
Of curse, rogeriopvl's solution works too, but it's a tad bit more verbose, and you have to remember to do it every time (unless you make an alias) so it seems a bit less "fun".
当然,rogeriopvl 的解决方案也有效,但它有点冗长,您必须记住每次都这样做(除非您使用别名),因此它似乎不那么“有趣”。
回答by christianbundy
I've created a tool to SSH and CD into a server consecutively – aptly named sshcd. For the example you've given, you'd simply use:
我已经创建了一个工具,可以将 SSH 和 CD 连续连接到服务器 - 恰如其分地命名为sshcd。对于您给出的示例,您只需使用:
sshcd somehost:/some/directory/somewhere/named/Foo
Let me know if you have any questions or problems!
如果您有任何疑问或问题,请告诉我!
回答by Drew
My preferred approach is using the SSH config file (described below), but there are a few possible solutions depending on your usages.
我的首选方法是使用 SSH 配置文件(如下所述),但根据您的使用情况,有几种可能的解决方案。
Command Line Arguments
命令行参数
I think the best answer for this approach is christianbundy's reply to the accepted answer:
我认为这种方法的最佳答案是 christianbundy 对已接受答案的回复:
ssh -t example.com "cd /foo/bar; exec $SHELL -l"
Using double quotes will allow you to use variables from your local machine, unless they are escaped (as $SHELL
is here). Alternatively, you can use single quotes, and all of the variables you use will be the ones from the target machine:
使用双引号将允许您使用本地机器中的变量,除非它们被转义(如此$SHELL
处)。或者,您可以使用单引号,并且您使用的所有变量都将是来自目标机器的变量:
ssh -t example.com 'cd /foo/bar; exec $SHELL -l'
Bash Function
Bash 函数
You can simplify the command by wrapping it in a bash function. Let's say you just want to type this:
您可以通过将其包装在 bash 函数中来简化命令。假设您只想输入以下内容:
sshcd example.com /foo/bar
You can make this work by adding this to your ~/.bashrc
:
您可以通过将其添加到您的~/.bashrc
:
sshcd () { ssh -t "" "cd \"\"; exec $SHELL -l"; }
If you are using a variable that exists on the remote machine for the directory, be sure to escape it or put it in single quotes. For example, this will cd to the directory that is stored in the JBOSS_HOME
variable on the remote machine:
如果您使用远程计算机上存在的变量作为目录,请确保对其进行转义或将其放在单引号中。例如,这将 cd 到存储在JBOSS_HOME
远程机器上的变量中的目录:
sshcd example.com $JBOSS_HOME
SSH Config File
SSH 配置文件
If you'd like to see this behavior all the time for specific (or any) hosts with the normal ssh command without having to use extra command line arguments, you can set the RequestTTY
and RemoteCommand
options in your ssh config file.
如果您希望使用普通 ssh 命令始终看到特定(或任何)主机的这种行为,而不必使用额外的命令行参数,您可以在 ssh 配置文件中设置RequestTTY
和RemoteCommand
选项。
For example, I'd like to type only this command:
例如,我只想输入以下命令:
ssh qaapps18
but want it to always behave like this command:
但希望它总是像这个命令一样:
ssh -t qaapps18 'cd $JBOSS_HOME; exec $SHELL'
So I added this to my ~/.ssh/config
file:
所以我将此添加到我的~/.ssh/config
文件中:
Host *apps*
RequestTTY yes
RemoteCommand cd $JBOSS_HOME; exec $SHELL
Now this rule applies to any host with "apps" in its hostname.
现在,此规则适用于任何主机名中带有“apps”的主机。
For more information, see http://man7.org/linux/man-pages/man5/ssh_config.5.html
有关更多信息,请参阅http://man7.org/linux/man-pages/man5/ssh_config.5.html
回答by Bas Peeters
Based on additions to @rogeriopvl's answer, I suggest the following:
根据对@rogeriopvl 的回答的补充,我建议如下:
ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted && bash"
Chaining commands by &&
will make the next command run only when the previous one was successful (as opposed to using ;
, which executes commands sequentially). This is particularly useful when needing to cd
to a directory performing the command.
链接命令 by&&
将使下一个命令仅在前一个命令成功时运行(与使用相反;
,后者按顺序执行命令)。这在需要cd
执行命令的目录时特别有用。
Imagine doing the following:
想象一下做以下事情:
/home/me$ cd /usr/share/teminal; rm -R *
The directory teminal
doesn't exist, which causes you to stay in the home directory and remove all the files in there with the following command.
该目录teminal
不存在,这会导致您留在主目录中并使用以下命令删除其中的所有文件。
If you use &&
:
如果您使用&&
:
/home/me$ cd /usr/share/teminal && rm -R *
The command will fail after not finding the directory.
找不到目录后该命令将失败。
回答by Mauricio Reis
In my very specific case, I just wanted to execute a command in a remote host, inside a specific directory from a Jenkins slave machine:
在我非常具体的情况下,我只想在远程主机中执行命令,在 Jenkins 从机的特定目录中:
ssh myuser@mydomain
cd /home/myuser/somedir
./commandThatMustBeRunInside_somedir
exit
But my machine couldn't perform the ssh (it couldn't allocate a pseudo-tty I suppose) and kept me giving the following error:
但是我的机器无法执行 ssh(我想它无法分配伪 tty)并让我给出以下错误:
Pseudo-terminal will not be allocated because stdin is not a terminal
I could get around this issue passing "cd to dir + my command" as a parameter of the ssh command (to not have to allocate a Pseudo-terminal) and by passing the option -T to explicitly tell to the ssh command that I didn't need pseudo-terminal allocation.
我可以解决这个问题,将“cd to dir + my command”作为 ssh 命令的参数(不必分配伪终端)并通过传递选项 -T 来明确告诉 ssh 命令我没有'不需要伪终端分配。
ssh -T myuser@mydomain "cd /home/myuser/somedir; ./commandThatMustBeRunInside_somedir"
回答by Eddy
I use the environment variable CDPATH
我使用环境变量 CDPATH
回答by insomiac
Another way of going to directly after logging in is create "Alias". When you login into your system just type that alias and you will be in that directory.
登录后直接进入的另一种方法是创建“别名”。当您登录系统时,只需键入该别名,您就会进入该目录。
Example : Alias = myfolder '/var/www/Folder'
示例:别名 = myfolder '/var/www/Folder'
After you log in to your system type that alias (this works from any part of the system)
this command if not in bashrc will work for current session. So you can also add this alias to bashrc to use that in future
登录到系统后,键入该别名(这适用于系统的任何部分),
如果不在 bashrc 中,此命令将适用于当前会话。因此,您还可以将此别名添加到 bashrc 以供将来使用
$ myfolder => takes you to that folder
$ myfolder => 带你到那个文件夹
回答by DKebler
going one step further with the -t
idea. I keep a set of scripts calling the one below to go to specific places in my frequently visited hosts. I keep them all in ~/bin
and keep that directory in my path.
更进一步的-t
想法。我保留了一组脚本,调用下面的脚本以转到我经常访问的主机中的特定位置。我把它们都放在里面~/bin
,把那个目录放在我的路径中。
#!/bin/bash
# does ssh session switching to particular directory
# , hostname from config file
# , directory to move to after login
# can save this as say 'con' then
# make another script calling this one, e.g.
# con myhost repos/i2c
ssh -t "cd ; exec $SHELL --login"
回答by Jan Jungnickel
SSH itself provides a means of communication, it does not know anything about directories. Since you can specify which remote command to execute (this is - by default - your shell), I'd start there.
SSH 本身提供了一种通信方式,它对目录一无所知。由于您可以指定要执行的远程命令(这是 - 默认情况下 - 您的 shell),我将从那里开始。