How to Manage a VPS via the Command Line?
Hey colleagues! I think each of us who has ever dealt with VPS hosting administration knows that the command line is our best friend, and sometimes our worst enemy. But fear not! In this article, we'll break down the main points of VPS management via the console, so that even the most novice Linux user feels confident. I'll share my experience, the mistakes I've made, and, of course, show you the most useful commands and techniques. We'll install software, configure services, monitor the system – in short, we'll do everything to make your VPS run like clockwork. This setup will be *fire*! Let's go!Contents:
- SSH Connection Basics for VPS
- Package Management and Software Installation
- Configuring and Managing Core Services
- Monitoring VPS Resources
- File and Permissions Management
- Troubleshooting Common Issues and Debugging
SSH Connection Basics for VPS

ssh user@192.168.1.100
Where `user` is your username, and `192.168.1.100` is the VPS IP address. Replace them with your own data, obviously. If you're using a non-standard port, add `-p` with the port number:
ssh -p 2222 user@192.168.1.100
If you're using an SSH key, you need to specify its path with the `-i` option:
ssh -i ~/.ssh/id_rsa user@192.168.1.100
Once connected, you'll see the server's welcome message and can start entering commands.
Setting up SSH Keys (for the Paranoid)
I'll be straight with you, setting up SSH keys is a bit of a hassle, but it's worth it. First, you need to generate a key pair (public and private) on your local computer:ssh-keygen -t rsa -b 4096
This command will create two files: `id_rsa` (the private key, keep it secret!) and `id_rsa.pub` (the public key). Now you need to copy the contents of `id_rsa.pub` to the server. There are several ways to do this. The simplest is to use the `ssh-copy-id` command:
ssh-copy-id -i ~/.ssh/id_rsa.pub user@192.168.1.100
This command will ask you for the server password, and then copy the public key to the `~/.ssh/authorized_keys` file. If the `ssh-copy-id` command is not available, you can do it manually:
cat ~/.ssh/id_rsa.pub | ssh user@192.168.1.100 "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
After this, you can log into the server without a password. Boom! That's it! Now, to completely secure yourself, you can disable password login in the `/etc/ssh/sshd_config` file. Open it in a text editor (e.g., `nano` or `vim`):
sudo nano /etc/ssh/sshd_config
Find the line `PasswordAuthentication yes` and change it to `PasswordAuthentication no`. You can also change the SSH port to a non-standard one (e.g., 2222) to attract less attention from bots. After that, restart the SSH service:
sudo systemctl restart sshd
*Word of warning*: If you mess up the SSH configuration, you might lose access to the server. So always make backups and check your configuration before restarting the service. Been there, done that...
| Authentication Method | Security | Convenience |
|---|---|---|
| Password | Low | High |
| SSH Key | High | Medium (requires setup) |
Security is always excessive until it's not enough.
Rob Joyce, NSA
Package Management and Software Installation

sudo apt update
This command will download package lists from the repositories. Then you can upgrade installed packages:
sudo apt upgrade
This command will upgrade all packages for which updates are available. If you want to upgrade only one package, specify its name:
sudo apt upgrade nginx
To install a new package, use the `apt install` command:
sudo apt install nginx
This command will install the `nginx` package and all its dependencies. To remove a package, use the `apt remove` command:
sudo apt remove nginx
This command will remove the `nginx` package but leave its configuration files. If you want to remove the configuration files as well, use the `apt purge` command:
sudo apt purge nginx
Working with Repositories (When Standard Ones Aren't Enough)
Sometimes, you might need to install a package that isn't in the standard repositories. In this case, you need to add a new repository. To do this, you either add its address to the `/etc/apt/sources.list` file or create a separate file in the `/etc/apt/sources.list.d/` directory. For example, to add the Node.js repository, you can create a file `/etc/apt/sources.list.d/nodesource.list` with the following content:deb https://deb.nodesource.com/node_16.x focal main
deb-src https://deb.nodesource.com/node_16.x focal main
Where `focal` is the codename for your Ubuntu version. You can find it using the `lsb_release -c` command. After adding the repository, you need to update the package list and install the repository's signing key:
sudo apt update
curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -
Now you can install Node.js:
sudo apt install nodejs
*Pro tip*: Always verify the authenticity of repositories and signing keys. Don't trust unknown sources.
| Command | Description |
|---|---|
sudo apt update | Updates the package list |
sudo apt upgrade | Upgrades installed packages |
sudo apt install <package> | Installs a package |
sudo apt remove <package> | Removes a package |
sudo apt purge <package> | Removes a package and its configuration files |
Software is a great combination between artistry and engineering.
Bill GatesReady to Apply These Commands? Get Your Own VPS!
Start managing your server with full control and flexibility. Our VPS plans offer the power you need. — from €4.49/mo.
Choose VPS Hosting →
Configuring and Managing Core Services
Alright, so, now let's talk about service configuration. Things get a bit more complex here, but don't worry, I'll help you figure it out. The deal is, every service has its own configuration file where its settings are stored. These files are usually located in the `/etc` directory. Let's look at configuring a few popular services.Nginx Configuration (web server)
Nginx is a very popular web server. Its configuration file is located at `/etc/nginx/nginx.conf`. In this file, you can configure the main server parameters, such as the number of worker processes, connection limits, etc. But the main settings for websites are located in the `/etc/nginx/sites-available/` directory. To create a new site, you need to create a configuration file in this directory, for example, `/etc/nginx/sites-available/mysite.com`:server {
listen 80;
server_name mysite.com www.mysite.com;
root /var/www/mysite.com;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
This setup is fire! This file tells Nginx to listen on port 80 (HTTP) and serve the `mysite.com` website from the `/var/www/mysite.com` directory. To activate the site, you need to create a symbolic link to this file in the `/etc/nginx/sites-enabled/` directory:
sudo ln -s /etc/nginx/sites-available/mysite.com /etc/nginx/sites-enabled/
After that, you need to restart Nginx:
sudo systemctl restart nginx
Now your site should be accessible at `mysite.com`. To check if Nginx is running, you can execute the command:
sudo systemctl status nginx
If everything is okay, you will see the message `active (running)`.
UFW Configuration (firewall)
UFW (Uncomplicated Firewall) is a simple and convenient firewall. It allows you to configure rules for incoming and outgoing traffic. To enable UFW, execute the command:sudo ufw enable
Before enabling, you need to allow incoming SSH connections:
sudo ufw allow ssh
You can also allow incoming connections for HTTP (port 80) and HTTPS (port 443):
sudo ufw allow 80
sudo ufw allow 443
To view the current rules, execute the command:
sudo ufw status
*Word of warning*: Don't forget to allow incoming SSH connections, otherwise you will lose access to the server! Been there, done that...
| Service | Configuration File | Restart Command |
|---|---|---|
| Nginx | /etc/nginx/nginx.conf, /etc/nginx/sites-available/* | sudo systemctl restart nginx |
| UFW | /etc/ufw/ufw.conf | sudo ufw enable, sudo ufw disable |
Simplicity is prerequisite for reliability.
Edsger W. Dijkstra
Monitoring VPS Resources
Real talk, resource monitoring is a must-have for any VPS administrator. Without monitoring, you won't know what's happening with your server until it crashes. Honestly, I don't understand how people work without monitoring. It's like driving a car blindfolded.Using the Command Line for Monitoring
There are several useful commands in the command line for monitoring resources. The simplest is the `top` command:top
This command shows a list of processes, sorted by CPU usage. You'll see information about CPU, memory, swap, etc. To exit `top`, press `q`. Another useful command is `htop`:
htop
`htop` is an improved version of `top` with a more user-friendly interface and the ability to sort by various parameters. If you don't have `htop`, install it:
sudo apt install htop
To monitor disk usage, you can use the `df -h` command:
df -h
This command shows information about occupied and free space on each disk. To monitor memory usage, you can use the `free -m` command:
free -m
This command shows information about occupied and free memory in megabytes. To view network usage, you can use the `iftop` command:
iftop
This command shows a list of connections and their speed. If you don't have `iftop`, install it:
sudo apt install iftop
*Pro tip*: Set up automatic monitoring using tools like Grafana and Prometheus. This will allow you to track resources in real-time and receive notifications about issues.
Example Script for Load Monitoring
Here's a simple script that shows current CPU, memory, and disk load:#!/bin/bash
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | sed "s/.*id: \([0-9.]*\).*/\1/" | awk '{print 100 - $1}')
MEM_USAGE=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2 }')
DISK_USAGE=$(df -h / | awk '$NF=="/"{printf "%s", $5}')
echo "CPU Usage: $CPU_USAGE%"
echo "Memory Usage: $MEM_USAGE"
echo "Disk Usage: $DISK_USAGE"
Save this script to a file, for example, `monitor.sh`, and make it executable:
chmod +x monitor.sh
And run it:
./monitor.sh
This script will show the current CPU, memory, and disk load.
| Command | Description |
|---|---|
top | Shows a list of processes sorted by CPU usage |
htop | An improved version of top |
df -h | Shows information about occupied and free disk space |
free -m | Shows information about occupied and free memory |
iftop | Shows network usage |
Without data, you're just another person with an opinion.
W. Edwards Deming
File and Permissions Management
Okay, so, now let's talk about file and permissions management. Honestly, this is one of the most fundamental things you need to know to manage a server. The deal is, everything in Linux is a file. Even directories. So, knowing how to work with files is a must-have.Basic File Management Commands
The most basic command is `ls` (list). It shows a list of files and directories in the current directory:ls
To show all files, including hidden ones (starting with a dot), use the `-a` option:
ls -a
To show detailed information about files, including permissions, use the `-l` option:
ls -l
The `cd` (change directory) command allows you to navigate to another directory:
cd /var/www/html
To go back to the previous directory, use `cd ..`:
cd ..
To return to your home directory, use `cd` without arguments or `cd ~`:
cd
cd ~
The `mkdir` (make directory) command creates a new directory:
mkdir mydirectory
The `rm` (remove) command deletes a file:
rm myfile.txt
To delete a directory, use the `rmdir` (remove directory) command:
rmdir mydirectory
But `rmdir` only deletes empty directories. To delete a directory with all its contents, use the `rm -r` command:
rm -r mydirectory
*Word of warning*: Be careful with the `rm -r` command. It can delete anything without warning. Been there, done that...
The `cp` (copy) command copies a file:
cp myfile.txt mycopy.txt
To copy a directory, use the `-r` option:
cp -r mydirectory mycopydirectory
The `mv` (move) command moves a file or directory:
mv myfile.txt /tmp
`mv` can also be used to rename files:
mv myfile.txt newfile.txt
Permissions (777 - evil!)
In Linux, every file has permissions for the owner, group, and other users. Permissions are denoted by three digits, each representing the sum of rights:- 4 – read permission
- 2 – write permission
- 1 – execute permission
- 7 – owner has read, write, and execute permissions (4+2+1)
- 5 – group has read and execute permissions (4+1)
- 5 – other users have read and execute permissions (4+1)
chmod 755 myfile.txt
*Pro tip*: Never give `777` permissions to files or directories. It's insecure. Use the minimum necessary permissions.
| Command | Description |
|---|---|
ls | Shows a list of files and directories |
cd | Changes to another directory |
mkdir | Creates a new directory |
rm | Deletes a file |
cp | Copies a file |
mv | Moves or renames a file |
chmod | Changes permissions |
Give a man a program, frustrate him for a day. Teach a man to program, frustrate him for a lifetime.
Muhammad Waseem
Troubleshooting Common Issues and Debugging
Alright, let's talk about troubleshooting. Honestly, this is one of the most complex and crucial parts of VPS management. The deal is, something always breaks. And your job is to find and fix the problem.Basic Debugging Tools
The first and most important tool is logs. Most services write their logs to files. They are usually located in the `/var/log` directory. For example, Nginx logs are in `/var/log/nginx/`. To view the last lines of a log, use the `tail -f` command:tail -f /var/log/nginx/error.log
This command will show the last lines of the `error.log` file and update them in real-time. To view all logs for a specific period, use the `grep` command:
grep "error" /var/log/nginx/error.log
This command will show all lines containing the word "error". To view logs for systemd services, you can use the `journalctl` command:
journalctl -u nginx
This command will show the logs for the `nginx` service. To view logs from the last 10 minutes, use the `--since` option:
journalctl -u nginx --since "10 minutes ago"
To view logs in real-time, use the `-f` option:
journalctl -u nginx -f
If a service isn't starting, check its status using the `systemctl status` command:
systemctl status nginx
Need Even More Power? Upgrade to a Dedicated Server!
For ultimate performance and complete control, explore our SSD dedicated servers. Perfect for demanding projects.
Explore Dedicated Servers →