Who & Where: whoami, uptime

Quick commands to see who you are, who else is logged in, and how the system is doing.

Syntaxwhoami uptime free -h

A handful of tiny commands answer "what is the state of this machine?"

Identity

  • whoami — the current user name.
  • hostname — the machine's name.
  • who — who is logged in right now.
  • w — who is logged in and what they are doing.

Health

  • uptime — how long the system has been running, plus load averages.
  • free -h — memory used and free, human-readable.
  • date — the current date and time.

Example

Example · bash
whoami
# alice

uptime
# 09:42:11 up 5 days,  3:11,  2 users,  load average: 0.15, 0.10, 0.09

free -h
#               total   used   free
# Mem:           7.7Gi  2.1Gi  4.3Gi

When to use it

  • A script validates it is running as root with 'if [ "$(whoami)" != "root" ]' before attempting system-level operations.
  • A developer runs 'id' after switching users with 'su' to confirm the new UID, GID, and group memberships are correct.
  • A security audit script logs 'whoami' and 'hostname' at the top of every run to create a clear audit trail of who ran what on which server.

More examples

Identify current user

Shows whoami for a quick username check and id for the full identity including all group memberships.

Example · bash
whoami
# alice

# Full identity: uid, gid, and all groups
id
# uid=1001(alice) gid=1001(alice) groups=1001(alice),27(sudo),998(docker)

Check user in a script

Uses whoami inside a conditional to guard a script that requires root privileges.

Example · bash
#!/bin/bash
if [ "$(whoami)" != "root" ]; then
  echo "Error: this script must run as root" >&2
  exit 1
fi
echo "Root check passed, continuing..."

Log identity at script start

Combines whoami, hostname, and date to write a standard audit header at the top of every automated script.

Example · bash
#!/bin/bash
echo "=== Job started ==="
echo "User:    $(whoami)"
echo "Host:    $(hostname)"
echo "Date:    $(date -Iseconds)"
echo "========================"

Discussion

  • Be the first to comment on this lesson.