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:


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

5 Answers 5


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)
  • Thanks. I edited the bash.bashrc file which was located in the /etc directory. Worked a treat :) Apr 16, 2013 at 8:56
  • 8
    Ooh, I would not do that, as it affects all users. Better to edit /home/yourlogin/.bashrc Apr 16, 2013 at 20:16
  • 12
    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, 2017 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, 2019 at 19:02

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
  • 1
    This is better. It is the documented - and therefore the correct - way of cd'ing into a default directory. Dec 31, 2020 at 11:33
  • bash -i (interactive) works better for me (Remote is CentOS 8).
    – Tvartom
    Apr 2, 2021 at 15:25
  • I get stty: 'standard input': Inappropriate ioctl for device and can't see my path either. Won't connect at all if I do the requestTTY
    – Ben Creasy
    Apr 3, 2021 at 20:53
  • It's important to include RequestTTY force or the path won't show up in my case. May 25, 2021 at 16:20
  • for z shell use zsh -l for fish use fish -l May 28, 2021 at 16:41

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$ _


local$ ssh my-host
webmaster@realhostname:~$ _
  • 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, 2017 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, 2018 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, 2019 at 3:31
  • 2
    Just a word of caution: If you have disabled PasswordAuthentication (see askubuntu.com/questions/346857/…) and accidentally mistype your command string, you may lock yourself out of the system. Oct 17, 2019 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, 2021 at 8:44

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";

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.


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

  • This is already suggested as a (not so good) option in the voted answer
    – ndemou
    Jun 15, 2021 at 19:53

You must log in to answer this question.

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