Change default path for when I SSH in to CentOS server?

38,924

Solution 1

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!

Solution 2

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.

Share:
38,924

Related videos on Youtube

Ben
Author by

Ben

Updated on September 18, 2022

Comments

  • Ben
    Ben over 1 year

    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.

  • laebshade
    laebshade over 11 years
    It's better to use usermod when changing anything concerning a user that writes to /etc/passwd, to prevent mistakes: usermod -d /home user.
  • cwallenpoole
    cwallenpoole about 7 years
    That side comment about .bashrc made me facepalm. It's so simple and obvious, wish I had thought of it! Good job. Kudos!
  • SpinUp
    SpinUp almost 7 years
    double +1, I was looking everywhere for this information