126

I just installed Cygwin, and it looks like the home directory in the bash prompt is on my Z: drive. That's not where I want it.

How can I change this?

Nathan Long
  • 113,812
  • 91
  • 316
  • 418
  • 1
    For those using Cygwin 1.7.34 or above, use the methods explained in the other posts, but don't forget to remove the # before `db_home:` to unquote it and make it work. – Maieul Feb 07 '19 at 18:01

7 Answers7

193

Starting with Cygwin 1.7.34, the recommended way to do this is to add a custom db_home setting to /etc/nsswitch.conf. A common wish when doing this is to make your Cygwin home directory equal to your Windows user profile directory. This setting will do that:

db_home: windows

Or, equivalently:

db_home: /%H

You need to use the latter form if you want some variation on this scheme, such as to segregate your Cygwin home files into a subdirectory of your Windows user profile directory:

db_home: /%H/cygwin

There are several other alternative schemes for the windows option plus several other % tokens you can use instead of %H or in addition to it. See the nsswitch.conf syntax description in the Cygwin User Guide for details.

If you installed Cygwin prior to 1.7.34 or have run its mkpasswd utility so that you have an /etc/passwd file, you can change your Cygwin home directory by editing your user's entry in that file. Your home directory is the second-to-last element on your user's line in /etc/passwd

Whichever way you do it, this causes the HOME environment variable to be set during shell startup.²

See this FAQ item for more on the topic.


Footnotes:

  1. Consider moving /etc/passwd and /etc/group out of the way in order to use the new SAM/AD-based mechanism instead.

  2. While it is possible to simply set %HOME% via the Control Panel, it is officially discouraged. Not only does it unceremoniously override the above mechanisms, it doesn't always work, such as when running shell scripts via cron.

Warren Young
  • 36,691
  • 8
  • 76
  • 94
  • 3
    Note that if you installed the Git bash shell from http://git-scm.com/download/win, you _might_ have issues with the cygwin install (depending on what options you chose). My cygwin ~ ended up at my Win7 user directory and `ssh` wouldn't work correctly until editing `/etc/passwd` as described. – Allan May 22 '13 at 21:40
  • The issues @Allan mentions here with git installations are addressed by setting the Windows environment variable for HOME and then recreating the `/etc/passwd` file. See [my answer here](http://stackoverflow.com/questions/1494658/how-can-i-change-my-cygwin-home-folder-after-installation/11182877#11182877) to avoid such issues. – Christopher Jul 05 '13 at 00:49
  • 11
    `db_home: /%H` may resolve for a domain user name (instead of local user name), which may not be what you would like to have. `db_home: /cygdrive/c/Users/%U` did what I wanted to have. – Abdull Sep 02 '15 at 07:42
  • 2
    I see a performance difference between the various methods. When I use `db_home: windows`, it takes `sh` about 2.5 to 3.5 seconds to start. When I use `db_home: /cygdrive/c/Users/%U`, it takes `sh` about 0.9 to 1.0 seconds to start. When I delete the `db_home` line or hard-code it to the Windows home directory, `sh` takes about 0.2 to 0.3 seconds to start. I am going with the hard-coded solution, e.g. `db_home: /cygdrive/c/Users/susam`, because I often run shell scripts from Command Prompt using the `sh` command, e.g. `sh foo.sh` and it becomes inconvenient to wait for more than 2 seconds. – Susam Pal Mar 01 '16 at 14:57
  • 1
    Thank you @abdull, db_home: /cygdrive/c/Users/%U is the answer I needed. db_home: windows uses "/home/%U", which does not exist on my config. – Jeff Jan 30 '17 at 21:22
  • Setting `db_home: windows` breaks `sshd` since the permissions on a user's home aren't strict enough. Key auth will stop working with "*Authentication refused: bad ownership or modes for directory*". And setting Windows user home permissions stricter is not recommended since SYSTEM must also have access to the user's home directory. – rustyx Feb 05 '17 at 08:42
75

Cygwin 1.7.34+

For those using Cygwin 1.7.34 or higher Cygwin supports configuring how to fetch home directory, login shell, and gecos information in /etc/nsswitch.conf. This is detailed in the Cygwin User Guide section:

If you've previously created an /etc/passwd or /etc/group file you'll want to remove those and configure Cygwin using the new Windows Security model to POSIX mappings.

[[ -f /etc/passwd ]] && mv /etc/passwd /etc/passwd.bak
[[ -f /etc/group ]] && mv /etc/group /etc/group.bak

The /etc/nsswitch.conf file's db_home: setting defines how Cygwin fetches the user's home directory. The default setting for db_home: is

db_home: /home/%U

So by default, Cygwin just sets the home dir to /home/$USERNAME. You can change that though to point at any other custom path you want. The supported wildcard characters are:

  • %u The Cygwin username (that's lowercase u).
  • %U The Windows username (that's uppercase U).
  • %D Windows domain in NetBIOS style.
  • %H Windows home directory in POSIX style. Note that, for the db_home: setting, this only makes sense right after the preceeding slash, as in db_home: /%H/cygwin
  • %_ Since space and TAB characters are used to separate the schemata, a space in the filename has to be given as %_ (that's an underscore).
  • %% A per-cent character.

In place of a path, you can specify one of four named path schemata that are predefined.

  1. windows The user's home directory is set to the same directory which is used as Windows home directory, typically something along the lines of %USERPROFILE% or C:\Users\$USERNAME. Of course, the Windows directory is converted to POSIX-style by Cygwin.

  2. cygwin AD only: The user's home directory is set to the POSIX path given in the cygwinHome attribute from the cygwinUser auxiliary class. See also the section called “The cygwin schema”.

  3. unix AD only: The user's home directory is set to the POSIX path given in the unixHomeDirectory attribute from the posixAccount auxiliary class. See also the section called “The unix schema”.

  4. desc The user's home directory is set to the POSIX path given in the home="..." XML-alike setting in the user's description attribute in SAM or AD. See the section called “The desc schema” for a detailed description.

The following will make the user's home directory in Cygwin the same as is used for the Windows home directory.

db_home: windows

Cygwin 1.7.33 or earlier

For those using Cygwin 1.7.33 or earlier, update to the latest version Cygwin and remove previously used /etc/passwd and /etc/group files, then see the steps above.

Else, follow these older steps below.

Firstly, set a Windows environment variable for HOME that points to your user profile:

  1. Open System on the Control Panel
  2. On the Advanced tab click Environment Variables (toward the bottom)
  3. In the User Variables area click "New…"
  4. For Variable name enter HOME
  5. For Variable value enter %USERPROFILE%
  6. Click OK in all the open dialog boxes to apply this new setting

Now we are going to update the Cygwin /etc/passwd file with the Windows %HOME% variable we just created. Shell logins and remote logins via ssh will rely on /etc/passwd to tell them the location of the user's $HOME path.

At the Cygwin bash command prompt type the following:

cp /etc/passwd /etc/passwd.bak
mkpasswd -l -p $(cygpath -H)  > /etc/passwd 
mkpasswd -d -p $(cygpath -H)  >> /etc/passwd 

The -d switch tells mkpasswd to include DOMAIN users, while -l is to only output LOCAL machine users. This is important if you're using a PC at work where the user information is obtained from a Windows Domain Controller.

Now, you can also do the same for groups, though this is not necessary unless you will be using a computer that is part of a Windows Domain. Cygwin reads group information from the Windows account databases, but you can add an /etc/group file if your machine is often disconnected from its Domain Controller.

At the Cygwin bash prompt type the following:

cp /etc/group /etc/group.bak
mkgroup -l > /etc/group 
mkgroup -d >> /etc/group 

Now, exit Cygwin and start it up again. You should find that your HOME path points to the same location as your Windows User Profile -- i.e. /cygdrive/c/Users/username

Christopher
  • 1,776
  • 15
  • 15
  • 7
    -d is really slow. If you only need the current user the you can use -c instead: http://sinewalker.wordpress.com/2006/10/27/cygwin-users-and-groups – Ben Challenor Oct 17 '12 at 12:21
  • Ben is correct, that you can just add an entry for the current user by specifying -c instead. If you do however, Cygwin will display ????? for the User and Group when listing files on shared file-systems, as it will not have records for other users and groups on your Domain or local computer. – Christopher Mar 09 '13 at 09:43
  • 1
    Can I suggest a small change in the wording: rather than 'Now, update your Cygwin /etc/passwd file with a new $HOME path', I would suggest 'Now we are going to update the Cygwin /etc/passwd file with the HOME variable we just created.' Many thanks – Robert Mar 18 '13 at 11:32
  • With -d It takes minutes (I have killed after 5) minutes to complete. So I have rerun with -c. All good for my needs. – bartosz.r Oct 17 '13 at 08:34
  • What's the purpose of mkgroup? – Samuel Apr 22 '15 at 14:46
  • 1
    @Samuel I've added an explanation to clarify the purpose of `mkgroup` above. – Christopher Apr 23 '15 at 06:53
  • Why is nothing easy – MegaMatt Feb 22 '21 at 18:15
31

I did something quite simple. I did not want to change the windows 7 environment variable. So I directly edited the Cygwin.bat file.

@echo off
SETLOCAL
set HOME=C:\path\to\home
C:
chdir C:\apps\cygwin\bin
bash --login -i
ENDLOCAL

This just starts the local shell with this home directory; that is what I wanted. I am not going to remotely access this, so this worked for me.

Ajay
  • 391
  • 3
  • 5
  • Second this as I can't change my home environment variable in my dev machine. – Deqing Dec 11 '14 at 06:31
  • @WernerCD I would just caution that this can have unintended consequences when interacting with native Windows applications. Your Windows applications will continue to look for HOME at the old location. An example would be Gvim, which will maintain a separate vim configuration at the old Windows `%HOME%` location, while Cygwin vim uses the new `$HOME` path you've declared. – Christopher Apr 23 '15 at 07:00
  • This is great... 5 years later and I like it because the "engineering" is simple enough for a Cygwin relative newb (like me) to use and understand. Incidentally, putting a shortcut to your .bat file in C:\Users\\AppData\Roaming\Microsoft\Windows\SendTo means you can go Send-->Cygwin on your context-sensitive (right-click) menu in Explorer (and open a terminal anywhere, with ~/ set wherever you want). – mike rodent Dec 31 '17 at 17:21
19

Cygwin mount now support bind method which lets you mount a directory. Hence you can simply add the following line to /etc/fstab, then restart your shell:

c:/Users /home none bind 0 0
Jihem
  • 349
  • 2
  • 5
11

Change your HOME environment variable.

on XP, its right-click My Computer >> Properties >> Advanced >> Environment Variables >> User Variables for >> [select variable HOME] >> edit

Michael Paulukonis
  • 8,540
  • 5
  • 46
  • 66
  • 1
    This will change your HOME environment variable for all your Windows applications, too. – mob Sep 29 '09 at 20:31
  • Very true. But what Windows applications do you have that use the HOME variable? I have EmacsW32 and.... Cygwin. Since I installed Emacs first, that's where my cygwin home lies, as well. – Michael Paulukonis Sep 29 '09 at 20:39
  • 1
    Won't this conflict with /etc/passwd somehow? Will it default to /etc/passwd's HOME if the environment variable is not set? If there's no conflict and it will default to /etc/passwd, it seems like the perfect set up: I'm running Cygwin off a thumbdrive, both to avoid having to ask permission to install at work, and so I have a Unix-like environment in my pocket that I can use when only a Windows box is available. At work I'll have my work computer's home directory, and elsewhere I can have a home directory on the thumbdrive. – iconoclast Aug 02 '12 at 16:27
6

I'd like to add a correction/update to the bit about $HOME taking precedence. The home directory in /etc/passwd takes precedence over everything.

I'm a long time Cygwin user and I just did a clean install of Windows 7 x64 and Cygwin V1.126. I was going nuts trying to figure out why every time I ran ssh I kept getting:

e:\>ssh foo.bar.com
Could not create directory '/home/dhaynes/.ssh'.
The authenticity of host 'foo.bar.com (10.66.19.19)' can't be established.
...

I add the HOME=c:\users\dhaynes definition in the Windows environment but still it kept trying to create '/home/dhaynes'. I tried every combo I could including setting HOME to /cygdrive/c/users/dhaynes. Googled for the error message, could not find anything, couldn't find anything on the cygwin site. I use cygwin from cmd.exe, not bash.exe but the problem was present in both.

I finally realized that the home directory in /etc/passwd was taking precedence over the $HOME environment variable. I simple re-ran 'mkpasswd -l >/etc/passwd' and that updated the home directory, now all is well with ssh.

That may be obvious to linux types with sysadmin experience but for those of us who primarily use Windows it's a bit obscure.

Dan Haynes
  • 388
  • 3
  • 6
  • 1
    The home directory in /etc/passwd DOES NOT take precedence over everything. It is however used as the primary source for your HOME directory path when logging in remotely, or using ssh. See this from the Cygwin FAQ http://cygwin.com/faq/faq.setup.html#faq.setup.home – Christopher Jun 25 '12 at 02:42
2

I happen to use cwRsync (Cygwin + Rsync for Windows) where cygwin comes bundled, and I couldn't find /etc/passwd.

And it kept saying

Could not create directory '/home/username/.ssh'.
...
Failed to add the host to the list of known hosts (/home/username/.ssh/known_hosts).

So I wrote a batch file which changed the HOME variable before running rsync. Something like:

set HOME=.
rsync /path1 user@host:/path2

And voila! The .ssh folder appeared in the current working dir, and rsync stopped annoying with rsa fingerprints.

It's a quick hotfix, but later you should change HOME to a more secure location.

sifferman
  • 2,615
  • 1
  • 21
  • 33
TEHEK
  • 972
  • 8
  • 8