40

I was wondering if there is a way to change the default directory that I get put into after I SSH into my Ubuntu server.

99% of the time when I'm logging into my server, it is to access files within a specific directory:

/var/www/websites

Is there a config file that I can edit that will make sure I am put straight into this directory when I login?

59

There are two ways to achieve this:

  • Change your homedirectory on the server to /var/www/websites (this is not really a good idea)
  • add cd /var/www/websites to the end of your .bashrc. I use this one on our puppetmasters as I always want to be in /etc/puppet/environments/dkaarsemaker there instead of my homedir :-)
  • Thanks. I edited the bash.bashrc file which was located in the /etc directory. Worked a treat :) – Bob Flemming Apr 16 '13 at 8:56
  • 7
    Ooh, I would not do that, as it affects all users. Better to edit /home/yourlogin/.bashrc – Dennis Kaarsemaker Apr 16 '13 at 20:16
  • 5
    You may want to put this in your .profile instead of .bashrc, but it depends on your use case. .profile is executed only for interactive logins (e.g. shell) but .bashrc is also executed for noninteractive logins (e.g. scp, rsync, etc.). Also, .profile is more likely to be called by shells which are not bash (e.g. zsh). – phord Mar 22 '17 at 17:37
  • .bashrc is executed when you do "exec bash" to refresh your bash. Changing the bashrc for a special use case (here ssh login) is not useful. – user3123159 Jan 20 at 19:02
12

If you use keys for SSH login then you can change path by prepending command= to before a key in ~/.ssh/authorized_keys on your remote server. Example:

command="cd /var/www/websites ; /bin/bash -l" ssh-rsa AAA.....rest of the key

It is fine to generate and use multiple keys for the same user. One key on the server may contain the command the other may not - this way you select expected behaviour at login time. You can simply wrap it up with local ~/.ssh/config:

Host websites-my-host
    HostName <realhostname>
    IdentityFile ~/.ssh/<key1>  #on the server key with "command"
    User webmaster

Host my-host
    HostName <realhostname>
    IdentityFile ~/.ssh/<key2>  #on the server key without command
    User webmaster

This is what will occur:

local$ ssh websites-my-host
webmaster@realhostname:/var/www/websites$ _

or:

local$ ssh my-host
webmaster@realhostname:~$ _
  • 3
    I prefer this question above the accepted one. It allows for multiple users to use the same user on the server and still be able to customize what happens when you personally log in. Much more flexible and as correct as the other one. – testuser Nov 14 '17 at 14:56
  • You have to be really careful here since adding commands to ~/.ssh/authorized_keys affects the other tools based on ssh like rsync, scp. These commands will simply hang – warunapww Jun 26 '18 at 15:30
6

Openssh sshd by default accepts these environment variables from the client:

AcceptEnv LANG LC_*

You can use that to send a value from the local environment of the client to the server like this:

LC_CDPATH=/var/www/websites ssh -o SendEnv=LC_CDPATH user@server

You can place the SendEnv directive in ~/.ssh/config so you don't have to include it on the command line.

If you place the following in your ~/.profile (to only affect interactive logins use .profile, to affect all logins use .bashrc):

if [ "$LC_CDPATH" -a -d "$LC_CDPATH" ]; then
  cd "$LC_CDPATH";
fi

Then it will automatically change directory to the one specified in the environment variable when you login, if it is specified and if it is a directory.

  • 1
    +1 for LC_* trick – Orient Dec 6 '17 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.