Windows – Changing the HOME for Cygwin

bashcygwin;windows

I'm trying to change the home directory for Cygwin on a computer at work. Our home directories mapped to a network share by default. This seems to cause some confusion for Cygwin, as it created a directory on my desktop that follows the same path as the network share would, some other prefix based on where I open cygwin from.

If I open Cygwin from the default shortcut created on installation:

$ echo $HOME
/cygdrive/c/Users/shaun/Desktop/SERVER/USERS:USERS/SHAUN

If I open Cygwin from PowerShell:

$ echo $HOME
/usr/bin/SERVER/USERS:USERS/SHAUN

I would rather have my home directory in a local programs directory I set up for managing programs myself. Like this:

/cygdrive/c/users/shaun/apps/cygwin/home/shaun

The preferred way to do this, as recommended by cygwin, is to make an entry in /etc/nsswitch.conf. This looks like it should do it:

db_home: unix /%H/Apps/cygwin/home/shaun

Any changes I have made to this file seemed to be ignored. I restart the terminal, and even when I intentionally mess up the syntax I get no errors. Some of the options I tried in and have failed:

db_home: /cygdrive/c/users/shaun/apps/cygwin/home/shaun
db_home: /%H/Apps/cygwin/home/shaun
db_home: unix /%H/Apps/cygwin/home/shaun
db_home: cygwin desc
db_home: cygwin /path /%H/Apps/cygwin/home/shaun

I restarted the Cygwin process after each attempt. I also tried changing the db_shell option to /bin/sh just as an experiment, but the shell did not change.

Past questions in StackExchange have suggested changing the /etc/passwd file, I was able to get this to work only after commenting out a block of code in /etc/profile that was automatically creating the HOME directory. And then, after some time it went back to the old HOME.

Is there something I'm missing about the /etc/nsswitch.conf file? It seems like it would be a simple and convenient way to set up custom configurations. The alternatives seem like they're outdated hacks that are not recommended by Cygwin, but if I'm wrong please let me know which method is preferred.

My current /etc/nsswitch.conf

    # /etc/nsswitch.conf
    #
    #    This file is read once by the first process in a Cygwin process tree.
    #    To pick up changes, restart all Cygwin processes.  For a description
    #    see https://cygwin.com/cygwin-ug-net/ntsec.html#ntsec-mapping-nsswitch
    #
    # Defaults:
    # passwd:   files db
    # group:    files db
    # db_enum:  cache builtin
    # db_home:  /home/%U
    db_home: /cygdrive/c/users/shaun/apps/cygwin/home/shaun
    # db_shell: /bin/bash
    # db_gecos: <empty>

Best Answer

db_home: unix /%H/Apps/cygwin/home/shaun

The manual page you reference tells you to use unix or a /path, not both together.

Unless your AD administrator has set the unixHomeDirectory value for each Cygwin-using user in your organization, you do not want to use unix at all, but the path instead.

I would suggest a modification to what you've given:

db_home: /%H/Apps/cygwin/home/%u

That will allow the setting to work for multiple users on the same machine.

Any changes I have made to this file seemed to be ignored.

There are two ways to override the nsswitch.conf setting:

  1. Run Cygwin's mkpasswd utility, which generates /etc/passwd and /etc/group from either the SAM or AD user info databases. This mechanism is obsolete, and should only be used on legacy Cygwin installations or for special purposes.

  2. Set the %HOME% environment variable at the Windows system level.

    This not only overrides Cygwin, it does so only under certain circumstances, which is why it is not recommended.

    Check both the user and system-wide environment variable sets to make sure it isn't happening to you.

Past questions in StackExchange have suggested changing the /etc/passwd file

You should remove that and use your site's Active Directory system instead, with /etc/nsswitch.conf controlling how Cygwin interacts with it.

Related Question