Skip to content

Instantly share code, notes, and snippets.

@stephenchew
Last active October 17, 2023 04:18
Show Gist options
  • Save stephenchew/bf9e5d3b8a427cbf97b21487eb996960 to your computer and use it in GitHub Desktop.
Save stephenchew/bf9e5d3b8a427cbf97b21487eb996960 to your computer and use it in GitHub Desktop.
Linux command cheatsheet

Linux command cheatsheet

Update editor alternative

sudo update-alternatives --config editor

Run docker with sudo without password (for docker group)

sudo visudo

Add the following line in

%docker ALL=(ALL)  NOPASSWD: /usr/bin/dockerd

User / group management

Add user

This command creates a user named harry

sudo useradd -m harry

Optional -m summons the command to create a home directory for the user and allows login

Add group

This command creates a group named porter

sudo groupadd porter

Add user to group

This command adds the user harry to the group porter

sudo usermod -aG porter harry

Check which user-group association

cat /etc/group

Use grep for filtering

Devices

List block devices (see all mounted storage devices)

lsblk -f

Check if a directory is mounted with device

grep -qs '/media/wd' /proc/mounts && echo yes || echo no

-qs flag means suppress and silent, good to be used in if statement

Ports

List all listening (opened) ports

sudo lsof -i -P -n | grep LISTEN
# or
sudo netstat -tulpn | grep LISTEN
# or
sudo ss -tulpn | grep LISTEN
# or
sudo lsof -i:22 # see a specific port such as 22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment