A more elegant way to solve this problem is to read from log files that are already written and cannot be changed by the user. No one could say it better than Bjรถrn Malmanger in his answer :
I would not trust the user to provide you with information. As root you take it; -)
The last command is a good way to do this, which is great because it accurately displays all logins: graphical, console, and SSH.
last
A less elegant but safe way is to make grep on /var/log/auth.log. On my Gnome / Ubuntu system, I can use this to track graphical inputs:
grep "session opened for user USERNAME"
For each type of login, you need to find the right template for your computer: graphic, console, and SSH. This is cumbersome, but you may need to do this if you need information that goes further than last .
To directly answer your question:
You can modify the script as follows to get the username
#!bin/bash printf "$(date) $HOSTNAME booted!\n" >> /home/$(whoami)/boot.log
And add this line to / etc / profile
. /path/to/script.sh
This is not safe because the user will be able to edit their own log.
source share