Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (9.5k points)
edited by

I run Ubuntu 12.04 to perform the task to read data from the disk which is not cached. 

Below is the command I run to drop cache: 

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

Though I run from my admin account, it still requires my password. I want to run it in a batch script without the input password. So, I run the command below: 

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

When I run the command  “sudo -l “ on my admin account, I get the line below:

(ALL) NOPASSWD: /home/peter/dropCache

When I run dropCache script, I still get asked for the password: 

./dropCache

[sudo] password for peter: 

Can anyone tell me how to run dropCache script as a non root user? 

1 Answer

0 votes
by (19.7k points)

Try to change the owner of the compile file(of any program)  to root and set the setuid bit. 

 

Here’s my code implementation below: 

#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;

}

Interested in Linux? Check out this Linux Certification by Intellipaat. 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 14, 2020 in AWS by Justin (7k points)

Browse Categories

...