Bash Aliases and Tricks for Sysadmin
A collection of bash aliases and functions that simplify daily work on a Linux server. Add them to /root/.bashrc or /root/.bash_aliases.
Basic setup
nano ~/.bashrc
Add at the end of the file:
# Load separate alias file (if you prefer to keep it separate)
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
To apply immediately without logout:
source ~/.bashrc
System and monitoring aliases
# Quick system info
alias meminfo='free -h'
alias cpuinfo='lscpu'
alias diskinfo='lsblk -f'
# Processes sorted by CPU and RAM
alias pscpu='ps aux --sort=-%cpu | head -15'
alias psram='ps aux --sort=-%mem | head -15'
# Top in batch mode (single snapshot)
alias topcpu='top -bn1 | head -25'
# Active network connections
alias netstat='ss -tulpn'
alias ports='ss -tulpn | grep LISTEN'
# CPU temperature (requires lm-sensors)
alias temp='sensors 2>/dev/null || cat /sys/class/thermal/thermal_zone*/temp'
# Readable uptime
alias uptime='uptime -p'
systemd and service aliases
# systemctl shortcuts
alias sc='systemctl'
alias scs='systemctl status'
alias scr='systemctl restart'
alias scst='systemctl start'
alias scsp='systemctl stop'
alias sce='systemctl enable'
alias scd='systemctl disable'
alias scl='systemctl list-units --type=service --state=running'
# Journal log live (last 50 lines + follow)
alias jf='journalctl -f'
alias jfe='journalctl -f -p err' # errors only
alias jfu='journalctl -u' # usage: jfu nginx
# Example: scs nginx, scr php8.2-fpm
Nginx, Apache, PHP aliases
# Nginx
alias ng-test='nginx -t'
alias ng-reload='systemctl reload nginx'
alias ng-restart='systemctl restart nginx'
alias ng-log='tail -f /var/log/nginx/error.log'
alias ng-access='tail -f /var/log/nginx/access.log'
# Apache
alias ap-test='apache2ctl configtest'
alias ap-reload='systemctl reload apache2'
# PHP-FPM (adjust version)
alias php-restart='systemctl restart php8.2-fpm'
alias php-log='tail -f /var/log/php8.2-fpm.log'
# MySQL/MariaDB
alias mysql-root='mysql -u root -p'
alias mysql-size="mysql -u root -e \"SELECT table_schema AS 'DB', ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS 'Size (MB)' FROM information_schema.tables GROUP BY table_schema;\""
Docker aliases
# Docker shortcuts
alias dk='docker'
alias dkps='docker ps'
alias dkpsa='docker ps -a'
alias dkimg='docker images'
alias dklogs='docker logs -f' # usage: dklogs container_name
alias dkexec='docker exec -it' # usage: dkexec container_name bash
alias dkstop='docker stop'
alias dkrm='docker rm'
alias dkrmi='docker rmi'
# docker-compose shortcuts
alias dc='docker compose'
alias dcu='docker compose up -d'
alias dcd='docker compose down'
alias dcr='docker compose restart'
alias dcl='docker compose logs -f'
alias dcp='docker compose pull'
# Docker cleanup
alias dk-clean='docker system prune -f'
alias dk-cleanall='docker system prune -a -f --volumes'
Advanced bash functions
Functions can accept arguments, unlike aliases.
# Search in system logs
logsearch() {
journalctl --no-pager | grep -i "$1" | tail -50
}
# Usage: logsearch "failed"
# Quick file backup (adds timestamp)
bak() {
cp "$1" "${1}.bak.$(date +%Y%m%d_%H%M%S)"
echo "Backup created: ${1}.bak.$(date +%Y%m%d_%H%M%S)"
}
# Usage: bak /etc/nginx/nginx.conf
# Extract any archive
extract() {
case "$1" in
*.tar.bz2) tar xjf "$1" ;;
*.tar.gz) tar xzf "$1" ;;
*.tar.xz) tar xJf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.gz) gunzip "$1" ;;
*.zip) unzip "$1" ;;
*.7z) 7z x "$1" ;;
*.tar) tar xf "$1" ;;
*) echo "Format not recognized: $1" ;;
esac
}
# Usage: extract archive.tar.gz
# Connect SSH and remember in command history
sshlog() {
echo "Connecting to $1..."
ssh "$@"
}
# Show open ports with the process using them
whoisport() {
ss -tlpn | grep ":$1 "
}
# Usage: whoisport 80
# Find recently modified files
recent() {
find "${1:-.}" -maxdepth "${2:-3}" -type f -newer /tmp/recent_mark 2>/dev/null || \
find "${1:-.}" -maxdepth "${2:-3}" -type f -mmin -60
}
# Check if a service uses too much CPU
topper() {
watch -n 1 "ps aux --sort=-%cpu | head -10"
}
Custom bash prompt
Add to ~/.bashrc for a colored prompt that shows user, host, path and last command exit code:
# Colors
RED='\[\033[0;31m\]'
GREEN='\[\033[0;32m\]'
YELLOW='\[\033[1;33m\]'
BLUE='\[\033[0;34m\]'
CYAN='\[\033[0;36m\]'
RESET='\[\033[0m\]'
# Prompt: [exitcode] user@host:path$
PS1='$(RET=$?; if [ $RET != 0 ]; then echo "'${RED}'[$RET] "; fi)'
PS1+="${GREEN}\u${RESET}@${YELLOW}\h${RESET}:${CYAN}\w${RESET}\$ "
Complete .bashrc file (ready to use)
# Apply all at once:
curl -s https://raw.githubusercontent.com/... > ~/.bash_aliases
source ~/.bash_aliases
Aliases defined in .bashrc or .bash_aliases are available only in interactive bash sessions. To make them available in scripts or cron, add them directly to the script or use source ~/.bashrc at the beginning.
Most used aliases: summary
| Alias | Equivalent to |
|---|---|
ll | ls -lah |
ports | ss -tulpn | grep LISTEN |
pscpu | ps aux --sort=-%cpu | head -15 |
scs nginx | systemctl status nginx |
scr nginx | systemctl restart nginx |
jfu nginx | journalctl -u nginx -f |
dkps | docker ps |
dc up -d | docker compose up -d |
ng-log | tail -f /var/log/nginx/error.log |
Articoli correlati
Server Reboot
How to properly restart your VPS or VDS, both via SSH and from the control panel
Resource Monitoring
How to check CPU, RAM, disk and network on your server in real time
Disk Management
How to check disk space, find large files and free up disk space
