Enable SSH Server on Debian 11
2022/06/24 by John
SSH is a protocol used to administer a remote computer from the command
line. It is widely used to remotely manage Linux desktops and servers.
This article serves as a guide on how to install the OpenSSH SSH server
on Debian 11 and how to configure it. Let’s get started.
Topic Contents
Installing OpenSSH SSH Server
Checking the SSH Server Status
Starting and Stopping the SSH Server
Adding and Removing the SSH Service from the System
Startup
Accessing the SSH Server
Configuring the SSH Server
Enabling Root Access for the SSH Server
Changing the Port of the SSH Server
Conclusion
Installing OpenSSH SSH Server
The OpenSSH SSH server package is available in the official package
repository of Debian 11, so it is very easy to install.
First, update the APT package repository cache of your Debian 11
desktop/server with the following command:
$ sudo apt update
To install the OpenSSH SSH server on Debian 11, run the following
command:
$ sudo apt install openssh-server
To confirm the installation, press Y and then press <Enter>.
After confirming, the OpenSSH SSH server and all the required
dependency packages will then be installed. It takes a few seconds to
complete the installation process.
At this point, the OpenSSH SSH server should be successfully installed
in the system.
Checking the SSH Server Status
You can check if the SSH server is running and if it is added to the
system startup (so that it automatically starts on boot) with the
following command:
$ sudo systemctl status ssh
As you can see, the SSH server is enabled1. It means that it’s added to
the system startup and it will automatically start on boot.
If the SSH server is inactive (dead)2, it means that it’s not running
at the moment. If you see an active (running) status, it means that the
SSH server is running.
Starting and Stopping the SSH Server
You can start the OpenSSH SSH server using the following command:
$ sudo systemctl start ssh
Here, you can see that the SSH server is active/running1. It’s
listening on port 22 for SSH connections2.
If you want to stop the OpenSSH SSH server, run the following command:
$ sudo systemctl stop ssh
Adding and Removing the SSH Service from the System Startup
You can add the SSH service to the system startup of Debian 11 using
the command provided below to start the OpenSSH SSH server
automatically on system boot.
$ sudo systemctl enable ssh
If you don’t want to start the OpenSSH SSH server automatically on
system boot, you can remove the SSH service from the system startup of
Debian 11.
$ sudo systemctl disable ssh
Accessing the SSH Server
To access the OpenSSH SSH server, you will need to know the IP address
of your Debian 11 desktop/server.
You can run the following command on your Debian 11 desktop/server to
find its IP address.
$ hostname -I
The IP address of my Debian 11 desktop is 192.168.0.115 as you can see
in the screenshot below. It is different from yours, so make sure to
input your desktop/server IP address.
You will also need to know the login username of your Debian 11
desktop/server. You can find it using the following command:
$ whoami
The login username of my Debian 11 desktop is shovon as you can see in
the screenshot below. Yours is different, so make sure to input your
own login username.
Once you know the login username and IP address of your Debian 11
desktop/server, you can connect to your Debian 11 desktop/server
remotely via SSH as follows:
$ ssh <username>@<ip-addr>
NOTE: Make sure to replace <username> and <ip-addr> with
the login username and IP address of your Debian 11 desktop/server
respectively.
In my case, the command is:
$ ssh shovon@192.168.0.115
Type in Yes and press <Enter> to confirm the fingerprint.
Type in the password of your login user and press <Enter>.
You should be logged in to your Debian 11 desktop/server remotely via
SSH.
You can run any commands here for managing and monitoring your Debian
11 desktop/server remotely.
Once you’re done, you can close the SSH session with the following
command:
$ exit
Configuring the SSH Server
The configuration files of the OpenSSH SSH server are in the /etc/ssh
directory. The main OpenSSH SSH server configuration file is
sshd_config as you can see in the screenshot below.
To configure the SSH server, you can open the sshd_config file with the
nano text editor as follows:
$ sudo nano /etc/ssh/sshd_config
The OpenSSH SSH server configuration file sshd_config should be opened
with the nano text editor.
Make the necessary changes here. Once you’re done, press <Ctrl> +
X followed by Y and <Enter> to save the configuration file.
Every time you make any changes to the sshd_config file, you will have
to restart the OpenSSH SSH server with the following command:
$ sudo systemctl restart ssh
To learn about all the available options and what they are used for,
you can read the manpage of the sshd_config configuration file.
To open the manpage of the sshd_config configuration file, run the
following command:
$ man sshd_config
The manpage of the sshd_config configuration file should be opened.
Scroll up and down the manpage to find the information you need to
configure the OpenSSH SSH server.
Enabling Root Access for the SSH Server
By default, you won’t be able to access the OpenSSH SSH server as root.
If you need to log in to the SSH server as the root user, you will have
to enable it from the sshd_config file.
Open the sshd_config file with the nano text editor as follows:
$ sudo nano /etc/ssh/sshd_config
Find the PermitRootLogin option as marked in the screenshot below. It
is commented out by default.
Uncomment the PermitRootLogin option and set it to yes as marked in the
screenshot below.
Once you’re done, press <Ctrl> + X followed by Y and
<Enter> to save the sshd_config file.
For the changes to take effect, restart the OpenSSH SSH server with the
following command:
$ sudo systemctl restart ssh
Now, you have to set a root password to enable the root user account on
your Debian 11 desktop/server.
To set a root password and enable the root user account, run the
following command:
$ sudo passwd
Type in your desired root password and press <Enter>.
Retype your root password and press <Enter>.
A root password should be set and the root account should be enabled.
You can log in to your Debian 11 desktop/server remotely via SSH as the
root user as follows:
$ ssh root@<ip-addr>
NOTE: Make sure to replace <ip-addr> with the IP address of your
Debian 11 desktop/server.
In my case, the command is:
$ ssh root@192.168.0.115
Type in the login password of the root user and press <Enter>.
You should be logged in to the Debian 11 desktop/server as the root
user.
You can run any command you want on your Debian 11 desktop/server from
here.
Once you’re done, you can close the SSH session as follows:
# exit
Changing the Port of the SSH Server
At times, for security reasons, you will want to change the default SSH
server port 22 to something like 2222.
To do that, open the sshd_config file with the nano text editor as
follows:
$ sudo nano /etc/ssh/sshd_config
You will find the Port option on top of the file as marked in the
screenshot below.
Uncomment the Port option and set it to 2222 as marked in the
screenshot below to configure the SSH server to listen on port 2222.
Once you’re done, press <Ctrl> + X followed by Y and
<Enter> to save the sshd_config file.
For the changes to take effect, restart the OpenSSH SSH server with the
following command:
$ sudo systemctl restart ssh
Once you’ve changed the SSH server port, you will have to use the -p
option followed by the port number while connecting to the SSH server
remotely as follows:
$ ssh <username>@<ip-addr> -p <port-number>
NOTE: Replace <port-number> with the port number of the SSH
server.
In my case, the command is:
$ ssh shovon@192.168.0.115 -p 2222
Type in your login password and press <Enter>.
You should be logged in to your Debian 11 desktop/server remotely via
SSH.
Conclusion
This article guides you on how to install the OpenSSH SSH server on
your Debian 11 desktop/server, how to access the SSH server to
manage/monitor your Debian 11 desktop/server remotely via SSH, and how
to configure the SSH server to enable root access and change the SSH
port as well.