一,chgrp改变文件所属群组
- 为了学习需求,首先我们增加一个testgroup用户组
# groupadd testgroup
- 查看用户组有没有创建成功
# cat /etc/passwd
最底部出现了我们刚才创建的用户组的名字,表示创建成功
testgroup:x:1000:
- 再添加一个用户并归属到testgroup用户组
# useradd -g testgroup testuser
- 查看用户有没有创建成功
# cat /etc/passwd
#testuser:x:1000:1000::/home/testuser:/bin/sh
用户名:口令:用户标识号:组标识号:注释性描述:主目录:登录Shell
- 创建一个名为testdir的文件夹
# mkdir testdir
- 查看文件属性
# ls -l
drwxr-xr-x 2 root root 4096 Feb 13 17:47 testdir
- 用chgrp改变文件所属用户组
# chgrp -R testgroup testdir
-R表示该文件夹下的所有子文件都应用该改变
- 再查看文件夹属性
# ls -l
drwxr-xr-x 2 root testgroup 4096 Feb 13 17:47 testdir
可以看到该文件夹的用户组已经由root改变到我们刚才创建的testgroup了
二,chown改变文件拥有者
这个命令既可以改变文件的拥有者,也可以和我们刚才的chgrp命令一样改变文件的群组
- 首先我们来说下改变文件拥有者的用法
# chown -R testuser testdir
# ls -l
drwxr-xr-x 2 testuser testgroup 4096 Feb 13 17:47 testdir
可以看到文件的拥有者已经由root改变到testuser
- 同时改变文件的拥有者和用户组
# chown -R root:root testdir
# ls -l
drwxr-xr-x 2 root root 4096 Feb 13 17:47 testdir
中间的冒号“:”可用点“.”代替,但由于文件后缀名前带“.”,会造成系统误判,所以推荐用“:”,为了证明可用,我们来试下
# chown -R testuser.testgroup testdir
# ls -l
drwxr-xr-x 2 testuser testgroup 4096 Feb 13 17:47 testdir
- 单独改变文件组的用法
# chown -R .root testdir
注意root前面有个点
ls -l
drwxr-xr-x 2 testuser root 4096 Feb 13 17:47 testdir
三,chmod改变文件的权限
这个改变权限的有两种方法,一种是数字类型改变文件权限,一种是字符类型改变文件权限;
1.数字类型改变文件权限
字母 | 代表数字 |
---|---|
r | 4 |
w | 2 |
x | 1 |
权限 | 字母表示 | 数字表示 |
---|---|---|
读 | r– | 4+0+0=4 |
写 | -w- | 0+2+0=2 |
执行 | –x | 0+0+1=1 |
拥有读和写 | rw- | 4+2=6 |
拥有读和执行 | r-x | 4+1=5 |
拥有写和执行 | -wx | 2+1=3 |
拥有全部权限 | rwx | 4+2+1=7 |
无权限 | — | 0+0+0=0 |
- 举几个栗子
· | 字母表示 | 数字表示 |
---|---|---|
权限(owner/group/others) | -rwxrwxrwx | [4+2+1][4+2+1][4+2+1]=777 |
. | -rwx------ | [4+2+1][0+0+0][0+0+0]=700 |
. | -rwxr–r-- | [4+2+1][4+0+0][4+0+0]=744 |
… | … | … |
- 数字类型改变文件权限
# chmod 777 testdir
# ls -l
drwxrwxrwx 2 testuser root 4096 Feb 13 17:47 testdir
2.字符类型改变文件权限
命令 | 身份 | 操作 | 文件名 |
---|---|---|---|
chmod | u(user) g(group) o(others) a(all) | +(添加权限) -(去除权限) =(设置权限) | filename/dirname |
举例子
- 去除group的写入(w)权限
# chmod g-w testdir
# ls -l
drwxr-xrwx 2 testuser root 4096 Feb 13 17:47 testdir
- 加上group的w权限
# chmod g+w testdir
# ls -l
drwxrwxrwx 2 testuser root 4096 Feb 13 17:47 testdir
- 直接设置group的权限为可读和可写入
# chmod g=rw testdir
# ls -l
drwxrw-rwx 2 testuser root 4096 Feb 13 17:47 testdir
- 去除ugo的执行权限
# chmod ugo-x testdir
或者
# chmod a-x testdir
结果:
# ls -l
drw-rw-rw- 2 testuser root 4096 Feb 13 17:47 testdir
一般设置全部身份的权限操作时推荐直接用a,其他的一些我就不一一举例子了,都是一些字母组合来使用的;如果要改变其子目录所有的文件权限,直接前面加个命令后面加个-R,列如 chmod -R a-x testdir;还有一些相关用法,直接chgrp/chown/chmod --help;