查看用户的所属组别.和文件的的所属的组别,当前用户所属组别.
hkbdm@ubuntu:/media/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/etc/wifi$ ls /dev/ttyS0 -l
crw-rw---- 1 root dialout 4, 64 Dec 9 16:40 /dev/ttyS0
//文件所属组别为dialout 拥有者为root
组别不存在导致查看文件为空.
比如u盘文件的组别为:
-r--r----- 1 1010 1010 77 Dec 11 01:21 wpa_supplicant.conf
你可以用root去查看,但是用非root帐号查看不了.
在创建时,任何文件的拥有者都是创建该文件的用户。当然用户可以修改该文件的拥有者及拥有者组。在 shell 中,使用 chmod 和 chgrp函数来修改。示例如下:
[root@localhost home]# touch testfile //由 root 用户创建文件[root@localhost home]# ls testfile –l
-rw--w--w- 1 root root 0 Jun 7 19:35 testfile //文件的拥有者及拥有者级均为 root
[root@localhost home]# chown yangzongde testfile //修改文件拥有者为 yangzongde
[root@localhost home]# ls testfile -l
-rw--w--w- 1 yangzongde root 0 Jun 7 19:35 testfile //查看文件拥有者为 yangzongde,但组
仍为 root
[root@localhost home]# chgrp yangzongde testfile //修改拥有者组为 yangzongde
[root@localhost home]# ls testfile -l
-rw--w--w- 1 yangzongde yangzongde 0 Jun 7 19:35 testfile
[root@localhost home]# chown root:root testfile // 使用 chown 一次性修改拥有者及组
[root@localhost home]# ls testfile -l
-rw--w--w- 1 root root 0 Jun 7 19:35 testfile
在 Linux 下 C 应用编程中,可以使用 chown 函数来修改文件的拥有者及拥有者组。此函
数声明如下:
//come from /usr/include/unistd.h
/* Change the owner and group of FILE. */
extern int chown (__const char *__file, __uid_t __owner, __gid_t __group)__THROW
__nonnull ((1)) __wur;
此函数的第一个参数为欲修改用户的文件,第二个参数为修改后的文件拥有者,第三个
参数为修改后该文件拥有者所在的组。
对于已打开的文件,使用 fchown 函数来修改。其第一个参数为已打开文件的文件描述符,
其他同 chown 函数。该函数声明如下:
#if defined __USE_BSD || defined __USE_XOPEN_EXTENDED
/* Change the owner and group of the file that FD is open on. */
extern int fchown (int __fd, __uid_t __owner, __gid_t __group) __THROW __wur;
对于连接文件,则可以使用 lchown 函数。其参数同于 chown 函数。
/* Change owner and group of FILE, if it is a symbolic link the ownership of the symbolic
link is changed. */
extern int lchown (__const char *__file, __uid_t __owner, __gid_t __group) __THROW
__nonnull ((1)) __wur;
以上这 3 个函数如果执行成功,将返回 0,否则返回-1。
进程的组别问题,批量修改文件的组别
在linux里是可以通过ps 和grep来查询到的。如下图中使用ps aux|grep nginx来查询nginx进程使用什么账户运行的:
可见是使用的www账户运行的。那么对应的网站根目录及其下面的文件目录的拥有者和拥有组都应该是www,只有这样网站访问才会正常。
使用ls –l来查询文件目录的拥有者和拥有组,如下图:
使用chown可以修改权限:chown -R www.www /alidata/www/phpwind/即可将目录/alidata/www/phpwind下的所有文件和目录的拥有者拥有组都修改为www账