linux – 允许非root用户删除缓存

linux – 允许非root用户删除缓存,第1张

概述我正在一个系统上进行性能测试,我需要确保从磁盘读取数据,并且它不仅仅是缓存(比如早期的测试).我读了 here,我可以使用命令删除缓存 echo 3 | sudo tee /proc/sys/vm/drop_caches 但请注意,即使我的帐户是管理员帐户(登录彼得),它仍然需要我的密码.我希望能够在批处理脚本中运行它而无需输入密码(因为这显然是手动的) 更多research让我进入了sudoer 我正在一个系统上进行性能测试,我需要确保从磁盘读取数据,并且它不仅仅是缓存(比如早期的测试).我读了 here,我可以使用命令删除缓存

echo 3 | sudo tee /proc/sys/vm/drop_caches

但请注意,即使我的帐户是管理员帐户(登录彼得),它仍然需要我的密码.我希望能够在批处理脚本中运行它而无需输入密码(因为这显然是手动的)

更多research让我进入了sudoers文件.我的计划是将上面的命令放入一个名为dropCache的单行脚本中,并编辑sudoers以便我可以在不输入密码的情况下运行它.所以我添加了这条线

ALL ALL=(ALL)nopASSWD:/home/peter/dropCache

在我的sudoers文件的末尾(使用visudo).使用我的管理员帐户,如果我运行

sudo -l

我明白了

(ALL) nopASSWD: /home/peter/dropCache

但是,如果我运行我的dropCache脚本,我仍然会被要求输入密码

./dropCache[sudo] password for peter:

任何有关这方面的帮助将非常感激.我正在运行Ubuntu 12.04

谢谢
彼得

解决方法 我在需要时做的是我编写了一个小程序,将编译文件的所有者更改为root,然后设置setuID位.

这是源代码:

#include <stdlib.h>#include <stdio.h>#include <unistd.h>#include <sys/types.h>extern voID sync(voID);int main(voID) {    if (geteuID() != 0) {        fprintf(stderr,"flush-cache: Not root\n");        exit(EXIT_FAILURE);    }    printf("Flushing page cache,dentrIEs and inodes...\n");    // First: the Traditional three sync calls. Perhaps not needed?    // For security reasons,system("sync") is not a good IDea.    sync();    sync();    sync();    file* f;    f = fopen("/proc/sys/vm/drop_caches","w");    if (f == NulL) {        fprintf(stderr,"flush-cache: Couldn't open /proc/sys/vm/drop_caches\n");        exit(EXIT_FAILURE);    }    if (fprintf(f,"3\n") != 2) {        fprintf(stderr,"flush-cache: Couldn't write 3 to /proc/sys/vm/drop_caches\n");        exit(EXIT_FAILURE);    }    fclose(f);    printf("Done flushing.\n");    return 0;}
总结

以上是内存溢出为你收集整理的linux – 允许非root用户删除缓存全部内容,希望文章能够帮你解决linux – 允许非root用户删除缓存所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1026843.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存