[英]Why can't this user delete this file?
如果我做:
ls -al /usr/local/bin/kill-all-sales-apps
我懂了:
-r-xr-xr-- 1 jenkins root 68 Aug 4 12:10 kill-all-sales-apps
如果我先将sudo换成root,然后再su换成jenkins,我应该可以删除它,是吗?
有关目录及其父目录的其他相关信息:
drwxr-xr-x 2 root root 4096 Aug 4 12:11 .
drwxr-xr-x 10 root root 4096 May 7 17:20 ..
如果我做:
groups jenkins
然后我看到比用户“ jenkins”已经被添加到“ root”组:
jenkins : jenkins root run-server-software
但是如果我:
rm /usr/local/bin/kill-all-sales-apps
我得到:
rm: remove write-protected regular file ‘/usr/local/bin/kill-all-sales-apps’? y
rm: cannot remove ‘/usr/local/bin/kill-all-sales-apps’: Permission denied
为什么拒绝权限?
至于为什么 jenkins用户无法删除,jenkins用户需要对要删除的文件的父文件夹具有写权限。 这是因为您实际上是从父文件夹中删除目录条目。
通常,在大多数文件系统上,删除文件需要对父目录具有写许可权(并具有执行许可权,以便首先进入目录)。 (请注意,对于初学者来说,混乱的是文件本身的权限无关紧要。但是,除非使用-f选项,否则GNU rm要求确认是否要删除写保护的文件。)
所以尝试跑步...
ls -ld /usr/local/bin
并确保jenkins用户对/ usr / local / bin具有写权限
另一种方法是修改sudoers
以使jenkins用户的sudo权限通过sudo仅rm该文件。 这是一个示例,授予用户joe显式权限,以从他没有写许可权的目录中sudo rm文件/usr/local/src/noperms/hi.txt
。 但是限制他删除该目录中的其他任何内容。
例如:
[root@joeyoung.io ~]# mkdir -p /usr/local/src/noperms
[root@joeyoung.io ~]# chmod -R 455 /usr/local/src/noperms
[root@joeyoung.io ~]# touch /usr/local/src/noperms/hi.txt
[root@joeyoung.io ~]# echo "hi" >> /usr/local/src/noperms/hi.txt
[root@joeyoung.io ~]# chmod 455 /usr/local/src/noperms/hi.txt
[root@joeyoung.io ~]# su - joe
[joe@joeyoung.io ~]$ cat /usr/local/src/noperms/hi.txt
hi
[joe@joeyoung.io ~]$ rm /usr/local/src/noperms/hi.txt
rm: remove write-protected regular file `/usr/local/src/noperms/hi.txt'? y
rm: cannot remove `/usr/local/src/noperms/hi.txt': Permission denied
[joe@joeyoung.io ~]$ exit
[root@joeyoung.io ~]# visudo
[root@joeyoung.io ~]# diff -Nur /tmp/sudoers.orig /etc/sudoers
--- /tmp/sudoers.orig 2015-08-04 17:17:24.020781442 +0200
+++ /etc/sudoers 2015-08-04 17:24:21.258274163 +0200
@@ -101,6 +101,7 @@
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
+joe ALL=(root) NOPASSWD: /bin/rm /usr/local/src/noperms/hi.txt
## Allows members of the 'sys' group to run networking, software,
## service management apps and more.
[root@joeyoung.io ~]# su - joe
[joe@joeyoung.io ~]$ sudo /bin/rm /usr/local/src/noperms/hi.txt
[joe@joeyoung.io ~]$ exit
[root@joeyoung.io ~]# ls -al /usr/local/src/noperms/hi.txt
ls: cannot access /usr/local/src/noperms/hi.txt: No such file or directory
[root@joeyoung.io ~]# ls -al /usr/local/src/noperms/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.