Linux
Specifically using Ubuntu.
SSH
Type exit
to exit out of a SSH session.
# SSH into a server with Raspberry Pi's default user.
ssh pi@<ip_address>
# SSH into a server as a particular user.
ssh <user_name>@<ip_address>
# Output current user's name.
whoami
# View last hour of SSH logs.
sudo journalctl -u ssh --since "1 hour ago"
# Determine shell being used.
ps -p "$$"
# Find process having a process id (PID).
ps -p <pid>
System management
# Reboot/restart.
sudo reboot
sudo shutdown -r now
# Reboot in 5 minutes.
sudo shutdown -r 5
# Shutdown
sudo poweroff
# Get machine and kernel information.
uname -mrs
# Get distribution-specific information.
lsb_release -a
# Get computer name.
hostname
# Get Debian version.
cat /etc/debian_version
Updates
# Get a list of current packages.
sudo apt update
# Get a list of upgradable packages.
apt list --upgradable
apt list --upgradable -a
# Upgrade all packages.
sudo apt upgrade
# Install or update specificed package.
sudo apt install <package-name>
File system
Information
# View disk space.
df
df -h
# Count number of files and folders in home directory.
ls ~ | wc -l
# List block devices. Helps with seeing partition information.
sudo lsblk -f -m
# View the total size of a directory.
sudo du -sh /path/to/directory
# Show by child directory size.
sudo du -h /path/to/directory --max-depth=1
# Show largest directories first.
sudo du -h /path/to/directory --max-depth=1 | sort -hr
# Sort by directory name.
sudo du -h /path/to/directory --max-depth=1 | sort -k2
# Largest directories.
sudo du --separate-dirs -h /path/to/directory | sort -hr | head
sudo du --separate-dirs -h /path/to/directory | sort -hr | head -n 2
# Get all .mp4 files sorted by date and showing date, folder name, file name, and size.
# Excludes ._ and .DS_Store files (macOS).
find . -type f -name "*.mp4" ! -name "._*" ! -name ".DS_Store" -printf "%CY-%Cm-%Cd %CT %h %f %s\n" | sort -n
Navigation
# Print working directory (current directory).
pwd
# By itself, move to the current user's home directory. Tilde is short for home directory.
cd
cd ~
Modification
# Make a new directory.
mkdir name
mkdir name1 name2 name3
# Make a new directory and sub-directory/ies.
mkdir -p path/to/directory
# Move file(s)/directories to an existing directory.
mv file.txt dir1
mv file1.txt file2.txt dir1
mv file1.txt file2.txt new-dir dir1
# Rename a file.
mv file.txt new-name.txt
# Rename a directory.
mv dir-name new-dir-name
# Copy a file
cp path/to/file.txt .
cp file.txt copy-file.txt
# Remove/delete a file.
rm file.txt
# Remove/delete an empty directory.
rmdir path/to/directory
# Remove/delete a directory, even if it has files. (Only macOS?)
rmdir -r path/to/directory
# Works on Ubuntu.
rm -r path/to/directory
File viewing and creation
# Look at a file or multiple files.
cat file.txt
cat file1.txt file2.txt
cat file?.txt
cat file*
# Write to a file.
echo "Some text" > file.txt
echo "Some more text" >> file.txt
# File viewer. Press q to quit.
less file.txt
# List all files in a directory, with full paths, into a text file.
find wwwroot -type f > files.txt
# The above with directory, file name, file date, and size, into a tab-separated file.
find . -type f ! -name "._*" ! -name ".DS_Store" -printf "%h\t%f\t%CY-%Cm-%Cd %CT\t%s\n" > files.tsv
# Get full path to a file.
readlink -f relative/path/to/file
Hidden content
# Make a hidden directory.
mkdir .dir1
# Make a hidden file.
mkdir .hide.txt
# List all hidden files/folders.
ls -a
System information
# View OS information.
cat /etc/os-release
# View system information on Ubuntu, which is displayed on SSH login:
landscape-sysinfo
# System monitoring.
top
# Prettier version of top.
htop
# Memory usage.
free
# Human readable.
free -h
# Show in MBs.
free -m
# IP address
ip a
hostname -I
# Server uptime.
uptime
# View path variable.
echo $PATH
# Find path of binary.
which cd
Package management
# Show manually installed packages.
apt-mark showmanual
apt list --manual-installed
# List installed packages.
apt list --installed
dpkg --list
sudo dpkg --audit
sudo dpkg --get-selections
User management
# Show who's logged in.
w
# Show the last logged in users.
last
# Show the last bad login attempts.
lastb
# Set the password for a user.
passwd <user>
# List user information, including groups.
id <user>
# List all users.
cat /etc/passwd
# List all users across multiple sources.
getent passwd
# Install sudo on Debian or the like (that don't have it by default).
apt update
apt install sudo
# Grant a user sudo.
usermod -aG sudo <username>
# Add the current user to a group (<group-name>).
sudo usermod -aG <group-name> ${USER}
Permissions
# List all users.
compgen -u
getent passwd
# List all groups.
compgen -g
getent group
# List all groups current user is in.
groups
# List all groups a user (username) is in.
groups username
# Add write access to group.
chmod g+w file-or-directory
# Remove write and execute from group.
chmod g-wx file-or-directory
# Remove read, write, and execute from others.
chmod o-rwx file-or-directory
# Users, groups, and others have read, write, and execute.
chmod ugo+rwx file-or-directory
# Grant all users read-only access to a file or directory.
chmod a=r file-or-directory
# View permissions.
ls -l
ls -ld
Service logs
# View logs for a particular service.
sudo journalctl -fu mycustom.service
sudo journalctl -fu mycustom.service --since "1 hour ago"
Copy files
# Copy a file to a directory.
scp file-name.ext <user>@<server>:~/path/to/directory/
# Copy the contents of a folder to a remote folder.
scp -r .\path\to\directory\* <user>@<server>:/path/to/remove/directory
Sudo
# Start a root shell. `exit` when done.
sudo -s
Firewall (ufw)
# Show status of firewall. If active, also lists rules.
sudo ufw status
# Show rules even when ufw is inactive.
sudo ufw show added
# Allow by service.
sudo ufw allow ssh
# Allows 80 and 443.
sudo ufw allow 'Nginx Full'
# Allow port-number.
sudo ufw allow <port-number>
sudo ufw allow 8080