Unix Toolbox
Unix Toolbox
Cheat sheet de commandes Unix/Linux/BSD.
Système
Informations sur le noyau, le matériel, la charge et les utilisateurs.
Noyau & version
uname -a # Kernel version (and BSD version)
lsb_release -a # Full release info of any LSB distribution
cat /etc/debian_version # Debian version
cat /etc/redhat-release # RedHat/Fedora version
uptime # How long the system has been running + load
hostname # System's host name
hostname -i # IP address of the host (Linux only)Matériel
dmesg # Detected hardware and boot messages
lspci -tv # Show PCI devices
lsusb -tv # Show USB devices
cat /proc/cpuinfo # CPU model
cat /proc/meminfo # Hardware memory
free -m # Used and free memory (-m for MB)
dmidecode # DMI/SMBIOS: hw info from the BIOSCharge & statistiques
top # Display and update the top cpu processes
mpstat 1 # Processors related statistics
vmstat 2 # Virtual memory statistics (2 s intervals)
iostat 2 # I/O statistics (2 s intervals)
tail -n 500 /var/log/messages # Last 500 kernel/syslog messagesUtilisateurs
id # Active user id with login and group
who # Show who is logged on
last # Show last logins on the system
useradd -c "Colin B" -g admin -m colin # Add user colin (Linux/Solaris)
usermod -a -G <group> <user> # Add existing user to group (Debian)
userdel colin # Delete user colin
groupadd admin # Add group "admin"Limites
ulimit -a # Check shell limits
ulimit -n 10240 # Open files limit (valid within the shell only)
sysctl -a # View all system limits
sysctl fs.file-max # View max open files limit
sysctl fs.file-max=102400 # Change max open files limit
# /etc/security/limits.conf: per-user/process limits
# asterisk hard nofile 409600Processus
Lister, surveiller, terminer et prioriser les processus.
Liste & signaux
ps -auxef # All processes with full listing
ps axjf # Process tree
top # Live view of processes
pgrep -l ssh # Find the PID of ssh processes
jobs # List background jobs of the shellTuer un processus
kill -9 1234 # Force kill process with PID 1234
kill -1 1234 # Reload config (SIGHUP)
killall -9 httpd # Kill all httpd processes
pkill -9 ssh # Kill all ssh processes
pkill -TERM -u colin # Terminate all processes of user colinPriorité
nice -n 5 command # Run command with lower priority
renice +5 1234 # Lower priority of PID 1234
renice -5 -u colin # Higher priority for all colin's processesSystème de fichiers
Disques, montage, recherche et permissions.
Disque
df -h # Free disk space (human readable)
du -sh /path # Size of a directory
du -sh * # Size of each item in current dir
lsblk # List block devices
fdisk -l # List partitions
mount | column -t # Mounted file systemsMontage
mount /dev/sda1 /mnt # Mount a partition
umount /mnt # Unmount
mount -t nfs srv:/share /mnt # Mount an NFS share
# /etc/fstab: persistent mounts
# /dev/sda1 /mnt ext3 defaults 0 1Recherche
find / -name "*.conf" # Find files by name
find . -type f -size +10M # Files larger than 10 MB
find . -mtime -1 # Files modified in last 24 h
find . -exec grep -l "foo" {} \; # Files containing "foo"
locate pattern # Fast find via indexed dbPermissions
chmod 755 file # rwxr-xr-x
chmod u+x file # Add execute for owner
chown colin:admin file # Change owner and group
chgrp admin file # Change group
chattr +i file # Make file immutable
umask 022 # Default permissions for new filesRéseau
Interfaces, routage, pare-feu et débogage réseau.
Interfaces & IP
ip addr # Show all interfaces and addresses
ip link show # Show link state
ifconfig # Legacy: show interfaces
hostname -I # All IP addresses of the host
ethtool eth0 # Link speed and parameters of eth0Routage & connexions
ip route # Show routing table
ip route add default via 192.168.1.1 # Add default gateway
ss -tulnp # Listening TCP/UDP ports with PID
netstat -an # All connections (legacy)
route -n # Routing table (legacy)Pare-feu
iptables -L -n -v # List all rules
iptables -A INPUT -p tcp --dport 22 -j ACCEPT # Allow SSH
iptables -A INPUT -j DROP # Drop everything else
ufw status # Ubuntu/Debian firewall status
ufw allow 22/tcp # Allow SSH (UFW)Débogage
ping -c 3 example.com # Test reachability
traceroute example.com # Trace the route
dig example.com # DNS lookup
dig +short example.com MX # MX records only
nslookup example.com # DNS lookup (legacy)
tcpdump -i eth0 -nn port 80 # Capture HTTP traffic on eth0SSH & SCP
Connexions sécurisées, transferts de fichiers et tunnels.
Connexion SSH
ssh user@host # Connect to host as user
ssh -p 2222 user@host # Connect on a custom port
ssh-keygen -t ed25519 # Generate an ed25519 key pair
ssh-copy-id user@host # Install public key on remote host
ssh-agent bash # Start an agent
ssh-add ~/.ssh/id_ed25519 # Add key to the agentCopie de fichiers
scp file.txt user@host:/tmp/ # Copy local file to remote
scp user@host:/tmp/file.txt . # Copy remote file to local
scp -r dir user@host:/tmp/ # Copy a whole directory
sftp user@host # Interactive secure FTP sessionTunnels
ssh -L 8080:localhost:80 user@host # Local port forward
ssh -R 8080:localhost:80 user@host # Remote port forward
ssh -D 1080 user@host # SOCKS proxy (dynamic forward)
ssh -N -f -L 5901:localhost:5901 host # Background tunnel, no commandRsync
Synchronisation efficace de fichiers et répertoires.
Synchronisation
rsync -a /src/ /dest/ # Archive mode (recursive, preserve)
rsync -a /src/ user@host:/dest/ # Sync to a remote host
rsync -a user@host:/src/ /dest/ # Pull from a remote host
rsync -a --delete /src/ /dest/ # Delete files in dest not in srcOptions utiles
rsync -a -v /src/ /dest/ # Verbose output
rsync -a -z /src/ user@host:/dest/ # Compress during transfer
rsync -a -n /src/ /dest/ # Dry run (show what would happen)
rsync -a --progress /src/ /dest/ # Show progress
rsync -a -e "ssh -p 2222" /src/ user@host:/dest/ # Custom SSH portSudo
Exécuter des commandes avec les privilèges d'un autre utilisateur.
Utilisation
sudo command # Run command as root
sudo -u colin command # Run as user colin
sudo -s # Start a root shell
sudo -k # Invalidate the cached credentialsConfiguration
visudo # Edit /etc/sudoers safely
# /etc/sudoers entries:
# colin ALL=(ALL) ALL # colin may run anything
# %admin ALL=(ALL) ALL # members of admin group
# colin ALL=(ALL) NOPASSWD: ALL # without passwordShells
Configuration Bash, alias et raccourcis clavier.
Configuration Bash
# ~/.bashrc is sourced for interactive shells
export PATH=$PATH:/sbin:/usr/sbin
export EDITOR=vim
export PS1='\u@\h:\w\$ ' # Prompt: user@host:cwd$
alias ll='ls -la'
source ~/.bashrc # Reload the configRaccourcis clavier
Ctrl+A / Ctrl+E # Move to start / end of line
Ctrl+W # Delete word before cursor
Ctrl+U / Ctrl+K # Delete to start / end of line
Ctrl+R # Reverse search in history
Alt+. # Insert last argument of previous commandScripting
Exemples de scripts Bash : variables, boucles et conditions.
Variables & paramètres
#!/bin/bash
var="hello" # Assign a variable (no spaces around =)
echo $var # Use the variable
echo $0 # Script name
echo $1 $2 # First and second arguments
echo $# # Number of arguments
echo "$@" # All arguments as a stringConditions
if [ -f /etc/hostname ]; then
echo "file exists"
elif [ -d /etc ]; then
echo "is a directory"
else
echo "not found"
fi
# Test operators: -f file, -d dir, -z empty string, -n non-empty
# Numbers: -eq, -ne, -lt, -le, -gt, -geBoucles
for f in *.txt; do
echo "processing $f"
done
for i in $(seq 1 5); do
echo "number $i"
done
while read line; do
echo "$line"
done < input.txt