$HOME is not defined for root user on Ubuntu 14.04

2

On a fresh Ubuntu 14.04 install, the bash environment for the root user does not have $HOME defined. I need to run some apps from a root shell, for example:

gdb /usr/sbin/apache2

This is difficult without a working $HOME directory, because apps like vim and gdb require $HOME in the environment, and they are somewhat broken without it. Adding

export HOME=/root

to /root/.bashrc works (though I noticed the apache2 environment script unsets $HOME).

What is the proper way for HOME=/root to be defined in the root environment? Is there a reason it wasn't defined in the first place? Should $HOME not be defined for the root user?

Byron Hawkins

Posted 2015-01-30T08:13:40.967

Reputation: 280

Answers

2

The HOME variable is defined in /etc/passwd. The line of the root user should normally look like this:

root:x:0:0:root:/root:/bin/bash

The 6th field (separated by colon :) defines the home directory and is copied to the HOME environment variable by PAM; the mechanism of linux to authenticate users.

chaos

Posted 2015-01-30T08:13:40.967

Reputation: 3 704

Thanks--though I do have /root in the normal place in /etc/password. Does that mean something was issuing a unset HOME command? It seems like the variable must have been deliberately unset at some point. – Byron Hawkins – 2015-01-30T09:10:35.950

Seems so. You could search for HOME in config files and rc scripts: grep HOME /etc/* and grep HOME ~/.*. Maybe that will reveal some scripts that unset/resets the variable. – chaos – 2015-01-30T09:29:55.603

Looks like it was just the apache2 environment script, which I must have executed a couple times without realizing it. So nothing is wrong with my user config after all... – Byron Hawkins – 2015-01-30T09:32:00.390