I am trying to lock down regular users on a Raspbian installation. I'll be using rbash for that.

I want to edit the PATH file so that only ~/bin files can be executed.

I've stripped $PATH from /etc/profile/:

if [ "`id -u`" -eq 0 ]; then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
  PATH=""
fi
export PATH

I've stripped $PATH from /etc/login.defs:

# cat /etc/login.defs | grep PATH=
ENV_SUPATH      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV_PATH        PATH=""

The /etc/environment file is empty:

~ # cat /etc/environment
~ #

The local user's .profile file contains only the following:

$ cat .profile
# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin"
fi

That works fine when running ssh user@host:

$ free
-rbash: free: command not found
$ uname
-rbash: uname: command not found
$ echo $PATH
/home/user/bin

However, when running ssh -t user@host bash --noprofile .profile is not executed, and I still get access to a fully working $PATH:

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/games
$ uname
Linux

What am I missing here? Where else is the $PATH file defined?


Solution 1:

It's set by sshd itself:

$ strings /usr/sbin/sshd | grep games
/usr/local/bin:/usr/bin:/bin:/usr/games

In man ssh under ENVIRONMENT it says:

 ssh will normally set the following environment variables:
 (...)
 PATH                  Set to the default PATH, as specified when compiling ssh.

To prevent sshd from setting PATH to the default value set

PermitUserEnvironment yes

in /etc/ssh/sshd_config and set a new value of PATH in ~/.ssh/environment, for example

PATH=~/bin

And restart SSH service:

sudo service ssh restart

BTW, you can get all of this information from man sshd_config.