11

I have a CentOS 5.7 web server, and I want to change the default place I land in when connecting using SSH.

Currently I land in /home/username, and I want to land in /home instead.

I've gone in as root and added PermitUserEnvironment yes to /etc/.ssh/sshd_config - and as I understand it this then sweeps the user's own ssh folder for an environment file. What I'm not sure about is exactly what I'm adding to this environment file, as export path=$PATH:$HOME doesn't seem to work, either here or in my .bashrc or .bash_profile files (which as I understand it wouldn't make a difference anyway as an SSH connection is a non-interactive shell?).

Thanks in advance.

2 Answers 2

14

If you are using PAM for authentication, which is probably the most likely. As root head into /etc/passwd.

There you should see your username and path! Change it there and you are home free!

EDIT - Sorry it just occurred to me that you maybe didn't want to change your home folder. In that case, simply add:

cd /home

To the bottom of your .bashrc file!

2
  • 4
    It's better to use usermod when changing anything concerning a user that writes to /etc/passwd, to prevent mistakes: usermod -d /home user.
    – laebshade
    Jan 9, 2013 at 2:19
  • 1
    That side comment about .bashrc made me facepalm. It's so simple and obvious, wish I had thought of it! Good job. Kudos! Feb 27, 2017 at 13:47
6

The thing to remember is that ~/.ssh/environment is read before a shell or ssh command is spawned, so (for example) neither export nor $PATH make sense. You can only set environment variables (not run general shell commands) here.

If you grab the environment for a non-interactive ssh shell, then modify that, you should get what you want for non-interactive commands. For example:

$ ssh mylogin@myserver env

will give you what the ssh starts with on your server. If you write your ~/.ssh/environment file as:

PATH=/usr/local/bin

and rerun the above, you should get "bash: env: command not found". Good!

Now, build up your path explicitly, based on what it was at base from your system's sshd (i.e. the first "ssh .... env" call), for example (adding /usr/local/bin at the head):

PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Note also that it is not useful to try to set the CWD in ~/.ssh/rc (which is run after ~/.ssh/environment is read, but before your ssh shell or command) as your shell will start (by default) in your home path.

1
  • double +1, I was looking everywhere for this information Jun 17, 2017 at 21:26

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.