Hello everyone! Today, I’d like to share a small security tip for those who operate Linux servers. It’s about SSH’s X11 Forwarding feature, and we’ll explore how to use it safely, step by step.

π€ What is X11 Forwarding?
X11 Forwarding is a very convenient feature that allows you to display GUI (Graphical User Interface) applications running on a remote server on your local computer screen. πΌοΈβ‘οΈπ»
For example, you can use a graphic editor or browser installed on the server as if you were running it directly on your own computer. However, this convenient feature can also be a security vulnerability. If an unauthorized user exploits this feature, it could lead to unnecessary risks for your server.
Therefore, we will enhance security by “disabling it by default, but allowing it only for specific users who absolutely need it.”
Step 1: Disable X11 Forwarding for the entire server π‘οΈ
The first thing to do is to change the default settings of your SSH server to disable X11 Forwarding for all users. This raises your server’s baseline security level.
sudo sed -i -E 's/^[#s]*X11Forwardings+yes/X11Forwarding no/i' /etc/ssh/sshd_config
Does this command look a bit complicated? Let’s break it down one by one.
- sudo: Executes the command with administrator privileges. π This is necessary because we’re modifying system configuration files.
- sed -i -E ‘…’: sed is a powerful tool for editing text.
- -i: Modifies the file directly (in-place) π
- -E: Uses extended regular expressions to find more flexible patterns.
- ‘s/find_pattern/replace_pattern/i’: This means to change ‘find_pattern’ to ‘replace_pattern’.
- ^[#s]*X11Forwardings+yes: This part is key.
- ^: Start of the line
- [#s]*: Zero or more occurrences of # (comment) or whitespace (s). This means it finds both commented-out lines ( #X11Forwarding yes) and active lines (X11Forwarding yes).
- X11Forwardings+yes: Finds the string X11Forwarding, followed by one or more spaces, and then yes.
- X11Forwarding no: Replaces the found content with this string.
- i: Case-insensitive.
- /etc/ssh/sshd_config: The path to the main configuration file for the SSH daemon. π
In conclusion, this command means: “Find any instances of X11Forwarding set to yes or commented out in the sshd_config file and definitively change them to no!” Now, the server’s default policy is to not allow X11 Forwarding.
Step 2: Allow X11 Forwarding only for specific users β
Now that we’ve disabled it globally, let’s allow it exceptionally for specific users who absolutely need this feature. Here, we’ll use ‘bob’ as an example user.
sudo tee -a /etc/ssh/sshd_config > /dev/null <<EOT
Match User bob
X11Forwarding yes
EOT
This command adds new settings to the very end of the sshd_config file.
- sudo tee -a /etc/ssh/sshd_config: The tee command takes input and outputs it simultaneously to a file and the screen.
- -a option: Appends content to the end of the file instead of overwriting it. β
- > /dev/null: The output that tee would normally print to the screen is redirected to /dev/null (aka ‘the black hole’), so nothing is displayed in the terminal. π€«
- <
- Match User bob: This specifies that this configuration block applies only to the user named ‘bob’. π€
- X11Forwarding yes: For the ‘bob’ user only, X11 Forwarding is set to yes, enabling the feature.
Now, only the ‘bob’ user can use the X11 Forwarding feature when connecting via SSH, and all other users will not be able to use this feature according to the server’s default policy.
Don’t forget! Restart the SSH service π
After changing the configuration files, you must restart the SSH service or reload its configuration for the changes to take effect.
# Use one of the following commands depending on your system.
sudo systemctl restart sshd
# or
sudo service sshd restart
Now your server is even more secure! β¨ Blocking unnecessary features by default and granting minimal privileges only to those who need them β this is the first step in security.
Leave a Reply