I have written a C program that syncs the time with the time from a GPS reciever. The computer is running Ubuntu 16.04 LTS. The program uses clock_settime(CLOCK_REALTIME, &timespec) to set the time. The program is executed in a bash script, and I cannot use sudo as the script is launched by the use of a desktop file. Anyone has an idea ?

NB: The script is executing multiple programs, that I do not want to execute as root

  • 1
    Is there a reason you are not using an existing NTP solution for this? – Kusalananda Feb 28 '18 at 15:57
  • Yes, the computer is not networked. I have previously used PPS signal and GPS data in connection with the NTP deamon for obtaining a very precise synchronisation, but from my experience it does not work properly if the difference between the system and gps time is large, the ntp daemon rejects the PPS source ! – Morten Skovgaard Feb 28 '18 at 17:41
  • Finally found a way that worked ,, first tried with visudo, not working .. but a less safe way using the following directly in my scipt echo $PASSWORD | sudo -S /path/to/program. Not the best solution, but it works ! – Morten Skovgaard Feb 28 '18 at 18:28

Capabilities can be a solution.

Before running program, you can add a CAP_SYS_TIME capbilities to your program like the next.

sudo setcap CAP_SYS_TIME+ep  /path/to/program

Example in docker

sys@2a2618c145ed:~$ ./a_pr.out
clock_gettime : 1534915500.689625314
clock_gettime : 1534916100.690310388
clock_gettime : 1534915500.690651299
sys@2a2618c145ed:~$ ./test.run
clock_gettime : 1534915504.139181778
Error : clock_settime -1
clock_gettime : 1534915504.139337010
Error : clock_settime -1
clock_gettime : 1534915504.139341290
sys@2a2618c145ed:~$ filecap $(pwd)
file                 capabilities
/jeremy/a_pr.out     sys_time

Addtionally, I'd like to recommend the below page : Allow a specific user or group root access without password to /bin/date

The next can be better. sudo setcap CAP_SYS_TIME+ei /path/to/program

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.