9

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.

2 Answers 2

16

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.

1

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
2
  • 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.
    – Emmett R.
    Commented Jan 29, 2015 at 18:22
  • 1
    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.
    – undefine
    Commented Jan 30, 2015 at 23:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .