82

I'm trying to safely update the home directory as specified in /etc/passwd, but the standard Linux utils - usermod and vipw - for doing so aren't provided by Cygwin.

Could anyone tell me how they changed this in Cygwin?

2

12 Answers 12

95

EDIT: For recent versions of Cygwin (1.7.34 and beyond), see this newer question.

Like sblundy's answer, you can always edit by-hand.

But if you want to do it the "official" way, use the cygwin-specific mkpasswd command. Below is a snippet from the official docs on mkpasswd :

For example, this command:

Example 3.11. Using an alternate home root

$ mkpasswd -l -p "$(cygpath -H)" > /etc/passwd

would put local users' home directories in the Windows 'Profiles' directory.

There's a bunch of other really useful commands described on the Cygwin Utilities documentation page (which includes mkpasswd). The use of cygpath in the example above is another of these cygwin-specific tools.

While you're at it, you probably also want to read the Using Cygwin Effectively with Windows documentation. There's a bunch of really good advice.

4
  • 10
    If you're on a domain, you may consider this alternate command: (-d for domain) $ mkpasswd -l -d -p "$(cygpath -H)" > /etc/passwd This didn't work for me - it basically hung. Since I'm the only one using this machine, I could use this alternative, which should be safe whether or not you're on a domain (the -c is for "current user) $ mkpasswd -l -c -p "$(cygpath -H)" > /etc/passwd Apr 27, 2012 at 14:36
  • 1
    After doing this, the .bashrc doesn't seem to get read, even if I copy it to my new $HOME. Any idea how to fix this?
    – Jeff
    Oct 19, 2016 at 15:45
  • 1
    Output should be appended (>>) to /etc/passwd (for corporate scenarios with several users). This worked ! :)
    – Andre
    Nov 22, 2016 at 0:09
  • 1
    use uname -r to see which cygwin version you're on May 4, 2017 at 14:25
19

I ended up exiting all my cygwin shells and editing it by hand in a text editor. So far, so good.

Note: don't escape the spaces in the "Documents and Settings" directory. The entry will look like

user:...:/cygdrive/c/Documents and Settings/user:/bin/bash

The line is tokenized on the : character.

2
  • This works for UNC paths too, thanks was overriding with HOME variable before
    – HaveAGuess
    Jun 11, 2014 at 14:18
  • What do I have to edit by hand? I don't know why my cygwin home directory is /cygdrive/c/Users/JoseAngel/AppData/Roaming/SPB_16.6 I want it to be C:/Users/MyName/
    – user4328677
    Mar 14, 2015 at 16:05
16

The simplest answer I have found is to make /home to be a soft link to your Windows Home/UserProfile directory

cd /
mv home oldhome
ln -s "$(cygpath -H)" home

I used cygpath as it will get the proper location for the HOME directory on the current version of Windows. On my box cygpath -H returns /cygdrive/c/Users

5
  • After doing this for some time there is one drawback: I have to run it each time I update cygwin using their setup.exe. Not a big deal but something to remember.
    – M Smith
    Jan 21, 2014 at 5:20
  • This is the best answer. I wonder why cygwin does not default to cygwin -H by default. It is the most sensible location, and since Windows 7 there is no space in its path too.
    – dashesy
    Dec 17, 2015 at 17:49
  • on another note, this will not work. It suddenly decided to ignore the symbolic link!
    – dashesy
    Dec 17, 2015 at 21:14
  • You have to reapply it when you run setup again. That is the only time it has failed for me. I have not tried it on windows 10 yet though
    – M Smith
    Dec 18, 2015 at 13:50
  • Works as expected on Windows 10. I still have to re-apply each time I update cygwin.
    – M Smith
    Mar 8, 2016 at 0:58
8

For the current user the following worked for me:

  1. Close Cygwin.
  2. Set the HOME Windows user environment variable.
  3. Start Cygwin.
  4. run "mkpasswd -c -p "$(cygpath -H)" > /etc/passwd".
  5. Restart Cygwin.

I confirmed it worked by running ssh-keygen without any arguments. After making this change the app now defaults to saving the key to /cygdrive/c/Users/user instead of /home/user.

I don't know if setting HOME is required, but I did it anyway per instructions for setting up TortoiseGit with Cygwin using Tortoise's official documentation for unofficial Cygwin support here. Setting HOME alone though was not enough for ssh-keygen to recognize the home directory change.

Also, note that Cygwin's official documentation on this issue can be found here.

Confirmed in Windows 7 using 64-bit Cygwin v1.7.35.

1
  • For Cygwin 1.7.35+ use of mkpasswd is discouraged in favour of /etc/nsswitch.conf Nov 23, 2016 at 5:59
4

I always set HOME as a user-specific environment variable in Computer Properties.

2
  • I'm trying to avoid that as it is set to a corporate network drive. Looks like it would work though.
    – sblundy
    Oct 22, 2008 at 13:58
  • 7
    Note that this will not work for ssh, it explicitly ignores the HOME and always uses /etc/passwd. See last sentence of cygwin.com/faq/faq-nochunks.html#faq.setup.home.
    – studgeek
    Sep 6, 2011 at 23:35
3

To avoid problems caused by having spaces in the path to your home directory, use the short-form of the Windows 'Profiles' directory - i.e. /cygdrive/c/DOCUME~1/user.

You can do this by typing the command:

mkpasswd -l -p "$(cygpath $(cygpath -dH))" > /etc/passwd
0
3

Original answer by Christopher from elsewhere

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

1
  • As the paths follow predefined schema with latest Cygwin instead of "moving" home directory I'd recommend to use symlinks (including Windows native) to particular directories from the HOME directory.
    – gavenkoa
    Dec 28, 2020 at 19:30
2

I like to keep my cygwin installation sync'd to a pen drive and another computer, so I hate hard-coding the home directory. I use the following cygwin.bat:

echo off
SETLOCAL
set SHELL=\\bin\\bash
set HOME=%~dp0..\..\doc\unix
bin\bash --login -i
ENDLOCAL

SETLOCAL and ENDLOCAL make sure that SHELL and HOME don't clobber existing env variables for other programs. HOME=%~dp0..\..\doc\unix sets HOME to be two directories up, in the doc/unix subdirectory. Then in ....\doc\unix.bashrc, I include PATH="/bin:/usr/local/bin:/usr/X11R6/bin:/usr/bin". I did not use start /wait %CD%\bin\bash to start bash, because I am using Console2, so I don't need an additional cmd window.

2

Using Windows Environment Variable: HOME

This works for me for a permanent, non-portable, non-network solution; i.e. setting the HOME Environment variable permanently in Windows.

Note that this doesn't affect ssh or telnet sessions which always refer to /etc/passwd

ref: Setting up Cygwin- My HOME environment variable is not what I want.

CMD

For current user (needs to run once per user)::

reg add HKCU\Environment /v HOME /t REG_EXPAND_SZ /d ^%USERPROFILE^%

For new Users:

reg add HKU\.DEFAULT\Environment /v HOME /t REG_EXPAND_SZ /d ^%USERPROFILE^%

Note: Carets ^ before percent-signs %

IMPORT REG FILE

Import this reg file (current user):

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Environment]
"HOME"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,4c,\
  00,45,00,25,00,00,00

For new users:

Windows Registry Editor Version 5.00

[HKU\.DEFAULT\Environment]
"HOME"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,4c,\
  00,45,00,25,00,00,00

REGEDIT

In Regedit, under:

For current user:

HKEY_CURRENT_USER\Environment

For new Users:

HKU\.DEFAULT\Environment

Create HOME as a new Expandable String Value (*REG_EXPAND_SZ*) and put in %USERPROFILE%

1
  • Whatever information this answer was based on, it is no longer current. The ref: link is also broken. I would not try this at home. Nov 23, 2016 at 5:53
1
cd /home
rm -rf chris
ln -s /cygdrive/z chris

I am not really sure if it is the safest solution but it is a possible solution that works for me ;)

0

I edited my /etc/passwd file directly (making sure nothing else would be accessing it), and changed all references to /home to be /Users (on Windows 7). I found that, in order for everything to work correctly, I had to delete any directories in the /home directory (or move them to the appropriate other location). Otherwise, cygwin would develop a split personality where, for example, 'bash -l' would start in /home/Pablo but $HOME would be /Users/Pablo and emacs would appear to do the reverse. Once I deleted /home/Pablo, everything worked fine.

0

I only needed to be in C:\Users\username when I start cygwin. So, I just added to .bashrc and .profile

cd ${HOMEPATH}

If you prefer to use ~/. instead of $HOMEPATH, you can also add the following:

export HOME=${HOMEPATH}

This way I don't disturb the cygwin installation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.