chmod 000 解决思路

本文介绍了解决chmod命令无法更改文件权限的问题,提供了多种方法包括使用加载程序、busybox、facl、复制文件、install命令、Perl及Python脚本等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题如下:chmod没有权限,貌似就算是root用户也无法授权
[root@localhost ~]# ll/bin/chmod 
----------. 1 root root 48712Oct 15  2014 /bin/chmod
[root@localhost ~]# chmod 755/bin/chmod 
-bash: /bin/chmod: Permission denied
解决方法1:直接运行加载程序,并将其传递给想要运行的命令
[root@localhost ~]#/lib64/ld-linux-x86-64.so.2 /bin/chmod 755 /bin/chmod
[root@localhost ~]# ll/bin/chmod 
-rwxr-xr-x. 1 root root 48712Oct 15  2014 /bin/chmod
说明:加载程序路径可能不同,32位系统应该是/lib/ld-linux.so,我没有测试

解决方法2:可以使用busybox的chmod授权
[root@localhost ~]# busybox chmod 755 /bin/chmod
[root@localhost ~]# ll/bin/chmod 
-rwxr-xr-x. 1 root root 48712Oct 15  2014 /bin/chmod

解决方法3:此方法我表示很喜欢
[root@localhost ~]# chmod 000 /bin/chmod 
[root@localhost ~]# ll /bin/chmod 
----------. 1 root root 48712Oct 15  2014 /bin/chmod
[root@localhost ~]# mv /bin/chmod /bin/chmod.orig
[root@localhost ~]# cp -a /bin/chown /bin/chmod
[root@localhost ~]# dd if=/bin/chmod.orig of=/bin/chmod
95+1 records in
95+1 records out
48712 bytes (49 kB) copied,0.00117323 s, 41.5 MB/s

解决方法4:使用facl额外授权
[root@localhost ~]# chmod 000 /bin/chmod 
[root@localhost ~]# ll /bin/chmod 
----------. 1 root root 48712Oct 15  2014 /bin/chmod
[root@localhost ~]# setfacl -m u::rx /bin/chmod 
[root@localhost ~]# chmod 755 /bin/chmod 
[root@localhost ~]# setfacl -b /bin/chmod 

解决方法5:复制一个可执行文件,然后使用chmod命令覆盖
[root@localhost ~]# ll /bin/chmod 
----------. 1 root root 48712Oct 15  2014 /bin/chmod
[root@localhost ~]# cp /bin/ls chmod
[root@localhost ~]# cp /bin/chmod .
cp: overwrite `./chmod'? y
[root@localhost ~]# cp -a chmod/bin /chmod 
cp: overwrite `/bin/chmod'? y
[root@localhost ~]# ll/bin/chmod 
-rwxr-xr-x. 1 root root 48712May 27 10:23 /bin/chmod

解决方法6:使用install命令的-m选项也可以设置权限
[root@localhost ~]# ll/bin/chmod
----------. 1 root root 48712May 27 10:04 /bin/chmod
[root@localhost ~]# install -ma+x /bin/chmod .
[root@localhost ~]# ./chmod 755/bin/chmod
[root@localhost ~]# ll/bin/chmod
-rwxr-xr-x. 1 root root 48712May 27 10:04 /bin/chmod

解决方法7:
perl解决
[root@localhost ~]# ll /bin/chmod 
----------. 1 root root 48712Oct 15  2014 /bin/chmod
[root@localhost ~]# perl -e'chmod 0755, "/bin/chmod"'
[root@localhost ~]# ll /bin/chmod
-rwxr-xr-x. 1 root root 48712Oct 15  2014 /bin/chmod

解决方法8:Python解决
[root@localhost ~]# chmod 000/bin/chmod 
[root@localhost ~]# ll/bin/chmod 
----------. 1 root root 48712Oct 15  2014 /bin/chmod
[root@localhost ~]# python -c'import os; os.chmod("/bin/chmod", 0755)'
[root@localhost ~]# ll/bin/chmod 
-rwxr-xr-x. 1 root root 48712Oct 15  2014 /bin/chmod
说明:各种方法都是在centos6.6 64位系统上测试

http://bbs.51cto.com/viewthread.php?tid=1160795

### 使用 `chmod` 实现 `fchmod` 功能的替代方案 在 C 语言中,如果缺少 `fchmod` 接口来更改已打开文件的权限,可以通过以下方法实现类似的逻辑。这些方法主要依赖于文件路径或其他间接手段。 --- #### 方法一:通过文件路径调用 `chmod` 最直接的方式是获取文件的路径名并通过 `chmod` 更改其权限。然而,这种方法的前提条件是你能够获得文件对应的路径名。 ```c #include <sys/stat.h> #include <unistd.h> int chmod_via_path(const char *path, mode_t mode) { return chmod(path, mode); } ``` 此方法简单有效,但在某些场景下可能无法满足需求,因为并非总是可以获得文件的路径名[^1]。 --- #### 方法二:查找文件路径(通过 `/proc/self/fd`) 在 Linux 系统中,可以利用 `/proc/self/fd/<fd>` 来动态获取文件路径。这种机制允许通过文件描述符找到对应的实际路径,从而使用 `chmod` 完成权限修改。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <limits.h> char* get_filepath_from_fd(int fd) { static char path[PATH_MAX]; snprintf(path, PATH_MAX, "/proc/self/fd/%d", fd); realpath(path, path); // 获取实际路径 return path; } int chmod_via_proc_self_fd(int fd, mode_t mode) { char *filepath = get_filepath_from_fd(fd); if (!filepath || access(filepath, F_OK) != 0) { return -1; // 路径不可用或不存在 } return chmod(filepath, mode); } ``` 这段代码展示了如何通过 `/proc/self/fd` 动态解析文件路径,并最终调用 `chmod` 修改权限[^3]。 --- #### 方法三:复制文件并设置新权限 如果既没有文件路径也无法通过其他方式获取路径,可以选择创建一个具有所需权限的新文件,然后将原有内容复制过去。完成后删除旧文件并重命名新文件。 ```c #include <fcntl.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <stdio.h> int set_permissions_by_copying(int old_fd, const char *new_path, mode_t mode) { int new_fd = open(new_path, O_WRONLY | O_CREAT | O_TRUNC, mode); if (new_fd == -1) { perror("open"); return errno; } ssize_t bytes_read; char buffer[4096]; while ((bytes_read = read(old_fd, buffer, sizeof(buffer))) > 0) { if (write(new_fd, buffer, bytes_read) != bytes_read) { perror("write"); close(new_fd); unlink(new_path); return errno; } } close(new_fd); close(old_fd); if (unlink(get_filepath_from_fd(old_fd)) != 0) { // 删除旧文件 perror("unlink"); return errno; } if (rename(new_path, get_filepath_from_fd(old_fd)) != 0) { // 重命名新文件 perror("rename"); return errno; } return 0; } ``` 这种方法虽然可行,但存在性能开销和潜在的数据一致性问题[^2]。 --- #### 方法四:模拟 `fchmod` 行为 如果以上方法均不适合,也可以尝试封装自己的函数来模仿 `fchmod` 的行为。例如,结合前两种方法的优点,优先尝试通过 `/proc/self/fd` 解析路径,失败后再采取备份策略。 ```c int custom_fchmod(int fd, mode_t mode) { char *filepath = get_filepath_from_fd(fd); if (filepath && access(filepath, F_OK) == 0) { return chmod(filepath, mode); } else { fprintf(stderr, "Failed to resolve filepath from fd %d\n", fd); return -1; } } ``` 这种方式提供了更高的灵活性,但也增加了复杂性和错误处理难度[^4]。 --- ### 总结 每种方法都有各自的适用范围和局限性: - **方法一**适合有明确路径的情况; - **方法二**适用于 Linux 平台下的动态路径解析; - **方法三**是一种通用但低效的选择; - **方法四**则综合了多种思路,适合作为兜底解决方案。 在实际开发中应根据具体环境选择合适的实现方式,并充分测试以确保可靠性。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值