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?
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.
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
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:
HOME
%USERPROFILE%
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'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.
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
I happen to use cwRsync (Cygwin + Rsync for Windows) where cygwin comes bundled, and I couldnt 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 HOME variable before running rsync. Something like:
set HOME=.
rsync /path1 user@host:/path2
And viola! .ssh folder appeared in 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.
Change your HOME environment variable.
on XP, its right-click My Computer >> Properties >> Advanced >> Environment Variables >> User Variables for >> [select variable HOME] >> edit
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.