Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to run some calculations on a machine that I can only access remotely but also serves as a work station. There are many of them and I want to pick a "free" one. Free means in this case that no one is logged in locally.

My first try was the who command, but for some reason it only lists "selected" users and I can't really find out how they are selected. Next try: ps aux | cut -d " " -f1 | sort | uniq: better showing a bunch of demons but also the local user that was not displayed by who.

My current solution is to go in and do ps aux | grep "gnome-session" which is better but still gives me a lot of junk. Ideally I am looking for something that I can include in my ssh profile that warns me about (active) local users when I log in.


  • Neither who nor w did return the local user. Is this an unexpected behaviour?
  • uptime on the other hand showed me the right amount of users (local and remote minus system users like root)
  • finger is not installed
share|improve this question
I also see that w, who, and users are not returning the local users. This is new and unexpected behaviour on Ubuntu 12.04. I just rebooted some lab machines while (now very annoyed) users were logged on. – user71375 Jun 18 '12 at 8:29

Use w

From w man page:

Show who is logged on and what they are doing.

Output example:

$ w
09:15:10 up 43 min,  2 users,  load average: 0.74, 0.38, 0.24

USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU  WHAT

kucing   tty7     :0               08:32   43:15m 57.73s  0.18s x-session-manager
kucing   pts/0    :0.0             08:48    0.00s  0.24s  0.00s w´
share|improve this answer

Is the finger command installed, or if not, could it be? This should give a listing of all users who are logged in and where from (i.e. another machine, or directly onto the machine), and should also tell you how long that user has been idle.

See the ubuntu finger manpage for more information.

share|improve this answer
depending on the network setup, you can also use finger to query remote machines without even logging in (though modern firewalls might have that feature blocked). – ImaginaryRobots May 31 '12 at 21:57
finger is not installed. There is a safe_finger that fails because it apparently relies on finger. – sebastiangeiger Jun 1 '12 at 5:51
@sebastiangeiger safe_finger is present as part of the tcpd package regardless of whether or not the finger package (which provides the finger command, which safe_finger is just a wrapper for) is actually installed. It's advisable to use safe_finger when contacting finger servers on remote machines (especially ones you don't yourself administer and ensure don't cause the client-side problems safe-finger protects against). If you're using finger locally, it shouldn't be necessary to use safe-finger. – Eliah Kagan Jun 1 '12 at 6:52

I would go with:

who | cut -d' ' -f1 | sort | uniq

This will show a list of real users. If the list is empty - machine is in logged out state, waiting for someone to log in.
ps aux will show also some system users, which you probably do not want to see.

share|improve this answer
But you probably need to think of another scenario, when people is leaving their working places by hitting Ctrl+Alt+L, rather than logging out. I'm doing that! :) – Andrejs Cainikovs May 31 '12 at 17:41
Strangely enough who does not show me the local user. I know that he was actively working on that machine - he complained that I was clogging up the workstation. Is that who misbehaving? – sebastiangeiger May 31 '12 at 18:44
@sebastiangeiger As Andrejs Cainikovs suggests, do you want to count a user as logged in locally if they have locked their workstation? You might not, as they are not right there interacting with the machine. But you might, because they still may be running programs and performing calculations (or whatever they do) that take CPU resources, which could take resources away from, and/or have their resources taken away by, your work. As a similar matter, do you want to consider users to be "logged in locally" if they are logged in locally on a virtual console, or only graphically? – Eliah Kagan Jun 1 '12 at 6:12
@EliahKagan Ideally I want users running a local X session. It would be nice to know about users that locked the workstation or that are logged in via ssh, but my priority is to find out whether there is a person locally (and actively) working on it right now – sebastiangeiger Jun 1 '12 at 8:14
@sebastiangeiger What about users logged in locally via text-based virtual console? – Eliah Kagan Jun 1 '12 at 8:52

Consider just setting the highest niceness for your calculations. Should avoid hogging the resources for any other users that may be logged in.

nice -n 19 your_calculation_command
share|improve this answer
Creative, I like. – sebastiangeiger Jun 1 '12 at 5:32
@sebastiangeiger Of course, it would be even better to periodically (say, every minute with a user crontab) check if there is a local user logged in, and if there is, decrease the niceness, and if there isn't, increase it (so as to get work done even when there are other remote users, or a system process, using a lot of CPU). Of course, to accomplish that, it would be necessary to have a reliable way to know if a local user is logged in. (You'd also have to overcome the problem that non-root users cannot renice a process down to a lower niceness, even the one it started at.) – Eliah Kagan Jun 1 '12 at 6:09


it looks through /var/log/wtmp and displays a log of the last users logged on, including those currently logged on.

share|improve this answer
Looks very promising, I'll have to check on Monday when there are people working at the office. – sebastiangeiger Jun 2 '12 at 18:04

My answer to list every user logged, in unique way.

w | tail -n +3 | cut -d " " -f1 | sort | uniq
share|improve this answer

A quick and dirty attempt:

ps au --no-heading | cut -d " " -f1 | uniq
share|improve this answer

I like this one:

for i in $(ls /dev/pts/*);do echo "TTY $i - $(ps e -t $i|grep -oP "(?<=(RUSER\=))[a-z]*"|uniq)";done ; echo -e '\n+++\n';  for i in $(pgrep ssh);do pstree -paul $i;done
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.