~/.profile is not loaded when using SSH (Ubuntu)

Solution 1:

You may explicitly specify that you want to start an interactive login shell:

 ssh user@host bash --login -i 

The "role" of ~/.profile (or ~./bash_profile) and .bashrc for ssh have some other files, (see man ssh for details):

~/.ssh/environment

Contains additional definitions for environment variables; see ENVIRONMENT, above.

~/.ssh/rc

Commands in this file are executed by ssh when the user logs in, just before the user's shell (or command) is started. See the sshd(8) manual page for more information.

Solution 2:

.profile is only loaded for login shells, which an ssh session is not (by default). If you want something to run on startup for all interactive shells, put it in .bashrc instead (or .zshrc or whatever your shell uses).

Also, if you just want to log into another account on the local machine, ssh is probably overkill. You might want to use su or something instead.

Solution 3:

Using bash should result in reading ~/.bashrc. The following might help with ksh and sh (bash in sh mode), or when your ~/.bashrc is not executed during login.

The sshd consults ~/.ssh/environment (check sshd_config(5) for permissions) and ~/.ssh/sshrc or ~/.ssh/rc. This gives the possibility to setup ENV=~/.profile or BASH_ENV=~/.profile and SSH_LOGIN=Y

In ~/.profile I've the following layout (Replace ENV with BASH_ENV when using bash):


if [[ -n $SSH_LOGIN || -z $ENV ]]; then
     # Put here login initialization code
     unset SSH_LOGIN
     ENV=~/.profile
fi
 # Put here code thats get executed with each ksh/sh invocation

Solution 4:

You probably have a ~/.bash_profile, which overrides ~/.profile.