79

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?

Teun Zengerink
  • 199
  • 5
  • 13
Bob Flemming
  • 1,245
  • 3
  • 14
  • 17

5 Answers5

99

There are four ways to achieve this:

  • add cd /var/www/websites to the end of your .bash_profile. This is executed only for interactive logins (e.g. ssh).
  • add cd /var/www/websites to the end of your .profile. This is more likely to be called by shells which are not bash (e.g. zsh). (Added from @Phil Hord's comment)
  • 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 :-)
  • Change your homedirectory on the server to /var/www/websites (this is not really a good idea)
MrValdez
  • 483
  • 2
  • 8
  • 21
Dennis Kaarsemaker
  • 19,277
  • 2
  • 44
  • 70
  • Thanks. I edited the bash.bashrc file which was located in the /etc directory. Worked a treat :) – Bob Flemming Apr 16 '13 at 08:56
  • 8
    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
  • 11
    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). – Phil Hord 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. – Timo Jan 20 '19 at 19:02
28

Since OpenSSH 7.6, you can use the new RemoteCommand option to achieve that.

In your ~/.ssh/config:

Host websites-my-host
    HostName <realhostname>
    IdentityFile ~/.ssh/keyfile
    User webmaster
    RequestTTY force # check if this is necessary
    RemoteCommand cd /var/www/websites && bash -l
igorsantos07
  • 620
  • 7
  • 13
26

Warning! Try this on non-essential account first because if you make a mistake in the command you may lose an access to the remote system.


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:~$ _
gertas
  • 1,077
  • 11
  • 12
  • 6
    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
  • 2
    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
  • 4
    You can use `$SSH_ORIGINAL_COMMAND` instead of `/bin/bash -l` to get the tools to work (in my case, hg). Or `$SHELL` if you don't want to hardcode to bash. Not sure how to get tools and human logins to both work. – mpen Sep 11 '19 at 03:31
  • 2
    Just a word of caution: If you have disabled PasswordAuthentication (see https://askubuntu.com/questions/346857/how-do-i-force-ssh-to-only-allow-users-with-a-key-to-log-in) and accidentally mistype your command string, you may lock yourself out of the system. – karan.dodia Oct 17 '19 at 18:18
  • I have tried this and now I just get message `Connection to hostname closed.` so I have basically locked myself out of the server - so use with caution. – kovinet Jan 26 '21 at 08:44
10

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.

Victor Jerlin
  • 518
  • 4
  • 6
1

Change entire home dir is another option: usermod -d /var/www/websites yourusername

pdropi
  • 51
  • 2