Score:2

trying to script ntp client settings

jo flag

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.

muru avatar
us flag
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
Scott Johnstone avatar
jo flag
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.
muru avatar
us flag
Are you sure the file contents were updated? None of the other commands are likely to give _that_ "permission denied" error
Scott Johnstone avatar
jo flag
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.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.