On a shared VM (or any Linux/Unix-like system), you can easily check which user owns a running process. The primary tools for this are ps, top, and htop.
Here's how you can do it, depending on whether you know the Process ID (PID) or the process name:
Method 1: Using ps (Process Status)
ps is a fundamental command for displaying information about current processes.
A. If you know the Process ID (PID):
If you have a PID (e.g., from an error message or another tool), this is the most direct way:
ps -p <PID> -o user,pid,cmd
- Replace
<PID> with the actual process ID.
-o user,pid,cmd specifies the output format: user (owner), pid (process ID), cmd (command).
Example:
If you want to check PID 12345:
ps -p 12345 -o user,pid,cmd
Output might look like:
USER PID COMMAND
john_doe 12345 /usr/bin/python3 my_script.py
This tells you john_doe owns PID 12345 which is running my_script.py.
B. If you know the Process Name (or part of it):
You can search for processes by name and then see their owners.
Option 1: Using ps aux | grep (Common)
This is a very common way to search for processes.
ps aux | grep <process_name> | grep -v grep
ps aux: Lists all running processes for all users, in a user-friendly format.
a: show processes for all users
u: display user/owner and other detailed information
x: show processes without a controlling TTY
grep <process_name>: Filters the output to show only lines containing your process name.
grep -v grep: Filters out the grep command itself from the results, as grep is also a process.
Example:
To find who is running nginx:
ps aux | grep nginx | grep -v grep
Output might look like:
root 1234 0.0 0.1 123456 5432 ? Ss Mar01 0:01 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 1235 0.0 0.0 123456 2100 ? S Mar01 0:00 nginx: worker process
www-data 1236 0.0 0.0 123456 2100 ? S Mar01 0:00 nginx: worker process
Here, the master process of nginx is owned by root, and the worker processes are owned by www-data.
Option 2: Using pgrep and ps (More precise)
pgrep finds PIDs based on name, then you can feed those PIDs to ps.
ps -f -p $(pgrep <process_name>)
pgrep <process_name>: Returns the PIDs of processes matching the name.
$(...): Command substitution, meaning the output of pgrep is used as arguments for ps.
ps -f: Shows full-format listing, which includes the UID (user ID) or USER column.
Example:
ps -f -p $(pgrep firefox)
Output might look like:
UID PID PPID C STIME TTY TIME CMD
john_doe 5432 1234 5 10:30 ? 00:01:23 /usr/lib/firefox/firefox
C. To see all processes and their owners (Overview):
ps aux
or
ps -ef
Both commands list all processes. The ps aux output has a USER column directly. The ps -ef output has a UID column, which ps usually translates to a username for readability. This is useful for getting a general overview of what's running and by whom.
Method 2: Using top or htop (Interactive Process Viewer)
top and htop provide a real-time, interactive view of running processes, including their owners.
A. Using top:
Simply type top in your terminal:
top
You'll see a dynamic list of processes. The first column (or usually one of the first few) is USER (or UID), showing the owner of each process.
- You can press
u then type a username to filter by user.
- You can press
f and then select USER to ensure the USER column is visible and at the position you prefer.
- Press
q to quit top.
B. Using htop (Recommended, if available):
htop is an enhanced version of top that is more user-friendly and feature-rich. If it's not installed, you might need to install it (e.g., sudo apt install htop on Debian/Ubuntu, sudo yum install htop on CentOS/RHEL).
htop
htop immediately shows a USER column prominently.
- You can easily scroll, search (
/), filter (F4), and sort processes.
- Press
F6 to sort by a column, then select USER to group processes by owner.
- Press
q or F10 to quit htop.
Method 3: Inspecting /proc (Advanced)
The /proc filesystem provides a direct interface to kernel process information. Each process has a directory named after its PID (e.g., /proc/12345). The owner of this directory is the owner of the process.
ls -ld /proc/<PID>
Example:
ls -ld /proc/12345
Output might look like:
dr-xr-xr-x 9 john_doe john_doe 0 Mar 1 10:00 /proc/12345
The john_doe directly after the permissions (dr-xr-xr-x) indicates the owner of the directory, which is the process owner.
Summary & Recommendation:
- For a quick check if you have the PID or a specific name:
ps -p <PID> -o user,pid,cmd or ps aux | grep <name> | grep -v grep.
- For an interactive, real-time view:
htop (preferred) or top.
- For scripting or more direct system info:
pgrep combined with ps, or ls -ld /proc/<PID>.
Remember that as a regular user, you generally have permission to see all processes running on the system, but you can only debug or send signals (like kill) to processes that you own. If you need to manage processes owned by others, you'd typically need sudo privileges.