共计 12642 个字符,预计需要花费 32 分钟才能阅读完成。
本篇内容介绍了“linux 用户权限设置有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让丸趣 TV 小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
1. 权限的基本概述
什么是权限
系统对用户所能执行的功能的限制
total 61152
-rw-r--r--. 1 root root 39 Jul 17 19:16 123.txt
-rw-r--r--. 1 root root 9272936 Jul 17 12:01 access.log
-R # 针对目录设置权限 赋予目录及目录以下所有文件的权限
2. 权限的设置案例
# 添加权限
- # 收回某个权限
= # 覆盖之前的权限
total 4
-rw-r--r-- 1 root root 158 Jul 23 09:06 hosts
[root@qls ~]# chmod u+x hosts
[root@qls ~]# ll
total 4
-rwxr--r-- 1 root root 158 Jul 23 09:06 hosts
[root@qls ~]# chmod g+wx hosts
[root@qls ~]# ll
total 4
-rwxrwxr-- 1 root root 158 Jul 23 09:06 hosts
[root@qls ~]# chmod o+w hosts
[root@qls ~]# ll
total 4
-rwxrwxrw- 1 root root 158 Jul 23 09:06 hosts
total 4
-rwxrwxrwx 1 root root 158 Jul 23 09:06 hosts
total 4
-rw-rw-rw- 1 root root 158 Jul 23 09:06 hosts
total 4
-r--rw-rw- 1 root root 158 Jul 23 09:06 hosts
[root@qls ~]# chmod g-w,o-rw hosts
[root@qls ~]# ll
total 4
-r--r----- 1 root root 158 Jul 23 09:06 hosts
[root@qls ~]#
#覆盖之前的所有权限
[root@qls ~]# chmod a=rw hosts
[root@qls ~]# ll
total 4
-rw-rw-rw- 1 root root 158 Jul 23 09:06 hosts
total 4
-rw-rw---- 1 root root 158 Jul 23 09:06 hosts
total 4
-rw-r--r-- 1 root root 158 Jul 23 09:06 hosts
[root@qls ~]# mkdir data
[root@qls ~]# ll
total 4
drwxr-xr-x 2 root root 6 Jul 23 09:35 data
-rw-r--r-- 1 root root 158 Jul 23 09:06 hosts
[root@qls ~]# cp /etc/hosts data/
[root@qls ~]# ll data/
total 4
-rw-r--r-- 1 root root 158 Jul 23 09:35 hosts
[root@qls ~]# ll -d data/
drwxr-xr-x 2 root root 19 Jul 23 09:35 data/
[root@qls ~]# chmod 700 data/
[root@qls ~]# ll -d data/
drwx------ 2 root root 19 Jul 23 09:35 data/
[root@qls ~]# ll data/
total 4
-rw-r--r-- 1 root root 158 Jul 23 09:35 hosts
[root@qls ~]# chmod -R 755 data/
[root@qls ~]# ll -d data/
drwxr-xr-x 2 root root 19 Jul 23 09:35 data/
[root@qls ~]# ll data/
total 4
-rwxr-xr-x 1 root root 158 Jul 23 09:35 hosts
total 0
drwxr-xr-x 2 root root 6 Jul 23 09:40 test
[root@qls ~]# groupadd dev
[root@qls ~]# chgrp dev /opt/test/
[root@qls ~]# ll /opt/
total 0
drwxr-xr-x 2 root dev 6 Jul 23 09:40 test
[root@qls ~]# chmod 760 /opt/test/
[root@qls ~]# ll /opt/
total 0
drwxrw---- 2 root dev 6 Jul 23 09:40 test
3. 权限对文件或者目录的影响
cat head tail 浏览目录及子目录的列表 ls tree
w 可以新增,修改文件内容的权利 vim echo 可以新建或者删除,移动目录中的文件的权利
x 可以执行文件的权利 脚本 可以进入目录 cd
4. 文件权限设置案例
[root@qls ~]# echo hostname /opt/file.txt
[root@qls ~]# ll /opt/
total 4
-rw-r--r-- 1 root root 9 Jul 23 09:57 file.txt
[root@qls ~]# useradd qls01
[root@qls ~]#
[root@qls ~]# echo 1 | passwd --stdin qls01
Changing password for user qls01.
passwd: all authentication tokens updated successfully.
[root@qls ~]# su - qls01
Last login: Thu Jul 23 09:59:14 CST 2020 on pts/0
[qls01@qls ~]$ ll /opt/
total 4
-rw-r--r-- 1 root root 9 Jul 23 09:57 file.txt
drwxrw---- 2 root dev 6 Jul 23 09:40 test
[qls01@qls ~]$ cat /opt/file.txt
hostname
[qls01@qls ~]$ head /opt/file.txt
hostname
[qls01@qls ~]$ tail /opt/file.txt
hostname
[qls01@qls ~]$ vim /opt/file.txt
[qls01@qls ~]$ echo 123 /opt/file.txt
-bash: /opt/file.txt: Permission denied
[qls01@qls ~]$ /opt/file.txt
-bash: /opt/file.txt: Permission denied
[root@qls ~]# chmod o=w /opt/file.txt
[root@qls ~]# ll /opt/file.txt
-rw-r---w- 1 root root 9 Jul 23 09:57 /opt/file.txt
[root@qls ~]# su - qls01
Last login: Thu Jul 23 10:00:14 CST 2020 on pts/0
[qls01@qls ~]$ cat /opt/file.txt
cat: /opt/file.txt: Permission denied
[qls01@qls ~]$ vim /opt/file.txt
[qls01@qls ~]$ cat /opt/file.txt
cat: /opt/file.txt: Permission denied
[qls01@qls ~]$ /opt/file.txt
-bash: /opt/file.txt: Permission denied
[root@qls ~]# cat /opt/file.txt
uirethruie
[qls01@qls ~]$ echo test /opt/file.txt
[qls01@qls ~]$ echo test /opt/file.txt
[root@qls ~]# cat /opt/file.txt
uirethruie
[root@qls ~]# cat /opt/file.txt
# 只有 w 权限时, 无法查看和执行文件的权限 使用 vim 编辑文件文件时,无法查看里面的内容,可以进行编辑,但是需要强制保存,但是保存之后,原来的内容被覆盖了 可以使用 echo 命令进行追加或者重定向内容进去
[root@qls ~]# chmod o=x /opt/file.txt
[root@qls ~]# ll /opt/file.txt
-rw-r----x 1 root root 5 Jul 23 10:09 /opt/file.txt
[root@qls ~]# su - qls01
Last login: Thu Jul 23 10:08:38 CST 2020 on pts/0
[qls01@qls ~]$ ll /opt/file.txt
-rw-r----x 1 root root 5 Jul 23 10:09 /opt/file.txt
[qls01@qls ~]$ cat /opt/file.txt
cat: /opt/file.txt: Permission denied
[qls01@qls ~]$ echo 123 /opt/file.txt
-bash: /opt/file.txt: Permission denied
[qls01@qls ~]$ /opt/file.txt
bash: /opt/file.txt: Permission denied
#文件只有 x 权限 什么都干不了
[root@qls ~]# chmod o=rw /opt/file.txt
[root@qls ~]# ll /opt/file.txt
-rw-r--rw- 1 root root 5 Jul 23 10:09 /opt/file.txt
[root@qls ~]# su - qls01
Last login: Thu Jul 23 10:11:27 CST 2020 on pts/0
[qls01@qls ~]$ cat /opt/file.txt
[qls01@qls ~]$ echo hostname /opt/file.txt
[qls01@qls ~]$ vim /opt/file.txt
[qls01@qls ~]$ cat /opt/file.txt
hostname
[qls01@qls ~]$ /opt/file.txt
-bash: /opt/file.txt: Permission denied
[root@qls ~]# chmod o=rx /opt/file.txt
[root@qls ~]# ll /opt/file.txt
-rw-r--r-x 1 root root 13 Jul 23 10:14 /opt/file.txt
[root@qls ~]# su - qls01
Last login: Thu Jul 23 10:14:16 CST 2020 on pts/0
[qls01@qls ~]$ cat /opt/file.txt
hostname
[qls01@qls ~]$ echo 123 /opt/file.txt
-bash: /opt/file.txt: Permission denied
[qls01@qls ~]$ vim /opt/file.txt
[qls01@qls ~]$ /opt/file.txt
/home/qls01
Permission denied # 权限不足 没有权限
5. 目录权限设置案例
权限对目录的影响
[root@qls ~]# chmod o=r /opt/test/
[root@qls ~]# ll /opt/
total 4
-rw-r--r-x 1 root root 13 Jul 23 10:14 file.txt
drwxrw-r-- 2 root root 6 Jul 23 09:40 test
[root@qls ~]# touch /opt/test/data.{txt,log,sh}
[root@qls ~]# mkdir /opt/test/oldboy{01..03}
[root@qls ~]# ll /opt/test/
total 0
-rw-r--r-- 1 root root 0 Jul 23 10:51 data.log
-rw-r--r-- 1 root root 0 Jul 23 10:51 data.sh
-rw-r--r-- 1 root root 0 Jul 23 10:51 data.txt
drwxr-xr-x 2 root root 6 Jul 23 10:51 oldboy01
drwxr-xr-x 2 root root 6 Jul 23 10:51 oldboy02
drwxr-xr-x 2 root root 6 Jul 23 10:51 oldboy03
[root@qls ~]# su - qls01
Last login: Thu Jul 23 10:51:09 CST 2020 on pts/0
[qls01@qls ~]$ ls /opt/test/
ls: cannot access /opt/test/data.txt: Permission denied
ls: cannot access /opt/test/data.log: Permission denied
ls: cannot access /opt/test/data.sh: Permission denied
ls: cannot access /opt/test/oldboy01: Permission denied
ls: cannot access /opt/test/oldboy02: Permission denied
ls: cannot access /opt/test/oldboy03: Permission denied
data.log data.sh data.txt oldboy01 oldboy02 oldboy03
[qls01@qls ~]$ ls -l /opt/test/
ls: cannot access /opt/test/data.txt: Permission denied
ls: cannot access /opt/test/data.log: Permission denied
ls: cannot access /opt/test/data.sh: Permission denied
ls: cannot access /opt/test/oldboy01: Permission denied
ls: cannot access /opt/test/oldboy02: Permission denied
ls: cannot access /opt/test/oldboy03: Permission denied
total 0
-????????? ? ? ? ? ? data.log
-????????? ? ? ? ? ? data.sh
-????????? ? ? ? ? ? data.txt
d????????? ? ? ? ? ? oldboy01
d????????? ? ? ? ? ? oldboy02
d????????? ? ? ? ? ? oldboy03
[qls01@qls ~]$ rm -f /opt/test/data.log
rm: cannot remove ‘/opt/test/data.log’: Permission denied
[qls01@qls ~]$ touch /opt/test/data.conf
touch: cannot touch ‘/opt/test/data.conf’: Permission denied
[qls01@qls ~]$ mv /opt/test/data.log /tmp
mv: cannot stat ‘/opt/test/data.log’: Permission denied
[qls01@qls ~]$ cd /opt/test/
-bash: cd: /opt/test/: Permission denied
[qls01@qls ~]$ tree /opt/test/ # 没有任何统计
/opt/test/
0 directories, 0 files
# 目录只有 r 权限,使用 ls - l 命令查看目录下的列表,会出现一堆的权限不足,但是文件名和文件类型显示出来了,其他的属性信息都是问号 不能对此目录下的文件进行新建或者删除及其移动的操作 也不能切换到这个目录
[root@qls ~]# chmod o=w /opt/test/
[root@qls ~]# ll /opt/
total 4
-rw-r--r-x 1 root root 13 Jul 23 10:14 file.txt
drwxrw--w- 5 root root 101 Jul 23 10:51 test
[root@qls ~]# su - qls01
Last login: Thu Jul 23 10:52:22 CST 2020 on pts/0
[qls01@qls ~]$ ls /opt/test/
ls: cannot open directory /opt/test/: Permission denied
[qls01@qls ~]$ ls -l /opt/test/
ls: cannot open directory /opt/test/: Permission denied
[qls01@qls ~]$ cd /opt/test/
-bash: cd: /opt/test/: Permission denied
[qls01@qls ~]$ touch /opt/test/123.txt
touch: cannot touch ‘/opt/test/123.txt’: Permission denied
[qls01@qls ~]$ rm -f /opt/test/data.log
rm: cannot remove ‘/opt/test/data.log’: Permission denied
[qls01@qls ~]$ mv /opt/test/data.log /tmp/
mv: cannot stat ‘/opt/test/data.log’: Permission denied
[qls01@qls ~]$ cp /opt/test/data.log /tmp
cp: cannot stat ‘/opt/test/data.log’: Permission denied
[qls01@qls ~]$ cp /opt/test/data.log /tmp
cp: cannot stat ‘/opt/test/data.log’: Permission denied
[qls01@qls ~]$ mv /opt/test/data.log /tmp/
mv: cannot stat ‘/opt/test/data.log’: Permission denied
[qls01@qls ~]$ rm -f /opt/test/data.log
rm: cannot remove ‘/opt/test/data.log’: Permission denied
[qls01@qls ~]$ ll -d /opt/
drwxrwxrwx 3 root root 34 Jul 23 09:57 /opt/
#只有 w 权限时,目录什么都做不了
[root@qls ~]# su - qls01
Last login: Thu Jul 23 10:57:51 CST 2020 on pts/0
[qls01@qls ~]$ cd /opt/test/
[qls01@qls test]$ ls
ls: cannot open directory .: Permission denied
[qls01@qls test]$ ls -l
ls: cannot open directory .: Permission denied
[qls01@qls test]$ rm -f data.log
rm: cannot remove ‘data.log’: Permission denied
[qls01@qls test]$ touch data.conf
touch: cannot touch ‘data.conf’: Permission denied
[qls01@qls test]$ mv data.log /tmp/
mv: cannot move ‘data.log’ to ‘/tmp/data.log’: Permission denied
[qls01@qls test]$ cp data.log /tmp/
[qls01@qls test]$ ll /tmp/
total 4
drwxr-xr-x. 2 root root 51 Jul 17 17:59 data1
drwxr-xr-x. 2 root root 51 Jul 17 17:59 data2
drwxr-xr-x. 2 root root 51 Jul 17 17:59 data3
-rw-r--r-- 1 qls01 qls01 0 Jul 23 11:03 data.log
#目录只有 x 权限时,可以切换到目录中,无法查看目录列表信息 也无法进行删除、新建、移动等操作 可以进行复制操作
[root@qls ~]# chmod o=rw /opt/test/
[root@qls ~]# ll /opt/
total 4
-rw-r--r-x 1 root root 13 Jul 23 10:14 file.txt
drwxrw-rw- 5 root root 101 Jul 23 10:51 test
[root@qls ~]# su - qls01
Last login: Thu Jul 23 11:06:21 CST 2020 on pts/0
[qls01@qls ~]$ cd /opt/test/
-bash: cd: /opt/test/: Permission denied
[qls01@qls ~]$ ls /opt/test/
ls: cannot access /opt/test/data.txt: Permission denied
ls: cannot access /opt/test/data.log: Permission denied
ls: cannot access /opt/test/data.sh: Permission denied
ls: cannot access /opt/test/oldboy01: Permission denied
ls: cannot access /opt/test/oldboy02: Permission denied
ls: cannot access /opt/test/oldboy03: Permission denied
data.log data.sh data.txt oldboy01 oldboy02 oldboy03
[qls01@qls ~]$ ls -l /opt/test/
ls: cannot access /opt/test/data.txt: Permission denied
ls: cannot access /opt/test/data.log: Permission denied
ls: cannot access /opt/test/data.sh: Permission denied
ls: cannot access /opt/test/oldboy01: Permission denied
ls: cannot access /opt/test/oldboy02: Permission denied
ls: cannot access /opt/test/oldboy03: Permission denied
total 0
-????????? ? ? ? ? ? data.log
-????????? ? ? ? ? ? data.sh
-????????? ? ? ? ? ? data.txt
d????????? ? ? ? ? ? oldboy01
d????????? ? ? ? ? ? oldboy02
d????????? ? ? ? ? ? oldboy03
[qls01@qls ~]$ touch /opt/test/data.conf
touch: cannot touch ‘/opt/test/data.conf’: Permission denied
[qls01@qls ~]$ rm -f /opt/test/data.log
rm: cannot remove ‘/opt/test/data.log’: Permission denied
[qls01@qls ~]$ mv /opt/test/data.log /tmp/
mv: cannot stat ‘/opt/test/data.log’: Permission denied
[qls01@qls ~]$ cp /opt/test/data.txt /tmp/
cp: cannot stat ‘/opt/test/data.txt’: Permission denied
[root@qls ~]# chmod o=rx /opt/test/
[root@qls ~]# ll /opt/
total 4
-rw-r--r-x 1 root root 13 Jul 23 10:14 file.txt
drwxrw-r-x 5 root root 101 Jul 23 10:51 test
[root@qls ~]# su - qls01
Last login: Thu Jul 23 11:10:14 CST 2020 on pts/0
[qls01@qls ~]$ cd /opt/test/
[qls01@qls test]$ ls
data.log data.sh data.txt oldboy01 oldboy02 oldboy03
[qls01@qls test]$ ls -l
total 0
-rw-r--r-- 1 root root 0 Jul 23 10:51 data.log
-rw-r--r-- 1 root root 0 Jul 23 10:51 data.sh
-rw-r--r-- 1 root root 0 Jul 23 10:51 data.txt
drwxr-xr-x 2 root root 6 Jul 23 10:51 oldboy01
drwxr-xr-x 2 root root 6 Jul 23 10:51 oldboy02
drwxr-xr-x 2 root root 6 Jul 23 10:51 oldboy03
[qls01@qls test]$ touch data.conf
touch: cannot touch ‘data.conf’: Permission denied
[qls01@qls test]$ rm -f data.log
rm: cannot remove ‘data.log’: Permission denied
[qls01@qls test]$ mv data.txt /tmp/
mv: cannot move ‘data.txt’ to ‘/tmp/data.txt’: Permission denied
[qls01@qls test]$ cp data.txt /tmp/
[qls01@qls test]$ ll /tmp/
total 4
drwxr-xr-x. 2 root root 51 Jul 17 17:59 data1
drwxr-xr-x. 2 root root 51 Jul 17 17:59 data2
drwxr-xr-x. 2 root root 51 Jul 17 17:59 data3
-rw-r--r-- 1 qls01 qls01 0 Jul 23 11:03 data.log
-rw-r--r-- 1 qls01 qls01 0 Jul 23 11:13 data.txt
# 目录拥有 rx 权限时,可以正常的查看目录列表信息,属性信息,也可以进入目录,可以复制文件到其他目录,但是不能执行新建、删除、移动等操作
[root@qls ~]# chmod o=wx /opt/test/
[root@qls ~]# ll /opt/
total 4
-rw-r--r-x 1 root root 13 Jul 23 10:14 file.txt
drwxrw--wx 5 root root 101 Jul 23 10:51 test
[root@qls ~]# su - qls01
Last login: Thu Jul 23 11:12:31 CST 2020 on pts/0
[qls01@qls ~]$ cd /opt/test/
[qls01@qls test]$ ls
ls: cannot open directory .: Permission denied
[qls01@qls test]$ touch 123.txt
[qls01@qls test]$ ls
ls: cannot open directory .: Permission denied
[qls01@qls test]$ rm -f data.log
[qls01@qls test]$ mv data.txt /tmp/
[qls01@qls test]$ ls
ls: cannot open directory .: Permission denied
正文完