Bash – changing user home directory has no effect

bashhomesudousers

When I switch from root user to a user (automatically created when installing git-auto-deploy) with

sudo -u git-auto-deploy /bin/bash I always get this error:

bash: /root/.bashrc: Permission denied

I created a user home directory, and put a .profile in it with the following (ran source on it too):

export HOME=/srv/users/git-auto-deploy

The command finger git-auto-deploy gives the following output:

Login: git-auto-deploy                  Name:
Directory: /srv/users/git-auto-deploy   Shell: /bin/bash
Never logged in.

When logged in as this user, the env command still lists incorrect directory:

HOME=/root

What am I doing wrong?

Best Answer

Use sudo -s -H -u git-auto-deploy.

sudo by itself does not change the value of HOME, but does so when -H is used. This is why you're getting that "Permission denied" error. The user can't access root's home directory (and shouldn't be able to either).

The -s option makes sudo start whatever shell is configured as the login shell for the specified user (i.e. no need to explicitly run /bin/bash).

Related Question