2

No, it is not a duplicate of that, because I want to assign one directory to one (or more) user, when they log in throught SSH.

For example, according to user's loggin :

  • userFoo => I want him to arrive /somewhere/here/ after SSH login
  • userBar => I want him to arrive /somewhere/here/ after SSH login
  • userOof => I want him to arrive /anywhere/ after SSH login
  • And so on.

How can I do that ? (They are connection with PuTTY and I can't force them to connect with additionnal data like ssh -t user@server 'cd /home/some/dir ; exec "$SHELL"'

Community
  • 1
  • 1
4wk_
  • 2,458
  • 3
  • 34
  • 46

2 Answers2

2

Change the home directories of your users, as that's the place where they land after connecting.

The second to last entry in /etc/passwd holds this information, and can be edited for example from

userFoo:x:1000:1000:,,,:/home/userFoo:/bin/bash

to

userFoo:x:1000:1000:,,,:/somewhere/here:/bin/bash
kwood
  • 9,854
  • 2
  • 28
  • 32
  • The user is still able to `cd /home/userFoo` to get back to his home dir. I don't get the OP's point :-S – Daniel W. Oct 22 '13 at 13:16
  • 2
    The OP didn't state he needs a chrooted environment. Sometimes it's just for convenience. "Connect to theserver.com and edit index.html" or "Connect and type _touch tmp/restart.txt_" is straight forward, no need to instruct someone to change to /my/fancy/path/where/you/propably/type/a/wrong/letter where someone could get "lost" and doesn't know how to recover. – kwood Oct 22 '13 at 13:36
  • @DanFromGermany There's no indication that the OP wants to confine users to the desired directory, just to make that the default on login. The physical directory `/home/userFoo` could simply be deleted if necessary, because `/somewhere/here` is now the user's real home directory. Permissions for `userFoo` can also be adjusted to prevent him from accessing other directories. – chepner Oct 22 '13 at 13:56
  • 1
    We have to agree the OP is not clear about the purpose. 1) There's no need to grant someone access to SSH not able to move between directories on unix. 2) If the origin home gets deleted, and the home moved to /somewhere/here.. what's the benefit of that? – Daniel W. Oct 22 '13 at 14:00
  • As @kwood said : `Sometimes it's just for convenience.` => It is my purpose. It's also a gain of time, for non-experimented users. @kwood : Works perfectly as expected, thank you. – 4wk_ Oct 22 '13 at 14:48
0

When you log in a remote machine via SSH, it starts a shell, in most cases it's bash. It has a script, which executes every time it starts, can be found at ~/.bashrc. You may edit them, append a cd /somewhere to them.

Anyway, it's probably not a good idea, it's better to link appropiate folders to the users:

ln -s /somewhere/here ~/userFoo/comehere
ln -s /somewhere/here ~/user/comehere
ln -s /anywhere ~/userOof/comehere

So, you may just tell the users "please perform a cd comehere after you log in", it will drop them to the specified folders.

ern0
  • 3,074
  • 25
  • 40