Arriving at a specified directory instead of the user's home

View: New views
6 Messages — Rating Filter:   Alert me  

Arriving at a specified directory instead of the user's home

by agostonbejo :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Hi!

By default when I ssh onto a machine, I arrive at the user's home directory.
Can I somehow specify upon ssh'ing that I would like to arrive at a different one?
Something like this:

ssh -target-directory=~/local/mystuff user1@themachine
password: ...
user1@themachine:/local/mystuff # ...

Setting up the target directory permanently won't do, because it varies where I would like to arrive.
Can it be done somehow?

P.S.: This is the version I'm using: "OpenSSH_4.2p1, OpenSSL 0.9.8a 11 Oct 2005"


Thanks!
Agoston

Re: Arriving at a specified directory instead of the user's home

by greenrd :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

That's easy. Just create this script, calling it something
like /usr/local/bin/arrive

#! /bin/sh
# Go to the requested directory
cd ${1}
# Find out the user's preferred shell and execute it
exec `getent passwd $USER|sed -e 's|.*:\(.*\)$|\1|'`

Then, to login, type

ssh -t [your usual ssh parameters] /usr/local/bin/arrive ~/local/mystuff

(You can omit the directory, /usr/local/bin in this case, if it's on
the user's PATH.)

It works for me. Let me know if for some reason it doesn't work on your
system. Note that you need to specify -t in order to get a proper
terminal allocated - since you're using a script, openssh assumes that
you won't need a proper terminal, so you have to tell it otherwise.
--
Robin

On Sat, 10 Mar 2007 05:10:18 -0800 (PST)
agostonbejo <bejoag1@...> wrote:

>
> Hi!
>
> By default when I ssh onto a machine, I arrive at the user's home
> directory. Can I somehow specify upon ssh'ing that I would like to
> arrive at a different one?
> Something like this:
>
> ssh -target-directory=~/local/mystuff user1@themachine
> password: ...
> user1@themachine:/local/mystuff # ...
>
> Setting up the target directory permanently won't do, because it
> varies where I would like to arrive.
> Can it be done somehow?
>
> P.S.: This is the version I'm using: "OpenSSH_4.2p1, OpenSSL 0.9.8a
> 11 Oct 2005"
>
>
> Thanks!
> Agoston


--
Robin

RE: Arriving at a specified directory instead of the user's home

by Dr Joe :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Why would you want to do this? If you are ssh'ing into arbitrary directories
as root (? - again why) then the user is irrelevant - unless you are doing
something else?
First of all, set up ssh so that there is public key exchange,
rather than entering a password all the time (put your key into the right
file under ~/.ssh/ )
then just do

ssh host.domain.etc "cd /dir/you/want"

You may/may not need a -c switch in between. You will probably
need the quotes so that the remote shell does not interpret the
command as two separate args.

Good luck.
Dr Joe Haskian

-----Original Message-----
From: listbounce@... [mailto:listbounce@...] On
Behalf Of agostonbejo
Sent: Sunday, March 11, 2007 12:10 AM
To: secureshell@...
Subject: Arriving at a specified directory instead of the user's home


Hi!

By default when I ssh onto a machine, I arrive at the user's home directory.
Can I somehow specify upon ssh'ing that I would like to arrive at a
different one?
Something like this:

ssh -target-directory=~/local/mystuff user1@themachine
password: ...
user1@themachine:/local/mystuff # ...

Setting up the target directory permanently won't do, because it varies
where I would like to arrive.
Can it be done somehow?

P.S.: This is the version I'm using: "OpenSSH_4.2p1, OpenSSL 0.9.8a 11 Oct
2005"


Thanks!
Agoston
--
View this message in context:
http://www.nabble.com/Arriving-at-a-specified-directory-instead-of-the-user%
27s-home-tf3380890.html#a9410209
Sent from the SSH (Secure Shell) mailing list archive at Nabble.com.




Re: Arriving at a specified directory instead of the user's home

by Greg Wooledge :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

On Sat, Mar 10, 2007 at 05:10:18AM -0800, agostonbejo wrote:
> By default when I ssh onto a machine, I arrive at the user's home directory.
> Can I somehow specify upon ssh'ing that I would like to arrive at a
> different one?
> Something like this:
>
> ssh -target-directory=~/local/mystuff user1@themachine
> password: ...
> user1@themachine:/local/mystuff # ...

ssh -t user1@themachine 'cd ~/local/mystuff && exec bash'

Keep in mind that since you are no longer running a login shell, you
may not have the full set of environment variables you expect (since
your /etc/profile and ~/.{bash_,}profile won't be sourced).  You can
change that by forcing a login shell in the exec part.

If you don't use bash as your shell, substitute appropriately.

Re: Arriving at a specified directory instead of the user's home

by Greg Wooledge :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

On Mon, Mar 12, 2007 at 07:03:55PM +0000, Robin Green wrote:
> # Find out the user's preferred shell and execute it
> exec `getent passwd $USER|sed -e 's|.*:\(.*\)$|\1|'`

$USER is far, far less standard than $LOGNAME.  Also, getent(1) is only
present on a very small cross-section of unix-like systems.

Why not simply use $SHELL?

Re: Arriving at a specified directory instead of the user's home

by agostonbejo :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

Thank you all,

the solution worked well, the point was this 'exec bash' part I didn't know about. Great!