LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Code runs as root? (https://www.linuxquestions.org/questions/programming-9/code-runs-as-root-331488/)

neo_in_matrix 06-08-2005 05:25 AM

Code runs as root?
 
I want to write a small program that:

1. Accepts a date string (06/08/2005);
2. Sets system date.

However, setting system date/time requries root privilege.

So I'd like to know if it possible to do it using C code? I do not want su.

Mega Man X 06-08-2005 06:00 AM

I don't think so. You may, however, give permission to all users to execute /bin/date, but that would severely put your system in jeopardy. For example, every user capable of changing the date, would be able to delay cron jobs, etc (I think cron uses system date, doesn't it?).

Either way, if you are doing it to learn only, then you can do that. Otherwise is just to call "su" within your program or by changing /bin/date to allow execution by user.

I could be mistaken though, as I often am :D

Hko 06-08-2005 11:46 AM

Quote:

I do not want su.
Try "sudo" instead. You can tell sudo to let specific user run specific programs as root, without asking the user(s) for a password.

neo_in_matrix 06-08-2005 12:02 PM

Could not get any idea out the man sudo. I tried to edit /etc/sudoers, but don't know how to.

I am running as user 'neo', if I want this user to run date without typing root password, what should I do?

Hko 06-08-2005 05:46 PM

First, you need to know the full path of the "date" program. It's probably "/bin/date. But to be sure, check with:
Code:

which date
.

Then to enable user "neo" to run "date" witthout being asked for a password, just put one line in /etc/sudoers
Code:

neo ALL = NOPASSWD: /bin/date
Then, for example, when user "neo" wants to run "date" as "root" to set the date-time to 19 march, 1:00 am:
Code:

sudo date 03190100
This will also work when this line is in a script.

neo_in_matrix 06-08-2005 09:38 PM

That's great! Thanks!


All times are GMT -5. The time now is 06:15 AM.