Skip to content

Linux Commands

System Information

# OS information
uname -a
cat /etc/issue
cat /etc/*-release
lsb_release -a

# Hostname
hostname
hostnamectl

# Kernel version
uname -r

# Architecture
uname -m
arch

# CPU information
cat /proc/cpuinfo
lscpu

# Memory information
cat /proc/meminfo
free -h

# Disk usage
df -h
lsblk

# Environment variables
env
printenv
echo $PATH

User Information

# Current user
whoami
id

# All users
cat /etc/passwd
cat /etc/passwd | cut -d: -f1

# Groups
groups
cat /etc/group

# Logged in users
who
w
last
lastlog

# User privileges
sudo -l

# Password hashes
cat /etc/shadow (requires root)

Network Information

# IP configuration
ip addr
ifconfig
hostname -I

# Routing table
ip route
route -n
netstat -rn

# Network connections
netstat -antup
ss -tulpn
lsof -i

# ARP table
ip neigh
arp -a

# DNS
cat /etc/resolv.conf
cat /etc/hosts

# Open ports
netstat -tuln
ss -tuln
lsof -i -P -n

Process Management

# List processes
ps aux
ps -ef
top
htop

# Process tree
pstree
ps auxf

# Find process
pgrep <name>
pidof <name>

# Kill process
kill <PID>
kill -9 <PID>
killall <name>
pkill <name>

# Background/Foreground
command &  # Run in background
jobs       # List background jobs
fg         # Bring to foreground
bg         # Resume in background

File Operations

# Find files
find / -name file.txt 2>/dev/null
find / -type f -name "*.conf" 2>/dev/null
find / -user username 2>/dev/null
find / -perm -4000 2>/dev/null  # SUID
find / -perm -2000 2>/dev/null  # SGID
find / -writable -type f 2>/dev/null

# Locate
locate file.txt
updatedb  # Update locate database

# Which
which python
whereis python

# Search in files
grep -r "password" /etc/ 2>/dev/null
grep -ri "password" . 2>/dev/null  # Case insensitive

# File permissions
chmod 755 file
chmod +x file
chown user:group file

# File information
file file.txt
stat file.txt
ls -la file.txt

Archive Operations

# Tar
tar -czf archive.tar.gz directory/
tar -xzf archive.tar.gz
tar -tzf archive.tar.gz  # List contents

# Zip
zip archive.zip file1 file2
zip -r archive.zip directory/
unzip archive.zip
unzip -l archive.zip  # List contents

# 7z
7z a archive.7z directory/
7z x archive.7z
7z l archive.7z  # List contents

# Gunzip
gzip file.txt
gunzip file.txt.gz

Text Processing

# Cat
cat file.txt
cat file1.txt file2.txt > combined.txt

# Head/Tail
head -n 10 file.txt
tail -n 10 file.txt
tail -f /var/log/syslog  # Follow

# Grep
grep "pattern" file.txt
grep -r "pattern" /path/
grep -i "pattern" file.txt  # Case insensitive
grep -v "pattern" file.txt  # Invert match

# Sed
sed 's/old/new/g' file.txt
sed -i 's/old/new/g' file.txt  # In-place

# Awk
awk '{print $1}' file.txt
awk -F: '{print $1}' /etc/passwd

# Cut
cut -d: -f1 /etc/passwd
cut -c1-10 file.txt

# Sort/Uniq
sort file.txt
sort -u file.txt  # Unique
uniq file.txt

Service Management

# Systemd
systemctl status service
systemctl start service
systemctl stop service
systemctl restart service
systemctl enable service
systemctl disable service
systemctl list-units

# SysV
service service_name status
service service_name start
service service_name stop
/etc/init.d/service_name status

Cron Jobs

# User crontab
crontab -l
crontab -e

# System cron
cat /etc/crontab
ls -la /etc/cron.*
cat /etc/cron.d/*

# Format: minute hour day month weekday command
# 0 2 * * * /path/to/script.sh

Package Management

Debian/Ubuntu (APT)

apt update
apt upgrade
apt install package
apt remove package
apt search package
apt list --installed
dpkg -l
dpkg -i package.deb

Red Hat/CentOS (YUM/DNF)

yum update
yum install package
yum remove package
yum search package
yum list installed
rpm -qa
rpm -i package.rpm

Firewall

iptables

# List rules
iptables -L
iptables -L -n -v

# Allow port
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# Block IP
iptables -A INPUT -s 192.168.1.100 -j DROP

# Save rules
iptables-save > /etc/iptables/rules.v4

UFW

ufw status
ufw enable
ufw disable
ufw allow 22
ufw allow 80/tcp
ufw deny 23
ufw delete allow 80

SSH

# Connect
ssh user@host

# With key
ssh -i key.pem user@host

# Port forwarding
ssh -L 8080:localhost:80 user@host  # Local
ssh -R 8080:localhost:80 user@host  # Remote
ssh -D 8080 user@host  # Dynamic (SOCKS)

# Copy SSH key
ssh-copy-id user@host

# Config
cat ~/.ssh/config

Download

# Wget
wget http://example.com/file.txt
wget -O output.txt http://example.com/file.txt
wget -c http://example.com/file.txt  # Resume

# Curl
curl http://example.com/file.txt
curl -O http://example.com/file.txt
curl http://example.com/file.txt -o output.txt
curl -L http://example.com/file.txt  # Follow redirects

Miscellaneous

# History
history
history -c  # Clear

# Screen
screen
screen -ls
screen -r <id>
screen -X -S <id> quit

# Tmux
tmux
tmux ls
tmux attach -t <id>
tmux kill-session -t <id>

# Watch
watch -n 1 'command'

# Calculate hash
md5sum file.txt
sha1sum file.txt
sha256sum file.txt

# Base64
echo "text" | base64
echo "dGV4dAo=" | base64 -d

# Hex dump
xxd file.txt
hexdump -C file.txt

# Strings
strings binary

# strace
strace command
strace -p <PID>

# nc listeners
nc -lvnp 4444
nc -lvnp 4444 > received_file
nc -lvnp 4444 -e /bin/bash