Skip to content

Instantly share code, notes, and snippets.

@shmalex
Last active November 7, 2023 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shmalex/6e771a869ad802474d01a0719bcb1484 to your computer and use it in GitHub Desktop.
Save shmalex/6e771a869ad802474d01a0719bcb1484 to your computer and use it in GitHub Desktop.
Ubuntu User Management

List users

https://www.godaddy.com/help/remove-a-linux-user-19158

cat /etc/passwd

Each row represents a user and the fields separated by the colon (:) has the following meaning:

  • User name
  • Password, x means that a password is set for the user
  • User ID (UID)
  • User's group ID (GID)
  • Full name, room number, phone number etc (optional)
  • Home directory of the user
  • Default login shell for the user

grep -E '^UID_MIN|^UID_MAX' /etc/login.defs

root@learnubuntu:~# grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
UID_MIN			 1000
UID_MAX			60000

Use the cut command to use the : as column separator and then display the first column.

cat /etc/passwd | cut -d: -f1

You can do the same with the awk command:

cat /etc/passwd | awk -F: '{print $1}'

Use getent command to list users

getent passwd

You can list only the regular users the same way:

getent passwd | cut -d: -f1

What if you just want to know if a user exists or not?

getent passwd : grep user_name

List normal users only (for scripting)

eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)} | cut -d: -f1

List currently logged in users

who or users

Manage users

https://www.howtoforge.com/how-to-install-teamspeak-server-on-debian-11/ Create user sudo adduser ts3 --home /opt/teamspeak --shell /bin/bash --disabled-password sudo chown -R ts3:ts3 /opt/teamspeak

Delete user

sudo su -
userdel USERNAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment