Sign In
Sign In

How to Set Up a Firewall with UFW on Ubuntu

How to Set Up a Firewall with UFW on Ubuntu
Mohammad Waqas Shahid
Technical writer
Ubuntu Firewall
02.04.2024
Reading time: 11 min

In this comprehensive tutorial, users are guided through the process of setting up a robust firewall using the Uncomplicated Firewall (UFW) on Ubuntu. UFW provides an intuitive interface for managing netfilter firewall rules, offering an accessible solution for securing Ubuntu systems effectively.

Introduction to UFW

UFW, or Uncomplicated Firewall, is a user-friendly interface for managing iptables, the standard firewall management tool for Linux systems. It simplifies the process of creating and managing firewall rules, making it accessible even to users with limited networking knowledge.

Understanding Firewall Basics

Before diving into the configuration process, it's essential to understand some fundamental concepts related to firewalls and how they operate.

What is a Firewall?

A firewall is a network security system that monitors and controls incoming and outgoing network traffic based on predetermined security rules. It acts as a barrier between a trusted internal network and untrusted external networks, such as the internet.

On Hostman, you can set up a cloud firewall that provides cutting-edge defense tailored for businesses of all sizes.

Types of Firewalls

There are several types of firewalls, including packet-filtering firewalls, stateful inspection firewalls, proxy firewalls, and application layer firewalls. Each type operates differently but serves the common purpose of protecting networks and systems from unauthorized access and malicious activity.

Creating Account and Server on Hostman

To kick off the process, prospective server hosts are encouraged to visit the official Hostman website. Sign up for a new account by providing essential details and create a strong password. Following this, check your email for a verification link, click on it, and swiftly log in to your Hostman account.

Image1

Within the Hostman control panel, the user-friendly interface offers to start a new server. By navigating to the Create button, users can initiate the server creation process. Select the parameters you need, including software (for the purposes of this guide, we need a server with the Ubuntu operating system), configuration, geographical region, and backups, choose the project for this server, then click Order to create your server. 

The server will be installed in a couple of minutes and you will see the server's dashboard. Later on, to find your server you can go directly to Cloud servers or to the project the server is added to. 

234567

Click on your server, start it by the play button and scroll down to see the SSH command and root password for your Ubuntu server.

593f4dc2 53df 4468 8352 F2309e4ffb7f

Accessing Your Server

Access the server through the web-based terminal provided by Hostman or use preferred SSH client. For this tutorial accessing through SSH is used.

Image5

Updating System Packages

The following code is to be written in terminal to update system packages of Ubuntu:

sudo apt-get update
sudo apt-get upgrade

Image7

Type “y” and hit Enter.

Did you know?

Some architectures combine Kubernetes managed services for stateless applications with an Ubuntu server for background jobs and system-level tasks. Hostman offers low priced tariffs for Linux VPS.

After the upgrade, the following screen may appear. (If there is nothing to upgrade on your server, i/e. you already had the latest versions of the installed packages, you will not see this window and can proceed to the next step.)

Image6

In this popup, you are prompted to select which services should be restarted after the installation process. The services listed are part of the systemd system and are related to various system functionalities.

Here's a brief explanation of the options:

  • systemd-journald.service: The journal service, which handles system logs.

  • systemd-logind.service: The login service, which manages user logins.

  • systemd-manager: The service manager for the system.

  • systemd-networkd.service: The network service, responsible for network configuration.

  • systemd-resolved.service: The DNS resolver service.

  • systemd-timesyncd.service: The time synchronization service.

  • unattended-upgrades.service: A service for automatically applying package updates.

  • user@0.service: A user-specific service (user 0 refers to the root user).

Given the importance of network-related services for firewall functionality, it is recommended to restart the following services after the upgrade:

  • systemd-networkd.service: This service is responsible for network configuration. Restarting it ensures that any changes made during the upgrade, particularly those related to networking or firewall rules, take effect.
  • systemd-resolved.service: The DNS resolver service handles DNS resolution. Restarting it is advisable if there were changes to DNS configurations or updates to the DNS resolver service, which could impact firewall rules that rely on domain name resolution.

  • systemd-timesyncd.service: The time synchronization service ensures accurate timekeeping on the system. Proper time synchronization is crucial for security measures such as certificate validation and timestamping of firewall logs.

These services are crucial for maintaining system functionality and security, especially in the context of firewall configuration. 

Installing UFW on Ubuntu

Before starting the firewall configuration, it's essential to ensure that UFW is installed on your Ubuntu system. Here's how to do it:

Checking UFW Installation Status

Open the terminal and run the following command to check if UFW is installed:

sudo ufw status

You should see the status Active (running). If the status is inactive, start the service using the command:

sudo ufw enable

If UFW is not installed, the terminal will output the message Command ‘ufw’ not found. Follow the instruction below to install it.

Installing UFW

Install UFW by executing the following commands in the terminal:

sudo apt update
sudo apt install ufw

After completing the installation, recheck the status by typing:

sudo ufw status

Did you know?

Backend services commonly use MySQL hosting solutions to simplify backups and updates. Application logic and APIs can then be deployed as cloud apps on a managed Application Platform with minimal operational effort.

Basic Firewall Configuration with UFW

Once UFW is installed, it's time to configure the basic firewall settings. Here's how to get started:

Enabling UFW

Activate UFW by running the following command in the terminal:

sudo ufw enable

You will receive a confirmation message indicating that the firewall is now operational.

Allowing SSH Access

If SSH access is not permitted by default, allow SSH connections using the command:

sudo ufw allow ssh

Permitting Specific Ports

To enable specific ports for various services such as web servers or database servers, use the command:

sudo ufw allow <port_number>

Replace <port_number> with the designated port number you wish to allow.

Advanced UFW Configuration

For advanced users looking to customize their firewall settings, UFW offers a range of configuration options:

Denying Incoming Connections

For enhanced security, deny all incoming connections by default and allow only designated ones:

sudo ufw default deny incoming

Allowing Outgoing Connections

Allow all outgoing connections by default:

sudo ufw default allow outgoing

Implementing Custom Rules

Define custom rules based on specific requirements:

sudo ufw <rule>

Below are examples of configuring custom rules in UFW for various scenarios, including allowing SSH, HTTP/HTTPS, specifying port ranges, and denying access based on IP addresses or subnets:

Allowing SSH Connections

To allow SSH connections, you can use the service name or specify the port number:

sudo ufw allow ssh

Or:

sudo ufw allow 22

Allowing HTTP and HTTPS Connections

To allow HTTP and HTTPS traffic, use the respective service names or port numbers:

sudo ufw allow http
sudo ufw allow https

Or:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Allowing Access to a Specific Port Range

To allow access to a range of ports, specify the port range:

sudo ufw allow 8000:9000/tcp

Allowing Access from Specific IP Addresses or Subnets

To allow access from specific IP addresses or subnets, specify the IP address or subnet:

sudo ufw allow from 192.168.1.100
sudo ufw allow from 192.168.0.0/16

Denying Access to a Specific Port

To deny access to a specific port, use the deny command:

sudo ufw deny 1234

Denying Access from Specific IP Addresses or Subnets

To deny access from specific IP addresses or subnets, use the deny command:

sudo ufw deny from 10.0.0.1
sudo ufw deny from 172.16.0.0/24

Denying All Incoming Connections (Except Allowed Ones)

To deny all incoming connections by default and allow only specific ones, use the default deny command:

sudo ufw default deny incoming

Allowing All Outgoing Connections

To allow all outgoing connections by default, use the default allow command:

sudo ufw default allow outgoing

These examples demonstrate how to configure custom rules in UFW for different scenarios, including allowing or denying access based on services, ports, IP addresses, and subnets. Customise these rules according to your specific requirements to enhance the security and control of your firewall configuration.

A Brief Guide for Requirements for Custom Rules

Following is a brief elaboration on which requirements may necessitate specific customizations in firewall rules to enhance security and control:

  1. Requirement: Secure Remote Access

Allowing SSH access (port 22) for remote administration while restricting access from specific IP addresses or subnets to prevent unauthorised access.

  1. Requirement: Hosting Web Services

Allowing HTTP (port 80) and HTTPS (port 443) traffic to host web services, while potentially restricting access to specific IP addresses or subnets to limit exposure to the public internet.

  1. Requirement: Application with Specific Port Range

Allowing access to a range of ports required by a specific application (e.g., ports 8000-9000) while denying access to all other ports to reduce attack surface.

  1. Requirement: Network Segmentation

Defining rules to allow communication between different segments of the network while denying access from external networks to sensitive segments to enforce network segmentation and control.

  1. Requirement: Denial of Service (DoS) Protection

Implementing rate-limiting rules to mitigate DoS attacks by limiting the number of incoming connections per second from specific IP addresses or subnets.

  1. Requirement: Compliance with Regulatory Standards

Implementing firewall rules to enforce compliance with regulatory standards (e.g., PCI DSS, HIPAA) by restricting access to sensitive data and ensuring secure communication channels.

  1. Requirement: Log Monitoring and Analysis

Enabling logging for specific firewall rules to monitor and analyze network traffic for security incidents, compliance audits, and troubleshooting purposes.

  1. Requirement: Application-Specific Rules

Defining application-specific rules based on the requirements of the deployed applications, such as allowing access to database ports only from application servers.

  1. Requirement: BYOD (Bring Your Own Device) Policies

Implementing rules to allow access for authorised devices while restricting access for unauthorised devices based on device attributes or user credentials.

  1. Requirement: High Availability and Failover

Configuring redundant firewall rules across multiple firewall instances to ensure high availability and failover in case of hardware or network failures.

These customizations align with best practices and address specific requirements to enhance security, control, and compliance in firewall configurations without technical errors or inaccuracies.

Cloud tip:

For test deployments and small projects, consider renting a server with minimum configuration that scales smoothly as your needs grow.

Testing Firewall Configuration

After configuring the firewall, it's essential to verify that the rules are applied correctly and test connectivity:

Verifying Firewall Rules

Ensure the correct application of firewall rules:

sudo ufw status verbose

Testing Connectivity

Conduct connectivity tests to verify that permitted connections function as intended. Users can do this by attempting to establish connections to services running on the system from both local and remote hosts.

Monitoring and Managing UFW

Once the firewall is configured, it's important to monitor and manage UFW to ensure optimal security.

Checking UFW Status

Monitor the status of UFW at any time:

sudo ufw status

Disabling UFW

Temporarily disable UFW when necessary:

sudo ufw disable

Logging Firewall Activity

Enable logging to monitor firewall activity and identify potential security threats:

sudo ufw logging on

Conclusion

Implementing a firewall using UFW on Ubuntu is crucial for enhancing system security and safeguarding against potential threats. By following the steps outlined in this tutorial, users can effectively configure and manage their firewall settings, ensuring the protection of their Ubuntu systems. With UFW's user-friendly interface and powerful capabilities, users can easily create and enforce firewall rules to control network traffic and prevent unauthorized access. By understanding the basics of firewalls and utilizing the advanced configuration options provided by UFW, users can create a robust defense against cyber threats.

By the way, with Hostman, you can run your workloads on an efficient Amsterdam VPS that support low latency for EU-based users. Check this out, we have plenty of budget VPS hosting options for your projects.

Ubuntu Firewall
02.04.2024
Reading time: 11 min

Similar

Ubuntu

Installing and Configuring Zabbix on Ubuntu 22.04

Zabbix is a popular open-source tool designed for monitoring servers, networks, services, cloud resources, and business metrics. It consists of several components, including: Zabbix Server: The core component responsible for data storage and network service management. Zabbix Agent: A background utility (daemon) that monitors and collects statistics on resources like RAM, CPU, and application metrics. It supports both active (agent requests data) and passive (agent waits for server requests) modes. Zabbix Proxy: An optional component that distributes the load on the Zabbix server. Web Interface: A web panel for tracking system metrics and configuring both Zabbix and monitored components. In this tutorial, we'll install Zabbix 6 on Ubuntu 22.04 and connect and configure one agent. Prerequisites You will need: Two cloud servers or virtual machines running Ubuntu 22.04: one for the Zabbix server and one for the Zabbix agent. A pre-installed MySQL/MariaDB or PostgreSQL database on the host for the Zabbix server. This tutorial uses PostgreSQL. Installing the Zabbix Server All steps should be performed as root or a sudo user. Add the official Zabbix repository: wget https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.0-4+ubuntu22.04_all.deb Install the downloaded package: dpkg -i zabbix-release_6.0-4+ubuntu22.04_all.deb Install Zabbix server and dependencies: apt update && apt -y install zabbix-server-pgsql zabbix-frontend-php php8.1-pgsql zabbix-nginx-conf zabbix-sql-scripts zabbix-agent Create a PostgreSQL user and database for Zabbix: sudo -u postgres createuser --pwprompt zabbixsudo -u postgres createdb -O zabbix zabbix Import the Zabbix database schema: zcat /usr/share/zabbix-sql-scripts/postgresql/server.sql.gz | sudo -u zabbix psql zabbix Edit the Zabbix server configuration: nano /etc/zabbix/zabbix_server.conf Find the DBPassword parameter and set the database password. Edit the Nginx configuration for Zabbix: nano /etc/zabbix/nginx.conf Uncomment and set the listen and server_name parameters. Restart and enable services: systemctl restart zabbix-server zabbix-agent nginx php8.1-fpmsystemctl enable zabbix-server zabbix-agent nginx php8.1-fpm Verify the Zabbix server status: systemctl status zabbix-server Configuring the Zabbix Server Further configuration is done via the web interface. Navigate to the domain name and port specified in nginx.conf. Select the language. Verify system requirements. Configure database connection: Enter the database name zabbix, user zabbix, and the password. Set Zabbix server name, time zone, and theme. Review and confirm settings. After successful configuration, log in with the default credentials: Admin and zabbix. Installing the Zabbix Agent Switch to the second server for the Zabbix agent installation. Download the Zabbix repository: wget https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_latest+ubuntu22.04_all.deb Install the downloaded package: dpkg -i zabbix-release_latest+ubuntu22.04_all.deb Install the Zabbix agent: apt update && apt -y install zabbix-agent Edit the Zabbix agent configuration: nano /etc/zabbix/zabbix_agentd.conf Set the parameters:  Server: enter the domain name or IP address of the Zabbix server. ServerActive: enter the same value as above; this parameter is responsible for the active mode, when Zabbix independently requests the necessary data. Hostname: enter the agent hostname exactly as it is specified in the system. You can use the hostname command to check. If the hostname is incorrect, the agent will not be able to connect to the Zabbix server. Restart and enable the Zabbix agent: systemctl restart zabbix-agentsystemctl enable zabbix-agent Verify the agent status: systemctl status zabbix-agent Adding the Zabbix Agent in the Zabbix Server Web Interface Navigate to Configuration > Hosts. Click on Create host. Fill in the host details: Host name: set any convenient name for the Zabbix agent to display in the Zabbix server web interface. Groups: create a new group or select an existing one. Groups are used for organizational purposes and to assign access rights to data. Templates: select a template that is used exactly like the agent installed on the server. Interfaces: Add the IP address or domain name of the Zabbix agent host. If using an IP address, you must enter it in the IP address field and select IP in the Connect to section. If using a domain name, you must enter the name in the DNS name section and select DNS in in the Connect to section. Update and verify: The agent will appear in the list and metrics will be available under Monitoring > Hosts > Graphs. Conclusion Zabbix provides comprehensive monitoring for hardware, networks, and services, making it suitable for both corporate environments and personal use. With this guide, you have set up a Zabbix virtual server and agent on Ubuntu, and you are now ready to monitor various metrics and ensure the health of your infrastructure. Frequently Asked Questions (FAQ) What is the difference between Zabbix Server and Agent? Zabbix Server: The central component that gathers data, calculates triggers, and sends notifications. It also houses the web interface. Zabbix Agent: A lightweight daemon installed on the client (the machine you want to monitor). It collects local metrics (CPU, disk, RAM) and sends them back to the Server. How to install and configure Zabbix Agent on Ubuntu? Install: Run sudo apt install zabbix-agent. Configure: Edit the config file (sudo nano /etc/zabbix/zabbix_agentd.conf) and update the Server= and ServerActive= lines to point to your Zabbix Server's IP address. Start: Run sudo systemctl restart zabbix-agent and sudo systemctl enable zabbix-agent. Where is the Zabbix config file in Ubuntu?  There are two main configuration files depending on what you have installed: Server Config: /etc/zabbix/zabbix_server.conf (Configure DB passwords, caches, etc.) Agent Config: /etc/zabbix/zabbix_agentd.conf (Configure which server to send data to). What is a Zabbix Agent?  It is a small piece of software that runs on the target device. It gathers information directly from the hardware and OS (like "how much drive space is left?") and reports it to the central Zabbix Server. Without the agent, you are limited to "agentless" checks like Ping or SNMP. How much RAM does Zabbix need? For the Agent: Negligible (usually < 64MB). For the Server: It depends on the number of hosts. A small home lab (10-20 hosts) runs fine on 2GB-4GB RAM. A production environment monitoring hundreds of devices should start with 8GB-16GB to accommodate the database (MySQL/PostgreSQL) and caching requirements. How do I check if the Zabbix Agent is communicating with the Server?  On the Zabbix Server, you can use the zabbix_get utility to test the connection manually: zabbix_get -s [Client_IP] -k agent.ping If it returns 1, the connection is successful.
28 January 2026 · 6 min to read
Ubuntu

How to Install and Configure VNC on Ubuntu

Various protocols are used to organize remote access to computers and servers. For Windows, the native protocol is RDP, while for Unix/Linux, we mostly use SSH. However, there is another option: VNC. This guide will cover installing a VNC server, specifically the TightVNC implementation, on Ubuntu 22.04, and explain how to connect to the VNC server. Before that, we'd like to recommend you check the instruction on how to deploy server on Ubuntu. And if you’re looking for a reliable, high-performance, and budget-friendly solution for your workflows, Hostman has you covered with Linux VPS Hosting options, including Debian VPS, Ubuntu VPS, and VPS CentOS. What is VNC? VNC (Virtual Network Computing) is a system for remote access to computers and servers based on the RFB (Remote FrameBuffer) protocol. Using a network connection, it transmits keyboard inputs and mouse movements from one machine to another. VNC is platform-independent and a cross-platform solution. VNC consists of a server and a client: the server provides access to the device's screen, and the client displays the server's screen. We will use TightVNC, which is open-source, optimized for slow connections, and widely supported by third-party VNC client programs. VNC vs. RDP While VNC and RDP both provide remote access, there are key differences. RDP is a proprietary protocol developed by Microsoft for Windows, while VNC is cross-platform, running on Windows, Linux/Unix, and macOS. VNC is open-source and free. RDP transmits a video stream using a capture device, displaying the remote desktop after the connection is initiated. VNC, however, sends pixel data directly. RDP includes built-in encryption and authentication integration with Windows, while VNC requires additional security configuration. RDP also supports device forwarding, file transfers, and peripheral access (e.g., USB drives and printers), while VNC primarily focuses on remote desktop functionality. Prerequisites To install and configure VNC, you'll need: A VPS running Ubuntu 22.04. A VNC client program installed on any operating system, as VNC is cross-platform. Some client programs are listed in the "Connecting to the VNC Server" section. Installing TightVNC and Xfce First, we'll install the TightVNC server and the Xfce desktop environment, which is lightweight and optimized for TightVNC. The following commands should be run as the root user or a user with sudo privileges. Update the package list and install the required packages: apt update && apt -y install xfce4 xfce4-goodies tightvncserver If you are using UFW, iptables, or another firewall tool, open port 5901 for VNC connections: For UFW: ufw allow 5901 You can also temporarily disable UFW for testing: systemctl stop ufw For iptables: To allow incoming connections on port 5901: iptables -I INPUT -p tcp --dport 5901 -j ACCEPT To allow outgoing connections on port 5901: iptables -I OUTPUT -p tcp --sport 5901 -j ACCEPT Configuring the TightVNC Server Once TightVNC is installed, we need to configure it. Set the password for accessing the remote host by running the vncserver command: vncserver The password should be between 6 and 8 characters. If it's longer, TightVNC will truncate it to 8 characters. You will be prompted to set a view-only password (optional). This password allows users to view the remote screen without controlling it. To set this password, type y and provide a password. If you don't need this feature, enter n. After running vncserver, you’ll see the following output: Creating default startup script /root/.vnc/xstartupStarting applications specified in /root/.vnc/xstartupLog file is /root/.vnc/[hostname]:1.log Stop the VNC server to configure it further: vncserver -kill :1 Backup the default configuration file before editing it: cp ~/.vnc/xstartup ~/.vnc/xstartup.bak Open the configuration file in a text editor: nano /root/.vnc/xstartup Add the following line to the end of the file: startxfce4 Save the changes and exit. Restart the VNC server: vncserver Managing TightVNC with systemd We’ll create a systemd service to manage TightVNC more easily. Create a new unit file: nano /etc/systemd/system/vncserver.service Add the following content: [Unit] Description=TightVNC server After=syslog.target network.target [Service] Type=forking User=root PAMName=login PIDFile=/root/.vnc/%H:1.pid ExecStartPre=-/usr/bin/vncserver -kill :1 > /dev/null 2>&1 ExecStart=/usr/bin/vncserver ExecStop=/usr/bin/vncserver -kill :1 [Install] WantedBy=multi-user.target Reload the systemd daemon: systemctl daemon-reload Enable the service to start on boot: systemctl enable --now vncserver Check the VNC server status: systemctl status vncserver If the status shows "active (running)," the server is running successfully. Connecting to the VNC Server There are various VNC client programs, both free and paid. Examples include UltraVNC and TightVNC Viewer for Windows, Remmina for Linux, and RealVNC for macOS. For example, to connect using TightVNC Viewer on Windows: Enter the server's IP address and port in the format: IP_address::port Note: TightVNC requires :: to separate the IP and port, whereas other programs may use :. When prompted, enter the password you set earlier. Once authenticated, the remote desktop will appear. TightVNC Viewer allows saving sessions for quick connections. Click the save icon, provide a name, and save the file with a .vnc extension. You can also save the password for easier future access. For increased security, it's recommended to use SSH tunnels when connecting over VNC. Conclusion VNC is a convenient system for remote access, often used for technical support or server maintenance. This guide provides a step-by-step process for installing and configuring TightVNC on an Ubuntu server and connecting to it from a remote machine. With simple setup steps, you can have a VNC server running in no time. Especially, if you use our low-latency US based VPS. If you want to know more about Hostman server solutions, you can check the most affordable VPS Servers.  Frequently Asked Questions (FAQ) What is the best VNC server for Linux?  For most users, TigerVNC or TightVNC are the best choices. TigerVNC: Known for speed and performance. TightVNC: Highly reliable and lightweight, great for low-bandwidth connections. RealVNC: Good for enterprise features but less common for open-source home labs. How do I install and configure VNC on Ubuntu?  The general process involves three steps: Install the Desktop: Ensure you have a desktop environment (like XFCE or GNOME) installed: sudo apt install xfce4. Install VNC Server: Run sudo apt install tigervnc-standalone-server. Configure: Run vncserver to set your password and generate the initial config files, then edit ~/.vnc/xstartup to tell VNC which desktop to launch. Is VNC better than RDP?  It depends on the use case. RDP (Remote Desktop Protocol): Generally offers better performance, audio support, and a smoother experience over slower networks because it transmits semantic instructions rather than just pixels. VNC (Virtual Network Computing): Is platform-independent (works on Mac, Linux, Windows, Android equally well) and uses a simpler "pixel-based" protocol, making it easier to troubleshoot across different systems. How do I check the status of VNC server in Ubuntu?  If you are running it manually, use: vncserver -list This will show all active display numbers and their process IDs. If you set it up as a systemd service, run: sudo systemctl status vncserver@1.service (adjusting the number to match your display ID). How do I find my VNC server address?  The address is your server's IP address followed by the port number. Find your IP: ip a (e.g., 192.168.1.50). Find your Port: Add 5900 to your display number. (Display :1 = Port 5901). Address: 192.168.1.50:5901. Why is my VNC screen blank or grey? This is the most common VNC error. It means the xstartup script is missing or has the wrong permissions. Ensure the file is executable (chmod +x ~/.vnc/xstartup) and contains the correct command to start your specific desktop session (e.g., startxfce4).
27 January 2026 · 7 min to read
Ubuntu

How to Install VNC on Ubuntu

If you need to interact with a remote server through a graphical interface, you can use VNC technology.Through a network, users can connect remotely to a server using VNC (Virtual Network Computing). It employs the RFB protocol to send screen images and input data from different devices (such keyboards and mice) and runs on a client-server architecture. Ubuntu, Windows, macOS, and other operating systems are among those that VNC supports. The ability to connect several users at once is another benefit of VNC, which can be helpful for group tasks or training sessions. Choose your server now! And if you’re looking for a reliable, high-performance, and budget-friendly solution for your workflows, Hostman has you covered with Linux VPS Hosting options, including Debian VPS, Ubuntu VPS, and VPS CentOS. In this guide, we will describe how to install VNC on Ubuntu, using a Hostman cloud server with Ubuntu 22.04 as an example. Finished installation of VNC on Ubuntu Step 1: Preparing to Install VNC Before starting the installation process on both the server and the local machine, there are a few prerequisites to review.  Here is a list of what you’ll need to complete the installation: A Server Running Ubuntu 22.04. In this guide, we will use a cloud server from Hostman with minimal hardware configuration. Hostman's plan selection in admin panel A User with sudo Privileges. You should perform the installation as a regular user with administrative privileges. Select a Graphical Interface. You’ll need to choose a desktop environment that you will use to interact with the remote server after installing the system on both the server and the local machine. A Computer with a VNC Client Installed.  At the moment, the console is the sole method of communication with a rented server running Ubuntu 22.04. You must install a desktop environment and VNC on the server in order to enable remote management through a graphical interface. The desktop environments and VNC servers that are compatible with Ubuntu servers are listed below. VNC Servers: TightVNC Server. One of the most popular VNC servers for Ubuntu. It is easy to set up and offers good performance. RealVNC Server. RealVNC provides a commercial solution for remote access to servers across various Linux distributions, including Ubuntu, Debian, Fedora, Arch Linux, and others. Desktop Environments: Xfce. A lightweight and fast desktop environment, ideal for remote sessions over VNC. It uses fewer resources than heavier desktop environments, making it an excellent choice for servers and virtual machines. GNOME. The default Ubuntu desktop environment, offering a modern and user-friendly interface. It can be used with VNC but will consume more resources than Xfce. KDE Plasma. Another popular desktop environment that provides a wide range of features and a beautiful design. The choice of VNC server and desktop environment depends on the user’s specific needs and available resources. TightVNC and Xfce are excellent options for stable remote sessions on Ubuntu, as they do not require high resources. In the next step, we will describe how to install them on the server in detail. Step 2: Installing the Desktop Environment and VNC Server To install the VNC server on Ubuntu along with the desktop environment, connect to the server and log in as a regular user with administrative rights. Update the Package List  After logging into the server, run the following command to update the packages from the connected repositories: sudo apt update Install the Desktop Environment  Next, install the previously selected desktop environment. To install Xfce, enter: sudo apt install xfce4 xfce4-goodies Here, the first package provides the basic Xfce desktop environment, while the second includes additional applications and plugins for Xfce, which are optional. Install the TightVNC Server  To install TightVNC, enter: sudo apt install tightvncserver Start the VNC Server  Once the installation is complete, initialize the VNC server by typing: vncserver This command creates a new VNC session with a specific session number, such as :1 for the first session, :2 for the second, and so on. This session number corresponds to a display port (for example, port 5901 corresponds to :1). This allows multiple VNC sessions to run on the same machine, each using a different display port. This command will ask you to create a password during the initial setup, which is necessary for users to access the server's graphical user interface. Don't forget to verify your password to run VNC on Ubuntu Set the View-Only Password (Optional)  After setting the main password, you’ll be prompted to set a password for view-only mode. View-only mode allows users to view the remote desktop without making any changes, which is helpful for demonstrations or when limited access is needed. If you need to change the passwords set above, use the following command: vncpasswd Now you have a VNC session. VNC on Ubuntu is running In the next step, we will set up VNC to launch the Ubuntu server with the installed desktop environment. Step 3: Configuring the VNC Server The VNC server needs to know which desktop environment it should connect to. To set this up, we’ll need to edit a specific configuration file. Stop Active VNC Instances  Before making any configurations, stop any active VNC server instances. In this guide, we’ll stop the instance running on display port 5901. To do this, enter: vncserver -kill :1 Simple command to stop VNC running on Ubuntu Here, :1 is the session number associated with display port 5901, which we want to stop. Create a Backup of the Configuration File  Before editing, it’s a good idea to back up the original configuration file. Run: mv ~/.vnc/xstartup ~/.vnc/xstartup.bak Edit the Configuration File  Now, open the configuration file in a text editor: nano ~/.vnc/xstartup Replace the contents with the following: #!/bin/bashxrdb $HOME/.Xresourcesstartxfce4 & #!/bin/bash: This line is called a "shebang," and it specifies that the script should be executed using the Bash shell. xrdb $HOME/.Xresources: This line reads settings from the .Xresources file, where desktop preferences like colors, fonts, cursors, and keyboard options are stored. startxfce4 &: This line starts the Xfce desktop environment on the server. Make the Configuration File Executable To allow the configuration file to be executed, use: chmod +x ~/.vnc/xstartup Start the VNC Server with Localhost Restriction Now that the configuration is updated, start the VNC server with the following command: vncserver -localhost The -localhost option restricts connections to the VNC server to the local host (the server itself), preventing remote connections from other machines. You will still be able to connect from your computer, as we’ll set up an SSH tunnel between it and the server. These connections will also be treated as local by the VNC server. The VNC server configuration is now complete. Step 4: Installing the VNC Client and Connecting to the Server Now, let’s proceed with installing a VNC client. In this example, we’ll install the client on a Windows 11 computer. Several VNC clients support different operating systems. Here are a few options:  RealVNC Viewer. The official client from RealVNC, compatible with Windows, macOS, and Linux. TightVNC Viewer. A free and straightforward VNC client that supports Windows and Linux. UltraVNC. Another free VNC client for Windows with advanced remote management features. For this guide, we’ll use the free TightVNC Viewer. Download and Install TightVNC Viewer Visit the official TightVNC website, download the installer, and run it. Download VNC from official website In the installation window, click Next and accept the license agreement. Then, select the custom installation mode and disable the VNC server installation, as shown in the image below. This is what you need to install Click Next twice and complete the installation of the VNC client on your local machine. Set Up an SSH Tunnel for Secure Connection To encrypt your remote access to the VNC server, use SSH to create a secure tunnel. On your Windows 11 computer, open PowerShell and enter the following command: ssh -L 56789:localhost:5901 -C -N -l username server_IP_address Make sure that OpenSSH is installed on your local machine; if not, refer to Microsoft’s documentation to install it. This command configures an SSH tunnel that forwards the connection from your local computer to the remote server over a secure connection, making VNC believe the connection originates from the server itself. Here’s a breakdown of the flags used: -L sets up SSH port forwarding, redirecting the local computer’s port to the specified host and server port. Here, we choose port 56789 because it is not bound to any service. -C enables compression of data before transmitting over SSH. -N tells SSH not to execute any commands after establishing the connection. -l specifies the username for connecting to the server. Connect with TightVNC Viewer After creating the SSH tunnel, open the TightVNC Viewer and enter the following in the connection field: localhost:56789 You’ll be prompted to enter the password created during the initial setup of the VNC server. Once you enter the password, you’ll be connected to the VNC server, and the Xfce desktop environment should appear. Stop the SSH Tunnel To close the SSH tunnel, return to the PowerShell or command line on your local computer and press CTRL+C. You found out how to install VNC on Ubuntu Conclusion This guide has walked you through the step-by-step process of setting up VNC on Ubuntu 22.04. We used TightVNC Server as the VNC server, TightVNC Viewer as the client, and Xfce as the desktop environment for user interaction with the server. We hope that using VNC technology helps streamline your server administration, making the process easier and more efficient. We're prepared more detailed instruction on how to create server on Ubuntu if you have some trouble deploying it. Or you can use our low-latency US based VPS! Choose your server now! Frequently Asked Questions (FAQ) How to install VNC server on Ubuntu via command line?  The most common lightweight server is TightVNC. To install it, open your terminal and run: Update lists: sudo apt update Install the package: sudo apt install tightvncserver Initialize it (and set a password) by running: vncserver How do I uninstall VNC server on Ubuntu?  To remove the software and your configuration files, follow these steps: Stop the VNC session: vncserver -kill :1 Remove the package: sudo apt remove tightvncserver --purge (Optional) Delete config files: rm -rf ~/.vnc Is VNC secure?  By default, no. VNC traffic is not encrypted, meaning passwords and keystrokes can be intercepted. It is highly recommended to tunnel your VNC connection through SSH rather than opening the VNC port (5901) directly to the internet. Why do I see a gray screen when I connect?  This "gray screen of death" usually means the VNC server doesn't know which desktop environment to load. You need to edit the ~/.vnc/xstartup file and add the command for your desktop (e.g., startxfce4 & for XFCE or gnome-session & for GNOME). Which port does VNC use?  VNC uses port 5900 + Display ID. Display :1 uses port 5901. Display :2 uses port 5902. You must ensure these ports are allowed on your firewall if you are not using an SSH tunnel. What is the difference between TigerVNC, RealVNC, and TightVNC? TightVNC: Lightweight, reliable, and great for slower connections. Very popular for Linux. TigerVNC: A high-performance fork of TightVNC, often faster on modern hardware. RealVNC: Often proprietary/commercial, offers cloud connectivity but is less common for open-source self-hosting.
21 January 2026 · 10 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support