Different ssh login directory from user home

19,183

Solution 1

In your ~/.bashrc or ~/.bash_profilefile, put this at the end of the file:

cd /path/to/your/destination

Save the file and log out and log back in, you should be in /path/to/your/destination.


You could also create an alias on your local account, edit your local ~/.bashrc and add:

alias fastlogin='ssh servername -t "cd /path/to/your/destination; exec bash --login"'

Source your file so changes take effect:

source ~/.bashrc 

Now test it by typing fastlogin in your terminal.

You require bash at the end so the connection doesn't terminate after cd executes and --login is required so it sources your ~/.bashrc & ~/.bash_profile files.

Solution 2

user home is pointed by HOME variable. So - if you want change it - you can change HOME variable in .bashrc, .bash_profile or something. Look:

undefine@uml:~$ echo $HOME
/home/undefine
undefine@uml:~$ export HOME=/tmp
undefine@uml:/home/undefine$ cd ~
undefine@uml:~$ pwd
/tmp
Share:
19,183

Related videos on Youtube

Emmett R.
Author by

Emmett R.

I'm something of a cross between a mechanical engineer, an IT professional, and a book editor. While I don't know everything, I am unshakable in the quest for more knowledge, and I have a memory like a steel trap.

Updated on September 18, 2022

Comments

  • Emmett R.
    Emmett R. over 1 year

    So, I'm working on a remote server via ssh that I log in and out of dozens of times a day, and I'd like to have bash cd to a default directory I've chosen as soon as I login, but I don't actually want to change the user home. Is there any simple way to do that?

    To be clear, what I want is to see, say, ~/foo/bar/ when I login, instead of ~/, and have the option to change the default at will without having to worry about dangerous usermod craziness.

    It's not important, but it would definitely be convenient.

  • Emmett R.
    Emmett R. over 9 years
    But will that cause any trouble with the .ssh directory that holds my private keys? I'd rather use a cd command than risk losing access.
  • undefine
    undefine over 9 years
    if you mean authorized_keys - no. .ssh path is defined in sshd_config(AuthorizedKeysFile) and is related to gecos entry. Any changes of HOME variable done after login has no impact to authorization process.