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
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%
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 thedb_home:
setting, this only makes sense right after the preceeding slash, as indb_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%
orC:\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
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.
来源:https://stackoverflow.com/questions/225764/safely-change-home-directory