How to Create SSH Server
Setting up a SSH (Secure Shell) server allows secured connections to your remote Linux systems, enabling you to execute commands and manage data as if you were physically sitting in front of the machine. This guide will walk you through the process of creating an SSH server on a Linux system using OpenSSH, a widely-used SSH protocol implementation.
Before we begin, ensure that you have a basic understanding of Linux commands and have access to a terminal or command line interface on your Linux system. This guide is tailored for Ubuntu and CentOS, but the general steps will be similar for other Linux distributions.
Setting Up the SSH Server
The first step in creating an SSH server involves installing the OpenSSH server software on your Linux system. You can accomplish this by running a single command in your terminal.
For Ubuntu, use the following command:
sudo apt update
sudo apt install openssh-server
For CentOS, use this command instead:
sudo yum update
sudo yum install openssh-server
After the installation, the SSH server will start automatically. To verify that the SSH service is running, use the following command:
sudo service ssh status
Configuring the SSH Server
The SSH server comes with a pre-set configuration, but you can customize it to enhance security and tailor it to your needs. The configuration file, named sshd_config, is located in the /etc/ssh/ directory. Edit this file using a text editor with root privileges:
sudo nano /etc/ssh/sshd_config
Here are some configuration options you might want to adjust:
- PasswordAuthentication: Set to
noto enforce public key authentication and increase security. - PermitRootLogin: Set to
noto prevent root login and enhance security. - Port: Change the default SSH port (
22) to a less common port to reduce the risk of brute-force attacks.
Restarting the SSH Server
After adjusting the configuration, restart the SSH server to apply the changes:
For Ubuntu:
sudo service ssh restart
For CentOS:
sudo service sshd restart
Or, to make changes take effect immediately, use the following command:
sudo systemctl reload sshd
Connecting to the SSH Server
Now that your SSH server is set up and configured, you can connect to it using any SSH client, such as the built-in SSH client in your Linux terminal or third-party clients like PuTTY for Windows.
To connect from your terminal, use the following command. Replace username with your Linux username, server_ip with the IP address of your SSH server, and ssh_port with the port number you chose in the configuration:
ssh username@server_ip -p ssh_port
Using SSH Keys for Authentication
To enhance security and convenience, you can use SSH keys for authentication instead of entering your password every time you connect. First, generate SSH key pairs on your client machine:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Then, copy the public key to your server:
ssh-copy-id username@server_ip
Now, you can connect to your SSH server using your SSH key for authentication:
ssh username@server_ip -p ssh_port
Congratulations! You've successfully created and configured an SSH server for secure connections to your remote Linux systems. Regularly update your server software and monitor your SSH server's access logs to ensure the security and stability of your SSH server.