Home  >  Article  >  Operation and Maintenance  >  How to configure an SSH server on Linux

How to configure an SSH server on Linux

WBOY
WBOYOriginal
2023-07-05 10:34:3810512browse

How to configure SSH server on Linux

Overview:
SSH (Secure Shell) is a secure and encrypted network transmission protocol, which is widely used in Linux systems. By configuring the SSH server, we can remotely access the server and perform secure file transfers and execute remote commands. This article will explain how to configure an SSH server on a Linux system and provide some practical code examples.

Step 1: Install OpenSSH server
Before starting to configure the SSH server, we need to ensure that the OpenSSH server software has been installed on the system. Most Linux distributions will have an OpenSSH server installed by default, but just to be sure, you can check and install it using the following command:

$ sudo apt update
$ sudo apt install openssh-server

Step 2: Configure SSH Server
The configuration file for the SSH server is located in / etc/ssh/sshd_config. You can use a text editor (such as vi, nano, etc.) to open the file for configuration.

2.1 Change the port that the SSH server listens on (optional)
By default, the port that the SSH server listens on is 22. For added security, you can choose to change to another unused port. Find the following line in the configuration file, comment it out (add # in front of the line), and add the new port number.

#Port 22
Port 12345

2.2 Prohibit Root login (recommended)
Prohibiting root users from logging in directly is a good security measure. Find the following line in the configuration file and make sure it is set to "no".

PermitRootLogin no

2.3 Set the users allowed to access
You can use the AllowUsers parameter in the configuration file to define the users allowed to access the SSH server. Multiple users can be specified, separated by spaces.

AllowUsers user1 user2

2.4 Other configuration options (optional)
There are other configuration options that can be set in the configuration file, such as login timeout, maximum number of login attempts, etc. It can be adjusted according to your needs. For specific configuration options, please view the OpenSSH documentation.

Step 3: Restart the SSH server
After completing the configuration, you need to restart the SSH server for the configuration to take effect. You can use the following command to restart the SSH server:

$ sudo service ssh restart

Step 4: Connect to the SSH server
After the configuration is completed, we can use the SSH client to connect to the SSH server. You can use tools like PuTTY on Windows systems, or the Terminal on Linux and Mac systems.

The basic command to connect to the SSH server is as follows:

$ ssh username@server_ip -p port_number

Among them, username is the user name on the SSH server, server_ip is the IP of the SSH server Address, port_number is the port number configured for the SSH server.

For example, if the user name is user1, the SSH server IP is 192.168.0.100, and the port number is 12345, connect SSH The server command is as follows:

$ ssh user1@192.168.0.100 -p 12345

After the connection is successful, you will be prompted to enter the user password. After entering the correct password, you can log in to the SSH server.

Summary:
Configuring the SSH server allows us to better manage and remotely access the Linux system. This article explains the basic steps for configuring an SSH server on Linux and provides relevant code examples. By properly configuring the SSH server, we can ensure the security of the system and facilitate remote management and file transfer. Hope this article is helpful to everyone.

The above is the detailed content of How to configure an SSH server on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn