I'm trying to automate some tasks on first-boot running Ubuntu on Raspberry Pi. I have a Systemd service that runs once and kills itself. As part of it, I'm trying to update the config on sshd_config and have tried every possible thing I could think of and search on google but in vain. Hopefully, someone can pitch in here with more experience dealing with this stuff.
# disable password login
echo "First Boot - disabling ssh password login"
sed -i 's/PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config
Systemctl and Syslog don't show any errors in the execution. If I run the above command on the command line it behaves as expected.
Other things I have tried
Attempt 1: Assuming permission errors due to in-place sed file creation. I have routed the output to a temp file on printing the contents it looks right but the actual location i.e. /etc/ssh/sshd_config has no changes
TFILE=`mktemp --tmpdir tfile.XXXXX`
trap `rm -f $TFILE` 0 1 2 3 15
sed 's/PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config > $TFILE
cat $TFILE > /etc/ssh/sshd_config
Attempt 2: Read somewhere that /etc/ssh/sshd_config is a symlink to file in /usr and get copied over and hence executing first line copies it to /etc and changes on top
sed -i '' /etc/ssh/sshd_config
sed -i 's/PasswordAuthentication.*/PasswordAuthentication no/' /etc/ssh/sshd_config
Updated 23/02:
Service file
[Unit]
Description=First boot script
ConditionPathExists=/first_boot.sh
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/first_boot.sh
StandardOutput=journal+console
[Install]
WantedBy=multi-user.target