9

In command line platforms online, like for instance the one on Codecademy, when I run

for cmd in w who whoami id
do
    echo $cmd
    $cmd
    echo =========================
    echo "             "
done

I get

w                              
00:52:54 up 8 days, 14:10,  0 users,  load average: 3.78, 2.98, 2.69      
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT       
=========================                            

who
=========================                                                 

whoami                          
ccuser                          
=========================                              
  
id
uid=1000(ccuser) gid=1000(ccuser) groups=1000(ccuser)     
=========================                   

Note that only whoami and id output something. When I run the same thing on my computer, I see similar results for all commands.

Why doesn't Codecademy display the user for w and who? What's different about these commands?

whoami
  • 93

1 Answers1

14
  • id reports
    • the current credentials of its own process; or
    • the credentials of a named user, as read out of the system account database.
  • whoami reports the current credentials of its own process.
  • who and w report the active login sessions table from the login database.

BSD doco notes that whoami does a subset of the job of id, and that id renders it obsolete.

A system does not have to have an active login sessions table. On Linux operating systems and on the BSDs, if the table has not been created at bootstrap, or has been deleted since, the system will operate without one. Logging in and out does not implicitly create it on Linux operating systems, moreover.

Furthermore, the table need not be readable by unprivileged users and neither the who nor the w command will report this as an error.

Further reading

JdeBP
  • 68,745