Tech Blog – Scripts – Linux Cheat Sheet

General Info
Hardware Info
Performance Info
User & Group Management
Directories & File Manipulation

All commands tested on Amazon Linux 2


General Info

# Show hostname
hostname

# Show IP Address
hostname -I

# Show who you are logged in as
whoami

# Show active user sessions
w

# Show uptime
uptime

# Show reboot history
last reboot

# Show date
date

# Show calendar
cal

Hardware Info

# Show messages in kernal ring buffer
dmesg

# Show CPU details
cat /proc/cpuinfo

# Show memory details
cat /proc/meminfo

# Show memory usage
free -h

Performance Info

# Show top processes (ctrl+c to cancel out)
top

# Show all open files
lsof

# Show all files open by a user
lsof -u user

User & Group Management

# Show last users who logged in
last

# Show users logged in now
users

# Show users logged in now, more info
who

# Show users logged in and commands being run
w

# Show all  users configured on the system
awk -F: '{print $1}' /etc/passwd

# Show all users configured on the system, more info
getent passwd
getent passwd | grep ec2-user

# Create new user
adduser pete 'password1'

# Delete a user (+ delete homefolders)
sudo userdel pete

# Show all groups
groups

# Show all groups, more info
getent group

# Create new group
sudo groupadd it

# Add user to group
sudo usermod -aG it pete

# Show users within a group
getent group it

# Show primary/secondary groups
id ec2-user

# Delete a group
sudo groupdel it

Directories & File Manipulation

# Show all files in current directory (detailed)tou
ls -al

# Show present working directory
pwd

# Create a new directory
sudo mkdir home/ec2-user stuff

# Delete above directory (-rf for without prompt)
sudo rm -r home/ec2-user stuff

# Create a new file
sudo touch home/ec2-user/stuff/newfile.txt

# Delete above file
sudo rm home/ec2-user/stuff/newfile.txt

# Copy a file
sudo cp home/ec2-user/stuff/file.txt home/ec2-user/stuff/copied_file.txt

# Move or rename a file
sudo mv home/ec2-user/stuff/file.txt home/ec2-user/more_stuff/file.txt