chmod cannot change group permission on Cygwin

2020-02-05 12:34发布

问题:

I am using Cygwin and trying to change the group access permission with chmod, e.g.

$ls -l id_rsa
-rwxrwxr-- 1 None 1679 Jun 13 10:16 id_rsa 

$ chmod g= id_rsa 

$ ls -l id_rsa 
-rwxrwxr-- 1 None 1679 Jun 13 10:16 id_rsa 

But this does not work. I can change permission for user and others. Seems that the permission level for group somehow keeps the same as that of user?

回答1:

I was having a similar problem to you, and I was using the NTFS filesystem, so Keith Thompson's answer didn't solve it for me.

I changed the file's group owner to the Users group:

chown :Users filename

After doing that I was able to change the group permissions to my will using chmod. In my case, since it was an RSA key for OpenSSH, I did:

chmod 700 filename

And it worked. In Cygwin you get two groups by default, the Root group and the Users group. I wanted to add another group, but I wasn't able to do it with the tools I'm used to use on Linux. For that reason I just used the Users group.



回答2:

Cygwin doesn't like files to be owned by groups that it doesn't know. Unfortunately, that happens quite often in Cygwin, especially if your PC is in a Windows domain where things keep changing. I also synchronise my files between two PCs, via an external drive, and the uids/gids are different between the different PCs, so this is a source of problems.

If you do ls -l and see a numeric group id instead of a group name, it means Cygwin doesn't know the gid - i.e. it's not in /etc/group, and Cygwin can't query it from Windows either. You can confirm this by running getent group <gid>, where <gid> is the numeric group id.

To fix it, you can either use chgrp to change the group for all affected files/directories, as described in the accepted answer above, or create an entry for the unknown gid in /etc/group, with any unused group name (e.g. Users2).

After doing this, it may be necessary to close all of your Cygwin windows and then re-open them.



回答3:

An experiment shows that chmod does work correctly to change group permissions under Cygwin.

The experiment used a file on an NTFS partition. Cygwin implements a POSIX layer on top of Windows, but it still ultimately uses the features of Windows itself, and of the particular filesystem implementation.

On modern versions of Windows, most hard drives are formatted to use NTFS, which provides enough support for chmod. But external USB drives typically use FAT32, which doesn't have the same abilities to represent permissions. The Cygwin layer fakes POSIX semantics as well as it can, but there's only so much it can do.

Try

$ df -T .

If it indicates that you're using a FAT32 filesystem, that's probably the problem. The solution would be to store the file on an NTFS filesystem instead. A file named id_dsa is probably an SSH private key, and it needs to be stored in $HOME/.ssh anyway.

Is your home directory on a FAT32 partition? As I recall, recent versions of Windows ("recent" meaning the last 10 or more years) are able to convert FAT32 filesystems to NTFS.


The remainder of this answer was in response to the original version of the question, which had a typo in the chmod command.


Cygwin uses the GNU Coreutils version of chmod. This,

chmod g=0 fileName

is not the correct syntax. I get:

$ chmod g=0 fileName
chmod: invalid mode: `g=0'
Try `chmod --help' for more information.

(This is on Linux, not Cygwin, but it should be the same.)

To turn off all group permissions, this should work:

$ chmod g= fileName
$ ls -l fileName 
-rw----r-- 1 kst kst 0 Jun 13 10:31 fileName

To see the chmod documentation:

$ info coreutils chmod

To see the documentation on symbolic file mode:

$ info coreutils Symbolic

The format of symbolic modes is:

 [ugoa...][+-=]PERMS...[,...]

where PERMS is either zero or more letters from the set 'rwxXst', or a single letter from the set 'ugo'.



回答4:

Like previous answers, not recognized groups cause such issues. It mostly happens in Windows Domains.

The easiest way to fix it is regenerate your /etc/passwd and /etc/group files (parameter -d is needed for domain users):

mkpasswd -l -d > /etc/passwd
mkgroup  -l -d > /etc/group

Close and launch Cygwin again.



回答5:

This is a very annoying issue for me. In my case user135348's solution worked best. The biggest issue with the chown :Users -R approach is that every time a new file is created, it will be assigned to the unknown gid 1049120. It's very frustrating to keep changing file gid.

I tried mkgroup too, but in my case it didn't work: My gid is 1049120.

Based on the rules explained in Mapping Windows SIDs to POSIX uid/gid values : : 0x100000 offset is used for account from the machine's primary domain. Trying to remove the same offset from 1049120, you get 544, which is built-in Administrators group's RID.

This account is not a member of the local Administrators group; we use SuRun to grant administrator rights without giving out credentials. In this case, mkgroup failed to generate all the possible gids.

Editing the group file and adding a customized group name seems always to fix the issue easily.



回答6:

I had this issue when working remotely from the Domain and using cygserver.

Running ls -l showed a numeric group id instead of a group name.

I stopped cygserver, net stop "CYGWIN cygserver, and other Cygwin processes, then ran the ls -l again, and group names were then displayed correctly.

I guess cygserver was holding incomplete domain group information.

After restarting cygserver the system continued to work correctly.



回答7:

You must specify the group name on the Windows system which your user belongs to.

So I just did this: chown -R ONEX:Users ~/*

You can find your user name and group here:



标签: cygwin