66

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?

82

There are three 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 .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)
4
  • Thanks. I edited the bash.bashrc file which was located in the /etc directory. Worked a treat :) Apr 16 '13 at 8:56
  • 8
    Ooh, I would not do that, as it affects all users. Better to edit /home/yourlogin/.bashrc Apr 16 '13 at 20:16
  • 10
    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.
    – Timo
    Jan 20 '19 at 19:02
23

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:~$ _
5
  • 4
    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
  • 1
    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
  • 3
    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 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 '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 at 8:44
11

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
5
  • This is better. It is the documented - and therefore the correct - way of cd'ing into a default directory. Dec 31 '20 at 11:33
  • bash -i (interactive) works better for me (Remote is CentOS 8).
    – Tvartom
    Apr 2 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 at 20:53
  • It's important to include RequestTTY force or the path won't show up in my case. May 25 at 16:20
  • for z shell use zsh -l for fish use fish -l May 28 at 16:41
9

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
0

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

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

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.