+359 893 560 371 Support 24/7 · sales 9:00–18:00 All services operational
VPS and servers

Linux commands: a cheat sheet for managing a server

A cheat sheet of essential Linux commands organised by task: navigation, files, permissions, processes, disk space, services, logs, networking, archives and packages, plus the dangerous commands to watch out for.

The MaxBG team · 16 min read
Linux commands: a cheat sheet for managing a server

If you have a VPS or your own server, sooner or later you'll find yourself in front of a black window with a blinking cursor. No buttons, no menus, just the command line. For many people this is the most intimidating part of running a server, but in reality a few dozen commands cover almost every day-to-day task.

This is a cheat sheet of the essential Linux commands, organised by task: navigation, files, permissions, processes, disk space, services, logs, networking, archives and packages. For each command you'll see what it does and how it's used in practice, as well as which commands are dangerous and how to avoid costly mistakes.

In short

  • Linux commands are typed in the terminal and follow the pattern: command, options, arguments. For example ls -la /var/www.
  • To find your way around: pwd, ls, cd. To work with files: cp, mv, rm, mkdir.
  • To check the server: df -h for disk space, free -h for memory, top for processes.
  • For services and errors: systemctl and journalctl, with logs in /var/log.
  • The rm command deletes permanently, with no recycle bin. Check the path before you press Enter.

What is the Linux command line

The command line, also called the terminal or console, is a text-based way to control a computer. Instead of clicking, you type a command and press Enter. The program that reads and runs your commands is called the shell. On most servers, that's Bash.

On a server, the command line isn't a matter of taste. Most servers have no graphical interface, to save resources, and you access them remotely. You connect over SSH, and if you haven't done that yet, see how to connect to your server over SSH. Once you're in, everything else is done with the commands in this article.

Linux commands in the terminal – managing a server from the command line

The commands below work on all the popular distributions: AlmaLinux, Rocky Linux, Ubuntu and Debian. The differences are mainly in package management, which has its own section.

How a Linux command is structured

Almost every command has three parts: the command name, options that change how it behaves, and arguments it acts on. In ls -la /var/www, the command is ls, the options are -la, and the argument is the /var/www folder.

Options usually start with a single dash and one letter (-l) or two dashes and a whole word (--help). Several single-letter options can be combined: -l -a and -la mean the same thing.

If you can't remember what a command does, there are two quick ways to check. man ls opens the full manual for ls (press "q" to exit), and ls --help shows a short summary of the options. Linux is case-sensitive, so File.txt and file.txt are two different files.

The Linux file system starts from a single root folder, /. Everything else sits under it: there are no C: and D: drives as in Windows. The path /var/www/site means the site folder inside www, which is inside var, which is in the root.

Command What it does
pwd Shows which folder you're in
ls Lists the contents of the folder
ls -la Lists all files, including hidden ones, with size, permissions and date
ls -lh The same, but with sizes in KB, MB and GB
cd /var/www Moves into the given folder
cd .. Moves up one folder
cd ~ Takes you back to your home folder
cd - Takes you back to the folder you were in before

A path can be absolute or relative. An absolute path starts with / and works from anywhere, for example /var/log. A relative path depends on where you are: if you're in /var, typing cd log is enough.

You'll come across a few folders all the time. /etc holds the settings for the system and its services. /var/www is where websites usually live. /var/log contains the logs. /home holds users' folders, and /tmp holds temporary files.

Working with files and folders

These are the commands for creating, copying, moving and deleting. Be especially careful with the last two, because Linux has no recycle bin: once something is deleted, it's gone.

Command What it does
mkdir backup Creates a folder
mkdir -p a/b/c Creates the whole chain of folders at once
touch notes.txt Creates an empty file
cp file.txt copy.txt Copies a file
cp -r site site-old Copies a folder with everything in it
mv old.txt new.txt Renames a file or moves it to another folder
rm file.txt Deletes a file
rm -r folder Deletes a folder and everything in it
ln -s /var/www/site current Creates a symbolic link (shortcut)

A good habit is to copy a file before you edit it: cp nginx.conf nginx.conf.bak. If something goes wrong, you restore the copy and you're back where you started.

The cp, mv and rm commands accept a -i option, which makes them ask before overwriting or deleting anything. On many servers it's already turned on by default for root.

Viewing and editing files

A large part of working on a server is reading configuration files and logs. There are several commands for this, each suited to a different situation.

  • cat file.txt prints the whole file at once. It's handy for short files.
  • less file.txt opens the file one page at a time. Move with the arrow keys, search with "/" and exit with "q".
  • head -n 20 file.txt shows the first 20 lines.
  • tail -n 50 file.txt shows the last 50 lines. Logs grow at the bottom, so this is the command you'll use most for them.
  • tail -f /var/log/nginx/error.log shows new lines live as they appear. Stop it with Ctrl+C.

The easiest editor to use is nano. Open a file with nano file.txt, change the text, save with Ctrl+O and exit with Ctrl+X. The shortcuts are listed at the bottom of the screen. Almost every server also has vim, which is more powerful but takes some getting used to. If you end up in it by accident, you can exit without saving by pressing Esc and typing :q!.

Viewing and editing files on a Linux server with tail and nano

Finding files and text

When you don't know exactly where a file is, or which file contains a particular setting, you search. The two main commands are find for files and grep for text inside files.

  • find /var/www -name "wp-config.php" searches for a file by name in the given folder.
  • find /var/www -mtime -2 lists files changed in the last two days. Very useful if you suspect the site has been hacked.
  • find / -size +500M finds files larger than 500 MB.
  • grep "server_name" /etc/nginx/nginx.conf shows the lines that contain the given text.
  • grep -ri "password" /var/www/site searches every file in the folder, ignoring upper and lower case.

Commands can be chained with a vertical bar, |, called a pipe. The output of the first becomes the input of the second. For example, ps aux | grep nginx shows only the processes related to nginx, and grep "404" access.log | wc -l counts how many 404 errors appear in the log.

File permissions and ownership

Every file in Linux has an owner, a group and permissions that determine who can do what with it. If you run ls -l, you'll see something like -rw-r--r-- 1 nginx nginx 4096 index.php. The first part is the permissions, followed by the owner and the group.

There are three types of permission: read (r), write (w) and execute (x). They're set separately for the owner, the group and everyone else. They're most often written as numbers, where read is 4, write is 2 and execute is 1, and their sum gives the permissions for each of the three groups.

Command What it does
chmod 644 file.php The owner can read and write, everyone else can only read. Standard for website files
chmod 755 folder The owner has full rights, everyone else can read and enter. Standard for folders
chmod 600 .env Only the owner can read and write. For files containing passwords
chown nginx:nginx file.php Changes the file's owner and group
chown -R nginx:nginx /var/www/site Changes the owner of the whole folder and everything in it

Never set 777 permissions on website files and folders, even when something "won't work". That lets any process on the server write to them, including malicious code. If the site can't write a file, the problem is almost always the owner, not the permissions.

Users and sudo

The root user has unlimited rights. That's why, on a well-configured server, you work as a regular user and run commands that need administrator rights with sudo in front of them.

  • whoami shows which user you're logged in as.
  • sudo systemctl restart nginx runs a single command with root rights. The system will ask for your password.
  • sudo -i switches to a root session until you type exit.
  • passwd changes the current user's password.
  • adduser ivan (on Ubuntu and Debian) or useradd -m ivan (on AlmaLinux) creates a new user.

Processes and server load

When a website slows down, the first question is what's putting load on the server. You can see the answer in seconds with a few commands.

  • top shows processes live, sorted by CPU usage. Press "M" to sort them by memory and "q" to exit.
  • htop is a friendlier, colour version of top. If it isn't on the server, it can be installed separately.
  • ps aux lists every process running at the moment. Combined with grep, it finds a specific process.
  • kill 1234 stops the process with ID 1234. If it doesn't respond, kill -9 1234 forces it to stop.
  • free -h shows how much memory is used and how much is free.
  • uptime shows how long the server has been running and its load average.

The load average is three numbers: the average load over the last 1, 5 and 15 minutes. If the number stays higher than the number of CPU cores, the server can't keep up. You can see the number of cores with nproc.

Processes and server load on Linux – top and load average

How to check disk space

A full disk is one of the most common reasons a website or database stops working. The database can't write, logs can't grow and email doesn't get delivered.

  • df -h shows how much space each disk has and how much is free.
  • du -sh /var/www shows how much space a folder takes up.
  • du -sh /var/* | sort -h shows the size of each folder in /var, sorted from smallest to largest. It quickly shows what's eating the space.
  • ncdu / is an interactive tool that lets you browse folders and see their sizes. It's installed separately.

Space is most often eaten up by old logs, forgotten archives and backups nobody deletes. If you keep backups on the server itself, give them an expiry date, and always keep at least one copy somewhere else.

Services and logs

The web server, the database and PHP run as services. On modern distributions you manage them with systemctl and read their logs with journalctl or directly from /var/log.

Command What it does
systemctl status nginx Shows whether the service is running and its latest messages
systemctl restart nginx Restarts the service
systemctl reload nginx Reloads the settings without dropping connections
systemctl enable nginx Makes the service start automatically when the server reboots
journalctl -u nginx -n 50 Shows the last 50 lines of the service's log
journalctl -u nginx -f Shows the log live

Before restarting nginx after a configuration change, test it with nginx -t. If there's an error, the command shows the line, and the running website won't go down. The same rule applies to Apache with apachectl configtest.

Network commands

When a website won't load, you need to work out whether the problem is the network, DNS or the service itself. These commands help answer that.

  • ip a shows the server's IP addresses.
  • ping google.com checks whether you can reach an address. Stop it with Ctrl+C.
  • curl -I https://example.com shows the response headers, including the status code (200, 301, 404, 500).
  • ss -tulpn shows which services are listening on which ports.
  • dig example.com shows the domain's DNS records and which IP address it points to.

Archives and file transfers

You'll need archives for manual backups and for moving a website from one server to another. The most widely used format on Linux is .tar.gz.

  • tar czf site.tar.gz /var/www/site creates a compressed archive of the folder.
  • tar xzf site.tar.gz extracts the archive into the current folder.
  • unzip archive.zip extracts a zip file.
  • wget https://example.com/file.zip downloads a file from the internet straight to the server.
  • scp site.tar.gz user@server:/root/ copies a file to another server over SSH.
  • rsync -avz /var/www/site/ user@server:/var/www/site/ syncs folders between servers and transfers only the changes.

A manual archive is a useful extra, but it doesn't replace automatic backups. You can read more about how and how often to make them in our article on website backup.

Archiving and transferring files between servers with tar, scp and rsync

Installing and updating software

This is where distributions differ. AlmaLinux, Rocky Linux and Red Hat use dnf, while Ubuntu and Debian use apt. You can see which Linux you have with cat /etc/os-release.

Task AlmaLinux, Rocky Linux Ubuntu, Debian
Update all packages dnf update apt update && apt upgrade
Install a package dnf install htop apt install htop
Remove a package dnf remove htop apt remove htop
Search for a package dnf search nginx apt search nginx

Update your server regularly, because most breaches exploit known vulnerabilities that were patched long ago. Make a backup before any major update.

Useful terminal tricks

A few keys and symbols save a lot of typing and mistakes.

  • Tab completes the name of a command, file or folder. Press it twice to see the possible options.
  • The up arrow brings back previous commands so you don't have to type them again.
  • Ctrl+R searches your command history. history shows the full list.
  • Ctrl+C stops the command that's currently running.
  • Ctrl+L or clear clears the screen.
  • > writes a command's output to a file, overwriting it. >> appends it to the end of the file.
  • && runs the second command only if the first one succeeded, for example nginx -t && systemctl reload nginx.

Dangerous commands to be careful with

Most server disasters aren't caused by hackers but by a single mistyped command. These are the ones where it's worth pausing for a second before you press Enter.

  • rm -rf with the wrong path. One extra space, such as rm -rf / var/www/old instead of rm -rf /var/www/old, can wipe the whole system.
  • chmod -R 777. It opens files for writing by anyone and makes the site an easy target.
  • > instead of >>. It overwrites the file instead of appending to it. One missing ">" can wipe out a configuration file.
  • Scripts from the internet run directly. Commands such as curl ... | bash run someone else's code with your rights. Read what it does first.
  • dd and mkfs. They work directly on disks. The wrong disk means lost data.

The simple rule is: before deleting anything or making a bulk change, run pwd, check with ls exactly what you're about to affect, and have a fresh backup.

Dangerous Linux commands – how not to wipe your server by mistake

Common errors and what they mean

Error messages in the terminal usually tell you exactly what the problem is.

  • command not found: the command isn't installed or you've mistyped the name. Check for a typo, then install the package.
  • Permission denied: you don't have the rights for this action. Try with sudo or check who owns the file.
  • No such file or directory: the path is wrong. Check with pwd and ls where you are and exactly what the file is called, including upper and lower case.
  • No space left on device: the disk is full. Start with df -h and du -sh.
  • Address already in use: the port is already taken by another service. Find out which with ss -tulpn.

Frequently asked questions

What are the most important Linux commands for beginners?

To start with, pwd, ls, cd, cp, mv, rm, mkdir, cat, nano, df -h, free -h, top and systemctl are enough. With them you can find your way around, edit files and check the state of the server.

How can I see all the commands in Linux?

Press Tab twice on an empty line and the terminal will offer to show every available command. For a description of a specific command, use man followed by its name.

Are the commands the same on Ubuntu and AlmaLinux?

Almost all the basic commands are the same. The main difference is in installing software: apt on Ubuntu and Debian, and dnf on AlmaLinux and Rocky Linux.

How do I exit nano or vim?

You exit nano with Ctrl+X. If there are changes, the editor will ask whether to save them. You exit vim without saving with Esc and :q!, and with saving using Esc and :wq.

Can a file deleted with rm be recovered?

Usually not. Linux has no recycle bin on the command line, so the only reliable way is to restore the file from a backup.

What does sudo mean?

Sudo lets a regular user run a single command with administrator rights. That way you don't work as root all the time, which reduces the risk of mistakes.

How can I see what's putting load on the server?

Run top or htop. The processes at the top are the ones using the most CPU. Press "M" in top to sort them by memory usage.

Do I need Linux commands if I have a control panel?

For the day-to-day running of a website, the panel is usually enough. You need commands when something goes wrong, when diagnosing a slow site, or when you manage a VPS without a panel.

Conclusion

You don't need to remember hundreds of Linux commands to run a server. A few dozen cover almost everything: navigation, working with files, permissions, processes, disk space, services and logs. Start with the basics, use Tab and man when you're unsure, and follow one rule: before deleting or changing anything, check where you are and have a backup. Over time, the commands will become faster than any control panel.

Looking for a server to put these commands to use? Get a VPS with full root access. And if you'd rather not deal with administration, choose a managed VPS and we'll look after the server for you.

See the VPS plans · Managed VPS · What is a VPS

AI assistant instant answers, 24/7
🍪 We use cookies

To keep MaxBG fast and secure, we use essential cookies. With your consent we also add Google tools (Analytics and Ads). They help us analyse visits and improve our hosting services, and the data is shared securely with Google. You can change your choice or learn more in our Cookie policy