Posted on 02/07/2019 8:42:12 AM PST by ShadowAce
This tutorial shows you how to list users in Linux. Youll also learn to list only the logged users.
Today different Operating Systems have the capability to use multiple users, each one with their settings and custom configurations to make things easier for administrators and operators to work in together on the same system.
Linux on the other hand is very strong on this matter as it allows multiple users to work at the same time on the system in an independent way. It can even allow a single user to open several sessions even from different locations in order to work on the system.
Here are some hints & tricks to handle users in Linux.
Probably, the very first thing to know is how to know what users are in my system. There are several ways you can obtain the list of users in Linux.
This command allows sysops to list the the users that are locally stored in the system. It will give the listing in structured way as:
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
johndoe:x:1000:1000:John Doe,,,:/home/helder:/bin/bash
davmail:x:127:65534::/var/lib/davmail:/usr/sbin/nologin
statd:x:128:65534::/var/lib/nfs:/usr/sbin/nologin
/etc/passwd (END)
The structure in the above output goes as:
x
represents password is stored)
Why so many users? Which ones are real?
The list shows a lot more users than you expected because it lists all the system users too.
Now if you want to distinguish the normal users from the system users, you can refer to the User ID (UID) number.
Generally, a normal user has UID greater or equal to 1000. This gives you a hint that the user with UID >=1000 is a normal user and users with UID <1000 are system users.
This command will give you a similar output as less /etc/passwd however, this one actually queries the GNU Name Service Switch functionality configuration file (located at /etc/nsswitch.conf).
This conf includes passwd, so thats why it will display very similar but if you use LDAP for authentication it will include that as well.
If you just want to list all the usernames without any additional information, you can use the compgen command with -u option.
compgen -u
The output would be like this: compgen -u
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
www-data
backup
list
irc
gnats
nobody
systemd-network
systemd-resolve
syslog
messagebus
_apt
uuidd
avahi-autoipd
usbmux
dnsmasq
rtkit
cups-pk-helper
speech-dispatcher
whoopsie
kernoops
saned
pulse
avahi
colord
hplip
geoclue
gnome-initial-setup
gdm
abhishek
Tip
You can use compgen command with -c option to list all the commands available to you. This is helpful when you are not the admin on a Linux system and dont have sudo access.
You just saw three ways to view users in Linux. Here are a few tips that would help you while dealing with the users listing.
You already have the compgen command for that but you dont have to remember it all the time.
If we would like to only get a list of the usernames in the system, you can use the awk command or the cut command to filter the output of the other two commands we saw earlier.
cut -d: -f1 /etc/passwd
or
getent passwd | awk -F: '{ print $1}'
Any of these will give us a filtered list of users, showing only the very first column which is username:
root
daemon
bin
sys
sync
games
man
lp
mail
news
johndoe
davmail
statd
This might be useful if you want to know if a particular username already exists in the system:
getent passwd | grep johndoe
johndoe:x:1000:1000:John Doe,,,:/home/johndoe:/bin/bash
If you want to know what users are currently logged into your system, then you need to perform a simple who on your command line and this will immediately list current usernames with an active session to your system
user@system:~$ who
johndoe :0 2019-01-28 21:35 (:0)
harrysmith pts/0 2019-02-01 09:51 (192.168.1.1)
stevejones pts/1 2019-02-02 09:51 (192.168.1.173)
In this case, the listing will give you not only the list of usernames connected but also how they are connected, since when they are connected and from where they are connected.
The very first column will tell you what username is it.
The second column will give you what type of connection it is: if its represented with a :X where X is a number, it means it is using a Graphical User Interface (GUI) or Desktop session such as Gnome, XDE, etc; if it says pts/X where X is a number, it means its a connection made through SSH protocol (command line).
The third column will tell you since when this session has been connected to the server (date and time). The fourth and last column will give you the location from where its connected, if remote it will display the IP from where the connection is made if local (like the GUI) it will display (:X) where X is the number of the session in this case and will match the number in the second column for that row.
As you can see, listing users in Linux is not difficult at all. It consists of simple commands which will output all the information for you, whatever you want to do or obtain of that information is something you need to filter depending on what you want to check on the system.
Lots of reading to find ‘who’.
Thanks.
shadowAce- This is off topic- but perhaps you might know a good answer- I would like to know if there is an easy way to use linux in a sandbox-like environment where at the end of a session everything gets deleted so that there is no possibility any harm being done- I have someone wanting to use my computer for online at home work- which i think is a farce- but just to be completely safe- I would like to use use something that can be deleted and not harm computer in any way-
I wonder if having them do their work from a linux CD might be safe?
Or from a virtual machine where it runs windows 10?
I checked out sandboxes for linux, but they look a bit too complicated for me-
Running in a VM is also a very viable option. It depends on your level of comfort in running each type of technology.
Thanks, I’ll probably have htem run it from a Cd- although runnign from virtual machine i have windows 10 on the vm which is more familiar to them and starts up quicker- hmmm decisions lol
Here is one for your list for those who have SSDs (cheapest way to increase speed):
TRIM helps the SSD avoid performance issues by preparing used data blocks for the overwriting process. You can check for TRIM support on your SSD through the Linux command line, as well as enabling it if it is not turned on by default in your system. Click the search box and type “Terminal.”
How to Know if TRIM Is Working in Linux | Chron.com
https://smallbusiness.chron.com/trim-working-linux-29092.html
Ubuntu Doesn’t TRIM SSDs By Default: Why Not and How To Enable It ...
https://www.howtogeek.com/.../ubuntu-doesnt-trim-ssds-by-default-why-not-and-how...
Dec 3, 2013 - (Android also uses the Linux kernel.) With TRIM enabled, the operating system tells the SSD each time it deletes a file. ... In other words, if you don’t use TRIM, your SSD will slow down over time. That’s why modern operating systems, including Windows 7+, Mac OS X 10.6.8+, and Android 4.3+ use TRIM.
Also,
By default, Mac OS, unlike Windows, doesn’t automatically enable the TRIM command for a self-installed SSD. (If your Mac comes with an SSD, TRIM will already be enabled.) TRIM allows the operating system to actively inform an SSD which blocks of data are no longer in use and can be wiped internally.Jul 20, 2016
Installing an SSD on your Mac? Don’t make this mistake - CNET
https://www.cnet.com/how-to/installing-ssd-on-mac-trim-mistake/
Windows should automatically enable TRIM if you have a modern version of Windows with a modern solid-state drive. If TRIM is disabled, it’s possible that Windows knows something you don’t, and TRIM shouldn’t be enabled for a drive.Jul 12, 2017
How to Check if TRIM Is Enabled for Your SSD (and Enable It if It Isn’t)
https://www.howtogeek.com/.../how-to-check-if-trim-is-enabled-for-your-ssd-and-enabl...
Disclaimer: Opinions posted on Free Republic are those of the individual posters and do not necessarily represent the opinion of Free Republic or its management. All materials posted herein are protected by copyright law and the exemption for fair use of copyrighted works.