Is it possible to redefine the tilde ('~', home directory)?

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.


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

  • Why was '~' chosen to represent the home directory?

Tags:

Csh

Solaris

Home