Safely change home directory - cygwin

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?

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.

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.

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

For the current user the following worked for me:
Close Cygwin.
Set the HOME Windows user environment variable.
Start Cygwin.
run "mkpasswd -c -p "$(cygpath -H)" > /etc/passwd".
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.

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

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

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:
Cygwin user names, home dirs, login shells
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.
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.
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”.
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”.
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:
Open System on the Control Panel
On the Advanced tab click Environment Variables (toward the bottom)
In the User Variables area click "New…"
For Variable name enter HOME
For Variable value enter %USERPROFILE%
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

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.

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%

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 ;)

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.

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.

Related

How do I redirect from a built-in bin to execute an AppImage instead?

Scenario: the current version of Kate in Ubuntu 18LTS points at their customized version (which doesn't appear to support regex search capability). The bin is: /usr/bin/kate.
Desired solution: run the Kate AppImage (which has the regex search/replace functionality). The AppImage currently resides in ~/Downloads.
Question: how do I redirect the system to execute the AppImage version of Kate, instead of the built-in version?
Can I simply create a link to the AppImage in /usr/bin?
Yes, it appears you can... i.e. in my case I replaced the existing kate bin with a link that points to the appimage:
# 1st remove the existing kate binary
# (cp kate somewhere first if you want to keep a copy)
sudo rm /usr/bin/kate
# 2nd create a link in the system bin that points to the appimage
sudo link [directory where the appimage resides]/Kate.AppImage /usr/bin/kate
Done! The system will now execute the appimage when 'kate' is executed (e.g. via context menus).
=========================
UPDATE...
The above solution kinda works... it does run the appimage, however the parameters normally passed to kate (i.e. file to open) are lost in the hard link.
So... the better solution is to create a simple executable shell script (named 'kate' in the /usr/bin directory) to execute the appimage:
#!/bin/sh
exec [directory where the appimage resides]/Kate.AppImage "$#"
This passes any provided parms to the appimage.
You may want to keep (for whatever reasons) your system-installed Kate in /usr/bin/kate...
Then do not touch it. Instead create a directory in your $HOME named bin (it may already be present depending on the Linux distro you run).
Inside that directory, create a symlink:
ln -sf ~/Downloads/kate.AppImage ~/bin/kate
This may already work. If not, you have to move the ~/bin directory to the front of your path:
export PATH=${HOME}/bin:${PATH} # if you use Bash
To permanently modify this $PATH, add this same line into ${HOME}/.bashrc

Location of .bashrc for "Bash on Ubuntu on Windows" in Windows 10

Microsoft just introduced a Linux subsystem in its Windows 10 Anniversary Edition. The installation is pretty straight forward, but I could not locate bash files on Windows.
How does it work? What does ~ refer to in Windows? Where to find .bashrc?
Since the Windows 10 Fall Creators Update, the location changed to:
C:\Users\USERNAME\AppData\Local\Packages\{DIST}\LocalState\rootfs\home\{LINUXUSER}\
Where:
{DIST} is equal to CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc
{LINUXUSER} is the user for which you are looking for the .bashrc file
Just for anyone wondering that came here from Google.
Sorry for the misunderstanding, I check on google and it will be at C:\Users\USERNAME\AppData\Local\Lxss\home\USERNAME .
I tried and it works, in the cmd just type cd\ && dir *bashrc* /s it will locate the file, and in my case i see the line C:\Users\USERNAME\AppData\Local\Lxss\home\USERNAME but when I want to navigate it with the window browser it doesn't work, but if you copy paste it, it works :-)
I found it here.
Considering that you need to know where a file is located you can use the find command.
The syntax of the command is find {search-path} {file-names-to-search} {action-to-take}by default the action to take is printing the file name.
So if you are finding .bashrc file you can use find / -name .bashrc the bash will return you /home/yourusername/.bashrc
Also, if you want to access to your home directory you can use cd ~
Hope my answer will be helpful :-)
just type
vi ~/.bashrc
and that should put you into the file where ever it is.
You can navigate there simply by doing cd ~
List all files with ls -a and you should be able to see it.
~ means that is user home folder, way like /home/%username%/
you can list files like ls -al and see .bashrc file.
Right now on WSL 2 you can find it under /home/{user_name} and the file is hidden.
You can access it from Ubuntu console by {text_editor} .bashrc
If you want to edit that in Windows just type in ubuntu console explorer.exe . and it opens the current folder and shows all hidden files.
It's weird but works fine.
Other answers doesn't work for me using WSL 2.
The LocalState folder contains a virtual disk so rootfs does not exist,
and AppData\Local folder does not have the Lxss folder.
The solution for me is surprisingly simple:
wsl -u root
This will allow you to get into wsl as root.
From here, you have access to the whole linux.
Fix the .bashrc or anything you want.
Don't screw up the root user. :)
I find my .bashrc file in:
/home/your_user_name
you can run cd /home/your_user_name or cd ~ should work as well
If you previously installed git bash for window, you may also find .bashrc file in your window user profile folder. In Linux subsystem, you may local the file under /mnt/c/Users/your_window_user_name/.bashrc However, modifying that file only works for git bash in window but not for the shell terminal of the Linux subsystem.
Note: my installation of the Ubuntu is 20.04 LTS straight from window store.

Enable native NTFS symbolic links for Cygwin

Recent NTFS and Windows implement symlinks:
NTFS junction point can be used as directory symlink since NTFS 3.0 (Windows 2000) using linkd or junction tools.
NTFS symbolic link can also be used as symlink (for both file and directory) since Windows Vista using mklink tool.
But on Cygwin 1.7 (installed on Windows 7), ln -s creates a text file.
on Cygwin:
$ ln -s -v target mylink
`mylink' -> `target'
on MinGW (or your favorite editor):
$ cat mylink
!<symlink>ÿþt a r g e t
Is it possible to tell Cygwing to use NTFS junction point or NTFS symbolic link?
other question: Is this available on MinGW?
⸻⸻  Short answer  ⸻⸻
Define environment variable:
CYGWIN=winsymlinks:nativestrict
As pointed out by mwm you may also have to go to the settings or to run bash as Administrator. See the Notes section.
⸻⸻  Long answer  ⸻⸻
Default Cygwin symlinks are just regular files
By default Cygwin creates text files as workaround for Windows symlink flaw.
These files are not really symlinks.
Almost all Windows programs do not considers these files as symlinks.
Native symlinks are available on recent Windows versions
Recent NTFS and Windows implement symlinks:
NTFS junction point can be used as directory symlink
since NTFS 3.0 (Windows 2000) using linkd or junction tools.
NTFS symbolic link can also be used as symlink
(for both file and directory) since Windows Vista using mklink tool.
Cygwin can create native NTFS symlinks
Simplified extract of the Cygwin documentation:
Symbolic links
[...]
Cygwin creates symbolic links potentially in multiple different ways:
The default symlinks are plain files containing a magic cookie
followed by the path to which the link points. [...]
The shortcut style symlinks are Windows .lnk [...] created
if the environment variable CYGWIN [...] is set to contain
the string winsymlinks or winsymlinks:lnk. [...]
Native Windows symlinks are only created on Windows Vista/2008 and later,
and only on filesystems supporting reparse points.
Due to to their weird restrictions and behaviour, they are only created
if the user explicitely requests creating them.
This is done by setting the environment variable CYGWIN
to contain the string winsymlinks:native or winsymlinks:nativestrict.
[...]
On the NFS filesystem, Cygwin always creates real NFS symlinks.
Configuring Cygwin
Cygwin User's Guide presents variable CYGWIN and option winsymlinks:
The CYGWIN environment variable is used to configure many global settings [...].
It contains the options listed below, separated by blank characters. [...]
[...]
[...]
[...]
[...]
winsymlinks:{lnk,native,nativestrict} -
if set to just winsymlinks or winsymlinks:lnk, Cygwin creates symlinks
as Windows shortcuts with a special headerand the R/O attribute set.
If set to winsymlinks:native or winsymlinks:nativestrict,
Cygwin creates symlinks as native Windows symlinks on filesystems
and OS versions supporting them. If the OS is known not to support
native symlinks (Windows XP, Windows Server 2003), a warning message
is produced once per session.
The difference between winsymlinks:native and
winsymlinks:nativestrict is this: If the filesystem supports native
symlinks and Cygwin fails to create a native symlink for some reason,
it will fall back to creating Cygwin default symlinks with
winsymlinks:native, while with winsymlinks:nativestrict
the symlink(2) system call will immediately fail.
CYGWIN=winsymlinks:native always creates a link but uses a Cygwin fall-back when target does not exists
on Cygwin:
$ export CYGWIN="winsymlinks:native"
$ ln -s -v target mylink
`mylink' -> `target'
$ echo content > target
on MinGW:
$ cat mylink
content
People using both Windows and Cygwin programs may have issues when a symlink is created as a dummy file (Cygwin fallback when target is missing)...
CYGWIN=winsymlinks:nativestrict always uses native-Windows symlink but fails when target does not exist
on Cygwin:
$ export CYGWIN="winsymlinks:nativestrict"
$ rm -f a b
$ ln -sv a b
ln: failed to create symbolic link `b': No such file or directory
$ touch b
$ ln -sv a b
ln: failed to create symbolic link `b': File exists
$ rm b
$ touch a
$ ln -sv a b
`b' -> `a'
Because nativestrict requires the target exists before the symlink creation, some commands/scripts may fail when creating a link.
Notes
Since Windows 10 build 14972, native NTFS symlinks are available in a non-elevated shell by enabling the Developer Mode in the Developer Settings.
Reference: https://blogs.windows.com/windowsdeveloper/2016/12/02/symlinks-windows-10/
In older versions, only administrators have the ability to create native NT symlinks
so under Windows UAC, the Cygwin terminal emulator (mintty)
should be run with elevated privileges
(right-click the shortcut and choose Run as Administrator
or set the mintty shortcut property, Advanced → Run as Administrator).
Special thanks to Guria, Spooky and Gene Pavlovsky for their contributions.
The accepted answer is right, two little side notes.
If you only care about the symlinks you create yourself on the command line, install cygutils-extra package, it includes a winln command, which has the same syntax as ln, but creates native Windows links. Create an alias: alias ln=winln (only works in interactive shell), or even replace the ln file with winln (works in shell scripts as well) - but it might get overwritten the next time coreutils package is updated.
I've only found out it's possible to use native symlinks when I already had Cygwin installed, and added some symlinks by myself as well. So after I set CYGWIN=winsymlinks:native as my system environment variable, I wanted to convert all the existing non-native links to native. Here's what I did.
Just in case, back up your entire Cygwin directory first.
Find all symlinks and save the list to /links file:
cd /; find . -regextype egrep -regex './(dev|proc|mnt|cygdrive)' -prune -o -type l -print >links
Review links.
Create a tar archive with all the links: tar c --files-from=links >links.tar
Extract the tar archive: tar x --files-from=links <links.tar
Since native symlinks are now enabled, tar will overwrite the old Cygwin's symlinks with native symlinks.
Clean up: rm -f links links.tar
P.S. At first I used CYGWIN=winsymlinks:nativestrict, but then I found out that in this mode, ln -s target link fails if target doesn't exist. By contrast, native will create a Cygwin (non-native) symlink link pointing to the nonexistent target - this matches the behavior of ln on UNIX systems. In rare cases, nativestrict can break some programs or scripts, for example Gentoo run-crons script uses a lockfile which is a symlink pointing to the PID of the running process. In nativestrict mode the script stopped working, because it could no longer create the lockfile. Note: run-crons is a crontab helper script on Gentoo Linux, adding support for cron.{hourly,daily,weekly,monthly}/ dirs, it works very well with Cygwin.
Since #olibre answer didn't work for me. I just created a shell function.
: '
mklink - Create NTFS (Windows) links that is usable by Windows and Cygwin
Usage: mklink [/D | /H | /J] <link-path> <target-path>
Options:
/D Directory Symbolic Link
/H Hardlink
/J Directory Junction (you should prefer /D)
With no options, it creates a NTFS file symlink.
'
mklink () {
if [ "$#" -ge "3" ]; then
cmd /c mklink "$1" "$(cygpath --windows --absolute "$2")" "$(cygpath --windows --absolute "$3")"
else
cmd /c mklink "$(cygpath --windows --absolute "$1")" "$(cygpath --windows --absolute "$2")"
fi
}
Do note you need administrator permissions (for Cygwin) to run the above without problems.
Note that I am unaware whether there's any difference between symlinking to an absolute path versus symlinking to a relative path using CMD's mklink. On Linux, those 2 have different behaviours if you ever decide to move the symlink or move the target file, or move both.
I guess the easiest way is to
grant SeCreateSymbolicLinkPrivilege from Local Group Policy editor (gpedit.msc, on path by default, non-home versions)
create script named ln on path (batch or bash), implementation
similar to above described shell function
profit
You were probably looking for a way to get to another destination in catalogue tree using MSYS. There is a way. You should create a shell script ("*.sh" file) which contains line:
cd "/drive_letter/SubCatalogue/SubFolder/..."

Make cygwin home directory to Windows' User Profile

As I am developing Ruby on Rails on a Windows machine, I need to use cygwin to emulate the Unix command prompt. The problem now is that every time when I open the cygwin terminal, I am brought to this directory C:/cygwin/home/my_user_name instead of the Windows' default user directory C:/Users/my_user_name.
Does anyone know how to make cygwin's default home directory to Windows default C:/Users/my_user_name directory?
I have skimmed through the various solutions provide in Stack Overflow, but none of them works for me, the "mkpasswd" doesn't work either. Does this have something to do with my operating system's version, or maybe something else?
I am using cygwin 1.7.5 and my operating system is Windows 7 Business 64 bit.
mount -f "$USERPROFILE" ~
mount -m > /etc/fstab
Related
Safely change home directory
According to Cygwin documentation you can edit /etc/nsswitch.conf and change de db_home parameter.
%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,
%_ - 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.
Here is the content of my /etc/nsswitch.conf to create a home directory into each user directory
# /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 <------ This was the default setting
db_home: /%H/home # db_home: /home/%U <- This was the default setting
# db_shell: /bin/bash
# db_gecos: <empty>
Restart any Cygwin process
I was able to change mine by simply setting the HOME environment variable in Windows to C:\Users\MyUsername. When I start Cygwin, now it looks there. This has the added benefit of causing Emacs on regular Win32 (i.e., not via Cygwin) to start in the right place instead of in C:\Users\MyUsername\AppData\Roaming (and thus looking for .emacs and .emacs.d there)

How can I change my Cygwin home folder after installation?

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?
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:
Consider moving /etc/passwd and /etc/group out of the way in order to use the new SAM/AD-based mechanism instead.
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.
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:
Cygwin user names, home dirs, login shells
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.
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.
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”.
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”.
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:
Open System on the Control Panel
On the Advanced tab click Environment Variables (toward the bottom)
In the User Variables area click "New…"
For Variable name enter HOME
For Variable value enter %USERPROFILE%
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
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.
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
Change your HOME environment variable.
on XP, its right-click My Computer >> Properties >> Advanced >> Environment Variables >> User Variables for >> [select variable HOME] >> edit
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.
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.

Resources