Skip to content
All Posts

Basic Linux User, System, and Package Commands

A compact command reference for Linux users, permissions, system information, package management, and Vim.

Logged-in users and groups

Show the current user

Terminal window
$ whoami

Or:

Terminal window
$ id -un

Show the current user and TTY

Terminal window
$ who am i

Show the current user’s identity and groups

Terminal window
$ id

Show command history

Terminal window
$ history

Show users who have logged in before

The last command displays a particular user’s login history. Without arguments, it displays the history for all users. By default, the information comes from /var/log/wtmp.

Terminal window
$ last username

Switch users

Terminal window
$ su username

Add or change a user’s password

Terminal window
$ sudo passwd username

Enter the new password when prompted to set the password for username.

File permissions

Change read and write permissions

chmod [-cfvR] [--help] [--version] mode file1 file2

mode is a permission string in the form ugoa...:

  • u is the file owner, g is users in the owner’s group, o is everyone else, and a means all three.
  • + adds permission, - removes it, and = sets the exact permissions.
  • r means readable, w writable, x executable, and X executable only when the file is a directory or already has an execute bit.
  • -R applies the change recursively.

For example, add execute permission to a file:

Terminal window
$ sudo chmod +x file

Change a file’s owner or group

  • -R recursively changes the owner of a directory and all files and subdirectories beneath it.
  • -v displays the work performed by chown.

For example, change the owner and group of dirname from root to bico:

Terminal window
$ sudo chown -R bico:bico dirname

Package management

Ubuntu has a useful official Chinese package-management guide, which I strongly recommend. Additional references cover tools including Yum:

Reference 1
Reference 2
Reference 3

Vim

Force-save a file opened without root privileges

:w !sudo tee %

Originally published on SegmentFault.