I am trying to create a custom log file that logs all ssh connections in a specific format. However, I need the log file to only be writtable and readable by root. So, the script needs to be run with root. This is my script :
now=$(date)
ip=`echo $SSH_CONNECTION | cut -d " " -f 1`
echo "User $USER just logged in from $ip at [$now]" >> /home/user/ssh_log.txt
Now, I run the script in /etc/ssh/sshrc, but this file only executes as the user and not root. Is there a way to execute the script as root when a user ssh in the server?