With the commands in this guide, you can manage a
Cloud VPS,
Offshore VPS,
or
dedicated server
without relying entirely on a graphical control panel.
Commands executed as
root or through sudo can modify the entire server. Confirm the command, path, filename, and backup status before deleting files, changing permissions recursively, modifying firewall rules, or restarting production services.Command categories covered
Server and OS information
Directory navigation
Files and directories
Viewing and editing files
Searching files and text
Permissions and ownership
Users and groups
Package management
Services and systemd
Processes and resources
Disk and storage
Networking and DNS
Firewall commands
Logs and troubleshooting
Transfers and backups
Archives and compression
Cron jobs
1. SSH connection commands
SSH, or Secure Shell, creates an encrypted connection between your computer and a remote Linux server. Most Linux VPS and dedicated server administration begins with the ssh command.
Connect using the default SSH port
ssh username@server_ip
Example:
ssh root@203.0.113.10
Connect using a custom SSH port
ssh -p 2222 username@server_ip
Connect using a private key
ssh -i ~/.ssh/server_key username@server_ip
Create a modern SSH key pair
ssh-keygen -t ed25519 -a 100
Store the private key securely and protect it with a strong passphrase. Never send a private SSH key through email, chat, or a support ticket.
Copy your public key to the server
ssh-copy-id username@server_ip
For a custom SSH port:
ssh-copy-id -p 2222 username@server_ip
Test the SSH connection with detailed output
ssh -vvv username@server_ip
Verbose output helps diagnose authentication failures, key problems, connection timeouts, and SSH negotiation errors.
Keep an idle SSH connection alive
ssh -o ServerAliveInterval=60 username@server_ip
Run one command remotely
ssh username@server_ip "uptime"
Close the SSH session
exit
Before disabling password authentication or direct root access, confirm that key-based authentication works in a second terminal window. Incorrect SSH changes can lock you out of the server.
2. Check server, operating system, and kernel information
Show the hostname
hostname
Show detailed hostname information
hostnamectl
Change the server hostname
sudo hostnamectl set-hostname server.example.com
Also review /etc/hosts after changing a hostname so local hostname resolution remains correct.
Show the Linux distribution and version
cat /etc/os-release
Show kernel and architecture information
uname -a
uname -r
uname -m
Show CPU information
lscpu
Show memory information
free -h
Show server uptime and load averages
uptime
Show the current date and timezone
date
timedatectl
3. Navigate the Linux filesystem
Display the current directory
pwd
List files and directories
ls
Detailed list with hidden files and readable sizes:
ls -lah
Sort by modification time:
ls -lath
Change directory
cd /path/to/directory
Go to your home directory:
cd ~
Go up one directory:
cd ..
Return to the previous directory:
cd -
Display a directory tree
tree -L 2 /var/www
The tree package may need to be installed first.
4. Create, copy, move, and delete files
Create an empty file
touch filename.txt
Create a directory
mkdir directory_name
Create nested directories:
mkdir -p /var/www/example.com/public
Copy a file
cp source.txt destination.txt
Copy a directory recursively while preserving attributes:
cp -a source_directory/ destination_directory/
Move or rename a file
mv old_name.txt new_name.txt
Move a file into another directory:
mv file.txt /path/to/destination/
Delete a file interactively
rm -i filename.txt
Remove an empty directory
rmdir directory_name
Commands such as
rm -rf can permanently remove entire directories without confirmation. Verify the full path with pwd and ls, and confirm that a usable backup or snapshot exists before deleting production data.Create a symbolic link
ln -s /actual/path /path/to/link
Inspect a file type
file filename
5. View and edit text files
Display the complete file
cat filename.txt
Read a long file one screen at a time
less filename.txt
Inside less, press / to search, n for the next match, and q to quit.
Show the first lines
head -n 20 filename.txt
Show the last lines
tail -n 50 filename.txt
Follow a file as new lines are added
tail -f /var/log/application.log
Start with the latest 100 lines and continue following:
tail -n 100 -f /var/log/application.log
Edit with Nano
nano filename.txt
Edit with Vim
vim filename.txt
Write output to a file as root
echo "configuration=value" | sudo tee /etc/example.conf
Append instead of overwriting:
echo "new line" | sudo tee -a /etc/example.conf
Compare two files
diff -u old.conf new.conf
6. Search for files, directories, and text
Find a file by name
find /path/to/search -type f -name "filename.conf"
Find files case-insensitively
find /var/www -type f -iname "*.php"
Find large files
find /var -type f -size +500M -printf '%s %p\n' 2>/dev/null | sort -nr | head
Find files modified within one day
find /var/www -type f -mtime -1
Search for text inside a file
grep "search text" filename.txt
Search recursively and include line numbers
grep -Rni "search text" /path/to/directory
Search using an extended regular expression
grep -E "error|failed|critical" application.log
Exclude comments and blank lines
grep -Ev '^[[:space:]]*(#|$)' /etc/ssh/sshd_config
Locate a command
command -v nginx
which php
Search command history
history | grep systemctl
You can also press Ctrl+R in Bash and begin typing part of a previous command.
7. Linux permissions and ownership
View permissions and ownership
ls -l filename
Change file permissions
chmod 644 filename
Typical permissions:
644for normal public files755for directories600for sensitive private files700for private directories or scripts accessible only by the owner
Make a script executable
chmod +x script.sh
Change owner and group
sudo chown user:group filename
Change ownership recursively
sudo chown -R user:group /specific/directory
Do not run recursive
chmod or chown commands against /, /etc, /usr, or an entire hosting account unless you fully understand the required ownership model. Incorrect ownership can break websites, services, SSH access, and control panels.Inspect permissions on every part of a path
namei -l /var/www/example.com/public/index.php
8. Manage Linux users and groups
Show the current user
whoami
Show user and group IDs
id username
Show logged-in users
w
who
Create a user on Debian or Ubuntu
sudo adduser username
Create a user on AlmaLinux, Rocky Linux, or RHEL
sudo useradd -m username
sudo passwd username
Give sudo access on Debian or Ubuntu
sudo usermod -aG sudo username
Give administrative access on AlmaLinux or Rocky Linux
sudo usermod -aG wheel username
Change a password
sudo passwd username
Lock and unlock a user account
sudo passwd -l username
sudo passwd -u username
Safely edit sudo rules
sudo visudo
9. Install and update Linux packages
Debian and Ubuntu package commands
Refresh package metadata:
sudo apt update
Install available upgrades:
sudo apt upgrade
Install a package:
sudo apt install package_name
Remove a package:
sudo apt remove package_name
Search for a package:
apt search package_name
AlmaLinux, Rocky Linux, and RHEL package commands
Check for updates:
sudo dnf check-update
Install updates:
sudo dnf upgrade
Install a package:
sudo dnf install package_name
Remove a package:
sudo dnf remove package_name
Search for a package:
dnf search package_name
Determine which package installed a file
Debian or Ubuntu:
dpkg -S /path/to/file
AlmaLinux, Rocky Linux, or RHEL:
rpm -qf /path/to/file
Review pending updates, confirm backups, and schedule a maintenance window before performing major package or kernel upgrades on an active production server.
10. Manage services with systemd
Check service status
sudo systemctl status nginx
Start, stop, or restart a service
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
Reload configuration without a complete restart
sudo systemctl reload nginx
Enable a service at boot
sudo systemctl enable nginx
Enable and start immediately:
sudo systemctl enable --now nginx
Disable a service at boot
sudo systemctl disable nginx
Check whether a service is active or enabled
systemctl is-active nginx
systemctl is-enabled nginx
List failed services
systemctl --failed
Validate configuration before restarting
Nginx:
sudo nginx -t
Apache on Debian or Ubuntu:
sudo apache2ctl configtest
Apache on AlmaLinux or Rocky Linux:
sudo httpd -t
OpenSSH:
sudo sshd -t
Always test configuration syntax before restarting a web server, SSH daemon, database service, or firewall.
11. Monitor processes, CPU, and memory
Interactive process monitoring
top
htop
htop may need to be installed separately.
List all processes
ps aux
Search for a specific process
ps aux | grep nginx
A cleaner alternative:
pgrep -a nginx
Show the highest CPU-consuming processes
ps aux --sort=-%cpu | head
Show the highest memory-consuming processes
ps aux --sort=-%mem | head
Send a normal termination signal
kill PID
Forcefully terminate a process only when necessary
kill -9 PID
Use a normal kill first. Signal 9 prevents the application from completing cleanup or writing pending data.
Inspect memory and system activity
vmstat 1 10
Inspect disk I/O activity
iostat -xz 1 5
The iostat command is commonly provided by the sysstat package.
12. Check disk space, inodes, and storage devices
Show filesystem disk usage
df -hT
Show inode usage
df -ih
A filesystem can report “No space left on device” even with free storage if all available inodes have been consumed by many small files.
Check the size of one directory
du -sh /path/to/directory
Show the largest items in a directory
du -xh --max-depth=1 /var | sort -h
Display block devices and filesystems
lsblk -f
List mounted filesystems
findmnt
Check current mounts
mount | column -t
Check swap usage
swapon --show
13. Linux networking and DNS commands
Show IP addresses and interfaces
ip address show
Short form:
ip a
Show the routing table
ip route
Show the route used to reach an IP
ip route get 1.1.1.1
Test basic connectivity
ping -c 4 1.1.1.1
A failed ping does not always mean the host is offline because ICMP traffic may be filtered.
Trace the network path
traceroute example.com
Run an interactive route quality test
mtr example.com
Generate a report suitable for troubleshooting:
mtr -rwzc 100 example.com
Show listening ports and services
sudo ss -lntup
Find the process using a port
sudo lsof -i :80
sudo ss -lntp 'sport = :443'
Test whether a remote TCP port is reachable
nc -vz example.com 443
Query DNS records
dig example.com A
Return only the resolved value:
dig example.com A +short
Check nameservers:
dig example.com NS +short
Check mail exchangers:
dig example.com MX +short
Check reverse DNS:
dig -x 203.0.113.10 +short
Inspect HTTP response headers
curl -I https://example.com
Follow redirects and show the final response
curl -IL https://example.com
Test a website against a specific server IP
curl -I --resolve example.com:443:203.0.113.10 https://example.com/
This is useful before DNS changes or while troubleshooting a reverse proxy, migration, or virtual host.
Display the server’s public IP
curl -4 https://icanhazip.com
14. Firewall commands
Confirm which SSH port is active and allow that port before enabling or reloading a firewall. Keep your existing SSH session open while testing a second connection.
UFW on Ubuntu or Debian
Show firewall status:
sudo ufw status verbose
Allow standard SSH:
sudo ufw allow OpenSSH
Allow a custom SSH port:
sudo ufw allow 2222/tcp
Allow HTTP and HTTPS:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
Enable the firewall only after confirming the rules:
sudo ufw enable
firewalld on AlmaLinux or Rocky Linux
Show active rules:
sudo firewall-cmd --list-all
Allow HTTP and HTTPS permanently:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
Allow a custom SSH port:
sudo firewall-cmd --permanent --add-port=2222/tcp
Reload after confirming all required rules:
sudo firewall-cmd --reload
View the active nftables ruleset
sudo nft list ruleset
Avoid flushing an active firewall remotely unless you have console or out-of-band access and understand how the firewall is managed.
15. Read logs and troubleshoot failed services
Show recent system errors
sudo journalctl -p err -b
Show logs for one service
sudo journalctl -u nginx
Show recent service logs and follow new entries
sudo journalctl -u nginx -n 100 -f
Show logs from the current boot
sudo journalctl -b
Show logs from a specific period
sudo journalctl --since "2026-07-20 14:00" --until "2026-07-20 15:00"
Inspect kernel messages
sudo dmesg -T | tail -n 100
Authentication logs
Debian or Ubuntu:
sudo tail -n 100 /var/log/auth.log
AlmaLinux or Rocky Linux:
sudo tail -n 100 /var/log/secure
Nginx logs
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
Apache logs on Debian or Ubuntu
sudo tail -f /var/log/apache2/access.log
sudo tail -f /var/log/apache2/error.log
Apache logs on AlmaLinux or Rocky Linux
sudo tail -f /var/log/httpd/access_log
sudo tail -f /var/log/httpd/error_log
Search compressed and active logs
zgrep -i "error" /var/log/application.log*
Check recent login history
last -a | head -n 30
Check failed login attempts
sudo lastb | head -n 30
16. Transfer and synchronize files
Copy a local file to a remote server with SCP
scp file.tar.gz username@server_ip:/remote/path/
Download a remote file
scp username@server_ip:/remote/path/file.tar.gz ./
Use SCP with a custom SSH port
scp -P 2222 file.tar.gz username@server_ip:/remote/path/
Notice that SCP uses uppercase -P, while SSH uses lowercase -p.
Synchronize a directory with rsync
rsync -avh --progress source_directory/ username@server_ip:/remote/path/
Use rsync through a custom SSH port
rsync -avh --progress -e "ssh -p 2222" source_directory/ username@server_ip:/remote/path/
Preview an rsync operation
rsync -avhn source_directory/ destination_directory/
The -n option performs a dry run without changing files. Use it before synchronization commands involving deletion or production directories.
Download a file with curl
curl -fLO https://example.com/file.tar.gz
Download a file with wget
wget https://example.com/file.tar.gz
Only download installation scripts or software from trusted sources. Review scripts before running them with elevated privileges.
17. Create and extract compressed archives
Create a gzip-compressed tar archive
tar -czf backup.tar.gz directory_name/
List archive contents before extracting
tar -tzf backup.tar.gz
Extract a tar.gz archive
tar -xzf backup.tar.gz
Extract into a specific directory
tar -xzf backup.tar.gz -C /destination/path/
Create a ZIP archive
zip -r backup.zip directory_name/
Extract a ZIP archive
unzip backup.zip -d /destination/path/
Calculate a SHA-256 checksum
sha256sum backup.tar.gz
Compare checksums before and after a transfer to verify file integrity.
18. Schedule commands with cron
Edit the current user’s cron jobs
crontab -e
List the current user’s cron jobs
crontab -l
Edit root’s cron jobs
sudo crontab -e
Run a script every day at 2:30 AM
30 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
Run a command every five minutes
*/5 * * * * /usr/local/bin/task.sh >> /var/log/task.log 2>&1
Use full executable paths inside cron jobs because cron usually runs with a limited environment. Redirect standard output and errors to a log so failed jobs can be diagnosed.
19. Shell shortcuts and command combinations
Run a second command only if the first succeeds
command_one && command_two
Run a second command only if the first fails
command_one || command_two
Send one command’s output into another
ps aux | grep php
Redirect output into a file
command > output.txt
Append output:
command >> output.txt
Redirect both standard output and errors:
command > output.log 2>&1
Run a command in the background
long_running_command &
List and manage shell jobs
jobs
fg %1
bg %1
Keep a command running after logout
nohup command > command.log 2>&1 &
For long interactive maintenance sessions, consider using tmux or screen instead.
Start a tmux session
tmux new -s maintenance
Reattach later:
tmux attach -t maintenance
20. Common server troubleshooting workflows
Website is not responding
- Check whether the web service is active.
- Validate the web server configuration.
- Review listening ports.
- Inspect error logs.
- Test the website locally and externally.
sudo systemctl status nginx
sudo nginx -t
sudo ss -lntp | grep -E ':80|:443'
sudo tail -n 100 /var/log/nginx/error.log
curl -I http://127.0.0.1
curl -IL https://example.com
Server is slow
uptime
free -h
top
ps aux --sort=-%cpu | head
ps aux --sort=-%mem | head
df -hT
df -ih
vmstat 1 10
iostat -xz 1 5
Disk is full
df -hT
df -ih
sudo du -xhd1 / | sort -h
sudo du -xhd1 /var | sort -h
sudo find /var -type f -size +500M -printf '%s %p\n' 2>/dev/null | sort -nr | head -n 20
Investigate before deleting anything. Common causes include oversized logs, old backups, application caches, database files, temporary files, and abandoned website archives.
SSH connection is refused
sudo systemctl status ssh
sudo systemctl status sshd
sudo sshd -t
sudo ss -lntp | grep ssh
sudo journalctl -u ssh -n 100
sudo journalctl -u sshd -n 100
The service is normally named ssh on Debian or Ubuntu and sshd on AlmaLinux, Rocky Linux, and RHEL.
DNS points to the wrong server
dig example.com A +short
dig example.com AAAA +short
dig example.com NS +short
dig @1.1.1.1 example.com A +short
dig @8.8.8.8 example.com A +short
21. Commands that require extra caution
Confirm the path and backup before using recursive deletion. Shell deletion does not normally provide a recycle bin.
Incorrect permissions can break websites, SSH, control panels, mail services, and system packages.
Always permit the active SSH port before enabling, reloading, or replacing firewall rules.
Back up the database and verify the selected database before running destructive SQL statements.
Formatting, repartitioning, and filesystem repair commands can cause permanent data loss when applied to the wrong device.
Validate configuration first and schedule disruptive changes during a maintenance window.
Choose the right server management approach
SSH access provides complete control, but it also makes you responsible for operating system updates, firewall policies, service configuration, backups, security monitoring, and incident response.
An unmanaged VPS is suitable when you are comfortable administering Linux yourself. A managed service is better when you need experienced administrators to handle server configuration, hardening, updates, monitoring, or troubleshooting.
Flexible virtual servers for websites, applications, development environments, and scalable Linux workloads.
Root-access virtual infrastructure in privacy-friendly locations for legitimate projects requiring jurisdictional flexibility.
Single-tenant hardware for high traffic, larger databases, sustained workloads, custom networking, and greater isolation.
Professional assistance with updates, hardening, monitoring, configuration, optimization, and technical incidents.
Frequently asked questions
What is the difference between SSH and the Linux shell?
SSH is the encrypted network protocol used to connect to a remote system. The shell, such as Bash, is the command interpreter that runs after you connect. SSH transports the session, while the shell processes your commands.
Should I log in directly as root?
For routine administration, using a normal user with controlled sudo access provides better accountability and reduces the risk of accidental system-wide changes. Keep a tested recovery method or console available before changing root or SSH authentication settings.
Are SSH keys safer than passwords?
Properly protected SSH keys are generally more resistant to password guessing and credential reuse. Protect the private key with a passphrase, restrict its file permissions, and never share it.
Why does a command work on Ubuntu but not AlmaLinux?
Linux distributions may use different package managers, service names, configuration paths, firewall systems, and default packages. Ubuntu and Debian normally use APT, while AlmaLinux and Rocky Linux use DNF. Apache is commonly called apache2 on Ubuntu and httpd on AlmaLinux.
How can I tell which process is using a port?
Run sudo ss -lntup to view listening ports and associated processes. You can also use sudo lsof -i :PORT for one specific port.
What should I check before restarting a service?
Validate the service configuration, inspect its current status and recent logs, confirm dependencies, and consider whether the restart will interrupt production traffic. For web servers and SSH, always run the available syntax test first.
How do I recover after blocking myself with the firewall?
Use the server’s remote console, KVM, IPMI, or management console to correct the firewall rule. When no console access is available, the hosting provider may need to restore access. This is why SSH access should be tested in a second session before closing the original connection.
Do these commands work on cPanel servers?
Most standard Linux commands work, but cPanel manages many services, configuration files, permissions, and package components itself. Avoid replacing cPanel-managed configurations manually unless you understand how cPanel rebuilds and maintains them.
![]()
Deploy a Linux server with full administrative control
UnderHost provides Cloud VPS, Offshore VPS, dedicated servers, backup solutions, and professional server management for projects that need reliable infrastructure and experienced technical support.
