w
Overview
The w
command displays information about currently logged-in users and their activities. It shows more detailed information than who
, including system load and what each user is doing.
Syntax
w [options] [user]
Common Options
Option | Description |
---|---|
-h |
Don’t print header |
-u |
Ignore username in process |
-s |
Short format |
-f |
Toggle printing from field |
-o |
Old style output |
-i |
Display IP instead of hostname |
Key Use Cases
- Monitor user activity
- System load analysis
- Security auditing
- Performance monitoring
- Session management
Examples with Explanations
Example 1: Basic Usage
w
Shows system load and all logged-in users with their activities
Example 2: Specific User
w username
Shows information for specific user only
Example 3: Short Format
w -s
Displays condensed output without JCPU and PCPU
Understanding Output
Header information: - Current time - System uptime - Number of logged-in users - Load averages (1, 5, 15 minutes)
User columns: - USER: Username - TTY: Terminal - FROM: Remote hostname/IP - LOGIN@: Login time - IDLE: Idle time - JCPU: CPU time used by all processes - PCPU: CPU time used by current process - WHAT: Current command
Load Average Interpretation
Load averages represent: - 1 min: Recent system load - 5 min: Medium-term load - 15 min: Long-term load
Values relative to CPU cores: - 1.0 = 100% utilization on single-core system - 2.0 = 100% utilization on dual-core system
Common Usage Patterns
Quick system overview:
w | head -1
Find idle users:
w | awk '$5 ~ /[0-9]+days/ {print $1, $5}'
Monitor specific activity:
w | grep -v idle
Advanced Usage
No header output:
w -h
Show IP addresses:
w -i
Old-style format:
w -o
Performance Analysis
Information useful for: - System load monitoring - User activity tracking - Resource utilization - Performance bottleneck identification - Capacity planning
Best Practices
- Regular system monitoring
- Identify resource-heavy users
- Monitor for unusual activity
- Track system performance trends
- Use for capacity planning
System Monitoring
Load average alerts:
LOAD=$(w | head -1 | awk '{print $10}' | cut -d, -f1) if (( $(echo "$LOAD > 2.0" | bc -l) )); then echo "High system load: $LOAD" fi
Idle user detection:
w | awk '$5 ~ /days/ {print "Idle user:", $1, "for", $5}'
Security Applications
Monitor unauthorized access:
w | grep -v "$(whoami)" | tail -n +2
Track remote connections:
w | awk '$3 !~ /^-/ {print $1, $3}'
Scripting Examples
System status report:
#!/bin/bash echo "=== System Status ===" w | head -1 echo "=== Active Users ===" w -h | wc -l
Performance monitoring:
while true; do LOAD=$(w | head -1 | awk '{print $12}' | cut -d, -f1) echo "$(date): Load average: $LOAD" sleep 60 done
Integration Examples
Alert system:
HIGH_LOAD=$(w | head -1 | awk '{print $12}' | cut -d, -f1) if (( $(echo "$HIGH_LOAD > 5.0" | bc -l) )); then mail -s "High Load Alert" admin@domain.com < /dev/null fi
User activity log:
echo "$(date): $(w -h | wc -l) active users" >> activity.log w -h >> activity.log
Output Parsing
Extract load average:
w | head -1 | awk '{print $(NF-2)}' | cut -d, -f1
Get active user count:
w -h | wc -l
Find users by activity:
w | awk '$NF !~ /^-/ {print $1, $NF}'
Troubleshooting
- High load investigation
- Idle session cleanup
- Resource usage analysis
- Network connectivity issues
- User activity verification
Automation Examples
Scheduled monitoring:
# Crontab entry */5 * * * * w | head -1 >> /var/log/system-load.log
Threshold alerting:
LOAD_THRESHOLD=3.0 CURRENT_LOAD=$(w | head -1 | awk '{print $12}' | cut -d, -f1) (( $(echo "$CURRENT_LOAD > $LOAD_THRESHOLD" | bc -l) )) && alert_admin