How to Filter Specific Users (e.g., root, nobody) in Linux Top Command


3 views

When monitoring system performance using the top command, you'll often see processes from all users including system accounts like root and nobody. In production environments, filtering out these system processes can help focus on application-specific metrics.

The quickest way to filter users is by pressing u followed by the username when top is running:

1. Launch top:
   top
2. Press 'u'
3. Enter the username to show (or prefix with '!' to exclude)
   !root,nobody

For persistent filtering, use these command-line options:

top -u \!root,\!nobody

Or to show only specific users:

top -u apache,www-data

For frequent use, create a .toprc file in your home directory:

cat > ~/.toprc <<EOF
RCfile for "top with windows"       # shameless braggin'
Id:a, Mode_altscr=0, Mode_irixps=1, Delay_time=3.0, Curwin=0
Def     fieldscur=AEHIOQTWKNMbcdfgjplrsuvyzX
        winflags=30009, sortindx=10, maxtasks=0
        summclr=1, msgsclr=1, headclr=3, taskclr=1
Job     fieldscur=ABcefgjlrstuvyzMKNHIWOPQDX
        winflags=62777, sortindx=0, maxtasks=0
        summclr=6, msgsclr=6, headclr=7, taskclr=6
Mem     fieldscur=ANOPQRSTUVbcdefgjlmyzWHIKX
        winflags=62777, sortindx=13, maxtasks=0
        summclr=5, msgsclr=5, headclr=4, taskclr=5
Usr     fieldscur=ABDECGfhijlopqrstuvyzMKNWX
        winflags=62777, sortindx=4, maxtasks=0
        summclr=3, msgsclr=3, headclr=2, taskclr=3
EOF

For more complex filtering, combine with pgrep:

top -p $(pgrep -u \!root,\!nobody)
  • Escaping special characters: Always escape ! with \ in shells
  • Multiple users: Separate with commas, no spaces
  • UID filtering: Use numerical UIDs when usernames are problematic

Consider these alternatives when top filtering isn't sufficient:

htop -u \!root
atop -u \!nobody
glances --disable-plugin users

When monitoring system processes with top, administrators often need to focus on specific user processes while excluding system accounts like root or nobody. The default view showing all processes can clutter the display with irrelevant system processes.

The quickest way to filter users is using top's interactive mode:

1. Launch top: top
2. Press U (uppercase)
3. Enter the username you want to display
4. For exclusion logic, use !username format

Example to exclude root and nobody:

U
!root,!nobody

For scripting or one-time use, specify users directly:

top -u $(awk -F: '$3 >= 1000 {printf "%s,",$1}' /etc/passwd | sed 's/,$//')

This complex command automatically filters system users (UID < 1000).

Create or modify ~/.toprc:

RCfile for "top with windows"       # shameless bullshit line
Id:a, Mode_altscr=0, Mode_irixps=1, Delay_time=3.0, Curwin=0
Def     fieldscur=AEHIOQTWKNMbcdfgjplrsuvyzX
        winflags=64137, sortindx=3, maxtasks=0
        summclr=6, msgsclr=2, headclr=3, taskclr=2
Job     fieldscur=ABcefgjlrstuvyzMKNHIWOPQDX
        winflags=62777, sortindx=0, maxtasks=0
        summclr=6, msgsclr=6, headclr=7, taskclr=6
Mem     fieldscur=ANOPQRSTUVbcdefgjlmyzWHIKX
        winflags=62777, sortindx=13, maxtasks=0
        summclr=5, msgsclr=5, headclr=4, taskclr=5
Usr     fieldscur=ABDECGfhijlopqrstuvyzMKNWX
        winflags=62777, sortindx=4, maxtasks=0
        summclr=3, msgsclr=3, headclr=2, taskclr=3

For comprehensive monitoring, combine user filtering with other options:

top -u appuser,dbuser -o +%MEM -n 1 -b > process_report.txt

This command:

  • Shows only appuser and dbuser processes
  • Sorts by memory usage
  • Runs once in batch mode
  • Outputs to file

Consider these alternatives when top's filtering isn't sufficient:

# htop with better UI
htop --user=!root,!nobody

# ps with custom formatting
ps -eo user,pid,ppid,%mem,%cpu,cmd --sort=-%cpu | grep -vE 'root|nobody'