2

Im writing or trying to write a script that will add some NTP servers to 22.04 server.

Setntpclient()
{
sudo cp /etc/systemd/timesyncd.conf /etc/systemd/timesyncd.conf.bk_`date +%Y%m%d%H%M`
echo
sudo chmod 664 /etc/systemd/timesyncd.conf #setting to rw for user and group
sudo cat > /etc/systemd/timesyncd.conf <<EOF
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it under the
#  terms of the GNU Lesser General Public License as published by the Free
#  Software Foundation; either version 2.1 of the License, or (at your option)
#  any later version.
#
# Entries in this file show the compile time defaults. Local configuration
# should be created by either modifying this file, or by creating "drop-ins" in
# the timesyncd.conf.d/ subdirectory. The latter is generally recommended.
# Defaults can be restored by simply deleting this file and all drop-ins.
#
# See timesyncd.conf(5) for details
[Time]
NTP=ntp0.domain.com ntp1.domain.com
FallbackNTP=IpAdd1 IpAdd2
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048
EOF
sudo chmod 644 /etc/systemd/timesyncd.conf
sudo systemctl unmask systemd-timedated.service
sudo systemctl restart systemd-timesyncd.service
sudo timedatctl set-timezone "Europe/London"
echo timedatectl status
}
Setntpclient

Now when I go to restart the systemd-timesyncd.service I get an error.

Permission denied
Failed to restart systemd-timesyncd.service: Unit systemd-timesyncd.service is masked.

So I added the "sudo systemctl unmask systemd-timedated.service" to the script.

Now from reading what I am guessing is when you run a shell script it runs it from /tmp and this maybe causing the issue? If so what is the way around this?


Update to clarify what I'm seeing

ntp.conf is being updated but when it comes time to restart the timesynd service its fails with the error.

  • 4
    It's not clear what the issue is. Are you talking about the "permission denied" error? If so, is the script being run as a normal user? Then sudo cat > file ... won't work - the redirection is done as that user, not by sudo. You need sudo tee file instead – muru Sep 01 '23 at 10:21
  • Hi muru, Still learning linux, But this is my logic. changed the file permissions on the ntp.conf to user read-write and group read-write. sudo cat and push that into the ntp.conf. this has worked for what ever reason. but getting permission denied on the restart of the timesyncd service. I gave you the full script so you can see what i was trying to do. Rather than just giving you the error message. – Scott Johnstone Sep 01 '23 at 10:29
  • Are you sure the file contents were updated? None of the other commands are likely to give that "permission denied" error – muru Sep 01 '23 at 10:43
  • Hi muru, rebuild my ubuntu instance and ran the script. yeah you were right getting permission denied on the timesyncd.conf. I have moved to tee thank you very much for pointing me in the right direction. Appreciated. – Scott Johnstone Sep 01 '23 at 11:45

0 Answers0