加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 以非root用户身份以编程方式删除Linux缓存

发布时间:2020-12-16 05:03:20 所属栏目:百科 来源:网络整理
导读:出于测试目的,我可以通过在procfs下写入 Linux中的drop_caches文件来删除缓存内存.我只能以root身份执行此操作.这是在嵌入式Linux上,因此没有sudo. sync; echo 3 /proc/sys/vm/drop_caches 我可以通过在帖子中执行某些操作来以编程方式写入文件 – How to pr
出于测试目的,我可以通过在procfs下写入 Linux中的drop_caches文件来删除缓存内存.我只能以root身份执行此操作.这是在嵌入式Linux上,因此没有sudo.
sync; echo 3 > /proc/sys/vm/drop_caches

我可以通过在帖子中执行某些操作来以编程方式写入文件 – > How to programmatically clear the filesystem memory cache in C++ on a Linux system?

sync();
std::ofstream ofs("/proc/sys/vm/drop_caches");
ofs << "3" << std::endl;

挑战是希望以非root用户身份运行应用程序时执行此操作.在重新启动时,权限如下所示:

# cd /proc/sys/vm/
# ls -lrt drop_caches 
-rw-r--r--    1 root     root             0 Feb 13 19:50 drop_caches

你似乎无法改变这些权限 – 即使是root:

# chmod 777 drop_caches 
chmod: drop_caches: Operation not permitted
# chown user:user drop_caches 
chown: drop_caches: Operation not permitted

我怎样才能在Linux上实现这一目标?是否可以更改procfs文件的权限?如有必要,我可以完全自定义我的内核.谢谢 –

解决方法

您可以创建一个辅助可执行文件(非常小心,它很危险),任何用户都可以使用root权限运行它.

这称为setuid.

出于安全原因,您无法设置shell脚本.

从wiki中提取如何使用它:

The setuid and setgid bits are normally set with the command chmod by
setting the high-order octal digit to 4 (for setuid) or 2 (for
setgid). “chmod 6711 file” will set both the setuid and setgid bits
(2+4=6)

更新

正如@rici指出的那样,您仍然需要具有执行权限才能执行此过程,因此您可以删除其他人的执行权限并将其保留在组中.因此,只有谁是该组的成员才能执行它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读