Linux中修改文件及目录权限

1.chmod 改变文件或目录权限

chmod命令中,u代表所有者,g代表所属组,o代表其他用户,a代表所有人。当修改完成后用ls -l命令查看修改结果。
用法:chmod [选项] 权限 文件或目录

[root@gao ~]# chmod u=rwx,g=rw,o=r /test/haha.txt
[root@gao ~]# chmod a=rwx /test/hi.txt 
[root@gao ~]# ls -l /test/hi.txt 
-rwxrwxrwx. 1 root root 0 3月  20 13:26 /test/hi.txt
//将hi.txt的所有人权限设置为可读可写可执行

使用字符形式修改的另一种形式,实在原有的基础上修改权限,方法是使用+/-的方式。如下命令:

[root@gao ~]# ls -l /user/install.log 
-rw-r--r--. 1 root root 0 3月  23 17:52 /user/install.log
[root@gao ~]# chmod u+x,g-r,o+w /user/install.log 
[root@gao ~]# ls -l /user/install.log 
-rwx---rw-. 1 root root 0 3月  23 17:52 /user/install.log

除了使用字符的方式外,chmod还支持使用数字的方式修改权限,数字与权限的对应关系为:r代表数字4,作用是读取文件内容;w代表数字2,作用是在目录下增,删,改文件文件与目录名称;x代表数字1,作用是可执行。

[root@gao ~]# chmod 653 /user/install.log 
[root@gao ~]# ls -l /user/install.log 
-rw-r-x-wx. 1 root root 0 3月  23 17:52 /user/install.log

2.chown 修改文件或目录的所有者与所属组

用法:chown [选项] [所有者] [:[所属组]] 文件或目录
选项:-R 递归将权限应用于所有的子目录与子文件。

[root@gao ~]# chown user:mail /user/install.log 
[root@gao ~]# ls -l /user/install.log 
-rw-r-x-wx. 1 user mail 0 3月  23 17:52 /user/install.log
//修改文件的所有者为user,所属组为mail
[root@gao ~]# chown :mail /user/install.log 
//仅修改文件所属组为mail
[root@gao ~]# chown user /user/install.log 
//仅修改文件所有者为root

猜你喜欢

转载自blog.csdn.net/gao_2109/article/details/88767421