Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
Wat het doet
Zo schrijf je het
logname [OPTION]Opties 2
Meest gebruikt
Niets past bij dit filter.
Voorbeelden 10
$ als gewone gebruiker# als root (of met sudo)
1Show the login name
logname studentThe user who logged in.
2After switching to root
logname still shows the real person.
3Use the real user in a root script
echo "$(date) $(logname) changed the firewall" >> /var/log/admin-changes.logBetter audit text than "root".
4Compare with SUDO_USER
sudo sh -c 'echo $SUDO_USER; logname' student studentSUDO_USER is set only by sudo; logname also works after su.
5Without a terminal
ssh server2 logname logname: no login nameA remote command without a terminal has no login record.
6In cron
Cron jobs have no login name; use
id -unthere.7Give a file back to the real user
chown "$(logname)": /tmp/report.txtAfter a root script, the user can open the file.
8Check the login records
who am i student pts/0 2026-10-25 09:02 (192.168.122.1)logname reads the same data.
9Script that needs a person
logname >/dev/null 2>&1 || echo "no interactive login"Exit code 1 when there is no login name.
10Show the home folder of the real user
Useful in an install script run with sudo.
Niets past bij dit filter.
Exitcodes
0 :: Done. 1 :: No login name.