How can I ssh directly to a particular directory?

2019-01-16 00:33发布

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?

10条回答
爷、活的狠高调
2楼-- · 2019-01-16 01:11

simply modify your home with the command: usermod -d /newhome username

查看更多
混吃等死
3楼-- · 2019-01-16 01:15

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'

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

$ myfolder => takes you to that folder

查看更多
Explosion°爆炸
4楼-- · 2019-01-16 01:18

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.

#!/bin/bash

# does ssh session switching to particular directory
# $1, hostname from config file 
# $2, 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 $1 "cd $2; exec \$SHELL --login"
查看更多
闹够了就滚
5楼-- · 2019-01-16 01:19

I use the environment variable CDPATH

查看更多
登录 后发表回答