C1V Hosting

How to remove the GUI and desktop environment from a Debian server

To turn a Debian desktop install into a command-line server, set the default boot target to multi-user.target, purge the desktop task package and the display manager, then run apt autoremove. Work over SSH, simulate every removal with apt-get -s first, and back up /etc and your data before you start.

On this page
  1. What you are removing
  2. Step 0: check what is installed
  3. Step 1: boot to the text target
  4. Step 2: purge the desktop packages
  5. Step 3: clean up leftovers
  6. Step 4: verify
  7. How to go back
  8. Frequently asked questions

Updated

At a glance

  • Works on Debian 12 (bookworm) and Debian 13 (trixie)
  • Reversible first step: systemctl set-default multi-user.target
  • Simulate with apt-get -s before purging
  • Run it from SSH or the console, not from the desktop session

What you are removing

A Debian graphical install is a stack: a display manager (gdm3, sddm or lightdm) that shows the login screen, a desktop environment (GNOME, KDE Plasma, Xfce, MATE and others), the X.org or Wayland display server, and a set of desktop applications. A server does not need any of it, and it costs RAM, disk space and attack surface.

systemd decides whether the graphical stack starts through the default target. graphical.target starts the display manager, multi-user.target is the text-mode multi-user system. Switching the target is the quick and reversible part; purging the packages is the permanent part.

If the machine was installed from a minimal or cloud image, there may be nothing to remove. Check first, as shown in the next step.

Step 0: check what is installed

Open an SSH session (or the provider console). If you are connected through the desktop session itself, stopping it will close your terminal. Record a baseline so you can compare afterwards:

systemctl get-default
systemctl status display-manager --no-pager
free -h
df -h /
dpkg -l | awk '/^ii/ && $2 ~ /^(task-.*-desktop|task-desktop|gdm3|sddm|lightdm)$/ {print $2}'

The last command lists the desktop task packages and display managers that are installed. If it prints nothing and systemctl get-default already says multi-user.target, the machine is already headless.

Step 1: boot to the text target

sudo systemctl set-default multi-user.target
sudo systemctl isolate multi-user.target

The first command changes what the next boot starts. The second stops the graphical session immediately, so run it only from SSH. This step alone frees most of the memory the desktop was using, and you can undo it at any time with sudo systemctl set-default graphical.target.

Step 2: purge the desktop packages

Collect the packages found in step 0, simulate the removal and read the list. Make sure openssh-server, sudo and the services you actually run (nginx, MariaDB, Docker and so on) are not in the list of packages to be removed:

PKGS=$(dpkg -l | awk '/^ii/ && $2 ~ /^(task-.*-desktop|task-desktop|gdm3|sddm|lightdm)$/ {print $2}')
echo $PKGS
sudo apt-mark manual openssh-server sudo
sudo apt-get -s purge --autoremove $PKGS

apt-mark manual tells apt that you want to keep those two packages even if a desktop package had pulled them in. The -s flag only simulates. If the simulation looks right, run it for real:

sudo apt-get purge --autoremove $PKGS

If $PKGS is empty, skip this step: running purge with no package names would only autoremove unrelated unused packages.

Step 3: clean up leftovers

sudo apt-get autoremove --purge
sudo apt-get clean
dpkg -l | awk '/^rc/ {print $2}' | xargs -r sudo dpkg --purge

The last line purges packages that were removed but left configuration files behind (status rc). Optionally, deborphan lists orphaned libraries:

sudo apt-get install deborphan
deborphan

Review the list before removing anything: libraries that you built software against may appear as orphans. Older guides, including the first version of this article, ran apt-get remove --purge x11-common followed by deborphan | xargs dpkg -P. Purging x11-common does remove every package that depends on it, but it is a blunt tool and it can take wanted packages with it, so simulate it with -s if you prefer that route.

Step 4: verify

systemctl get-default
systemctl is-active display-manager
free -h
df -h /
dpkg -l | grep -E 'xserver-xorg|gnome-shell|plasma-desktop|xfce4-session'
sudo reboot

The default target should be multi-user.target, the display manager should be inactive or not found, and the last dpkg command should print nothing. Compare free -h and df -h with your baseline. After the reboot, log in over SSH again to confirm that the server comes up without the desktop.

How to go back

Install the task package for the desktop you want and restore the graphical target:

sudo apt-get install task-xfce-desktop
sudo systemctl set-default graphical.target

Other task packages follow the same pattern (task-gnome-desktop, task-kde-desktop, task-mate-desktop). Your files in /home are not touched by any of these commands, but per-user desktop settings stay there too.

Frequently asked questions

Can I remove the GUI over SSH?

Yes, and it is the recommended way. Run systemctl set-default multi-user.target and the apt purge commands from an SSH session; only the local graphical session is affected.

Will removing the GUI delete my files?

No. Purging desktop packages does not touch /home or your application data. Take a backup anyway before changing a server.

What is the difference between multi-user.target and graphical.target?

graphical.target starts everything in multi-user.target plus the display manager and the graphical session. multi-user.target is the text-mode system that a server normally runs.

How much RAM and disk will I save?

It depends on the desktop environment and on what was running. Compare the output of free -h and df -h before and after, as shown in steps 0 and 4.

Do the same steps work on Debian 12 and Debian 13?

Yes. The systemd target commands, apt options and task-* package names used here exist on both releases.

C1V Hosting is a brand of Lumanex S.r.l., Rome, Italy.