16

Is it possible to redefine the home directory? e.g. to /ext1/username instead of /home/username, i.e. expanding the ~ to another directory (as opposed to changing the actual home directory where users' home files are located).

(This question is mostly academic, as it seems like bad practice to do so. I also have no choice in the matter of using csh, despite having read the Top 10.)

3
  • 11
    Do you mean change the actual home directory, or do you mean make the shell expand ~ to something other than the actual home directory?
    – derobert
    Dec 4, 2013 at 13:47
  • I meant expanding the ~ to another directory (not, if I understand your alternative correctly, changing where all users' home directories are to be located). I didn't realize that ~ was interpreted as a short form of $HOME, as @michas described in his answer. Dec 4, 2013 at 14:42
  • 2
    Not all users' home directories, you can change just one, as slm shows in his answer.
    – derobert
    Dec 4, 2013 at 14:44

3 Answers 3

34

The tilde ~ is interpreted by your shell. Your shell will interpret ~ as a short form of $HOME.

Try (echo ~; HOME=foo; echo ~). This should first print your real home directory and afterwards "foo", as you set $HOME to that.

The default value of $HOME comes from you system configuration. Use getent passwd to list all known users and their home directories. Depending on your system configuration those entries might come from /etc/passwd or any remote directory service.

If you only want to temporarily redefine your home directory, just set another $HOME.

If you permanently want to change it you have to change the passwd entry, e.g. by manually editing /etc/passwd.

3
  • Thanks, @michas. Can't believe I missed that variable. (BTW, turns out in csh the relevant variable is $home (lowercase), and you can't setenv HOME /ext1/acheong—you can only set home=/ext1/acheong, and that worked for me.) Dec 4, 2013 at 14:50
  • 3
    To change it permanently, you can also use usermod -d
    – user606723
    Dec 4, 2013 at 15:02
  • 1
    Use vipw (or whatever GUI your OS provides) rather than editing /etc/passwd directly. Direct editing risks corrupting the file, which can be difficult to recover from. Dec 4, 2013 at 20:45
11

The value that is used for ~ is determined from the value you get from the administrative database (getent passwd), typically in the /etc/passwd file, for each user's home directory that's defined there.

$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
sam:x:500:500:Sam Mingolelli:/home/sam:/bin/bash
tracy:x:501:501::/home/tracy:/bin/bash

The 6th column in this file is where the value used when someone types cd ~ comes from.

nsswitch

You can see what a system would use for the user's home directory using the command getent passwd:

$ getent passwd
root:x:0:0:root:/root:/bin/bash
sam:x:500:500:Sam Mingolelli:/home/sam:/bin/bash
tracy:x:501:501::/home/tracy:/bin/bash

The "database" that provides these is controlled through your systems resolver, defined in /etc/nsswitch.conf.

$ grep passwd /etc/nsswitch.conf 
#passwd:    db files nisplus nis
passwd:     files

Files above means /etc/passwd, but the "database" could come from LDAP, NIS, or other locations over the network, for example.

Moving/Redefining?

To perform this operation is a little tricky after the accounts have been created. If you're creating accounts from scratch then it's trivial to redefine a user's location of their home directory. When running the useradd command you can specify the location to be used for a user's home directory.

Example

$ useradd -d /ext1/acheong ...

excerpt from man page

-d, --home HOME_DIR
   The new user will be created using HOME_DIR as the value for the user’s 
   login directory. The default is to append the LOGIN name to BASE_DIR and 
   use that as the login directory name. The directory HOME_DIR does not 
   have to exist but will not be created if it is missing.

For existing accounts?

This becomes more of a surgical operation since often times the path of a user's home directory gets included statically in configuration files, making it trickier.

Example

$ grep home /home/sam/.*
/home/sam/.gtkrc-1.2-gnome2:include "/home/sam/.gtkrc.mine"

These will either need to be fixed, or you'll have to provide a link from /home/sam to the new location, /ext1/sam.

Moving when "database" isn't /etc/passwd

If the system is getting the home directories from LDAP, NIS, etc. then you'll need to peform the relocation in those systems, and coordinate with moving the files from /home/sam to /ext1/sam.

References

3
  • Of course, changing that will require moving the user's files. And probably leaving a symlink in the old place (as otherwise a lot of things will break...)
    – derobert
    Dec 4, 2013 at 13:54
  • @derobert - yeah having done this a few times I try to avoid it like the plague.
    – slm
    Dec 4, 2013 at 14:01
  • @slm - Thanks for such a detailed answer (and the bit of history). I'm afraid I was looking to do something much simpler—and I was ignorant to the existence of an environment variable representing the session's home directory. I'm sure your answer will help another reader in the future, however. Thanks again. Dec 4, 2013 at 14:48
1

If the reason is to spread out users across multiple file systems, you could also use the automounter. And even allow you to have a different "base" directory for your users.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .