Use OpenSSH to build an SSH server in centos (figure)

Source: Internet
Author: User
Tags ssh server

Use OpenSSH to build an SSH server in centos

Author: centos pub from: linux.chinaunix.net


Preface

Like the telnet service, the SSH service can remotely log on to the system and control the system. However, it differs from telnet in that Telnet transmits plain text while SSH encrypts the transmitted content, ensuring the confidentiality of the transmitted content during transmission, this improves the system security.

Here, we do not prepare the SSH service as a tool for uploading and downloading files. We only use the activation of the SSH service to facilitate the remote management system. In addition, in terms of user authentication, for the sake of server and user security, the user password authentication method is prohibited, and the "key" method is used.

Modify ssh-related configuration files

First, modify the SSH configuration file. As follows:

[Root @ sample ~] # Vi/etc/ssh/sshd_config using VI to open the SSH configuration file

# Protocol locate this row and delete the line header "#". Then, delete ", 1" at the end of the line, and only allow connections in SSH2 mode.
Bytes
The Protocol 2 protocol changes to this status after modification. Only SSH2 is used.

# Serverkeybits 768 rows find this line, remove "#" at the beginning of the line, and change 768 to 1024
Bytes
Changed serverkeybits 1024 bytes to this status, and changed the serverkey strength to 1024 bits.

# Permitrootlogin Yes locate this line, remove "#" at the beginning of the line, and change "yes" to "no ".
Bytes
Permitrootlogin no logs is changed to this status and cannot be logged on using root.

# Passwordauthentication Yes locate this line and change Yes to no
Bytes
Passwordauthentication No. login is changed to this status, and password-based logon is not allowed.

# Permitemptypasswords no longer find this row and delete the line header "#". Empty Password Logon is not allowed.
Bytes
Permitemptypasswords No. Changed to this status. Do not log on with a blank password.

Save and exit. (The command for saving and exiting VI is ZZ)

Because we only want to make the SSH service more convenient for the management system, we only allow the Intranet client to log on to the server through SSH without Remotely Managing the system over the Internet, to minimize unsafe factors. The setting method is as follows:

[Root @ sample ~] # Vi/etc/hosts. Deny blocks modify the blocking rule and add corresponding lines at the end of the text

#
# Hosts. deny this file describes the names of the hosts which are
# ** Not * allowed to use the local Inet services, as decided
# By the '/usr/sbin/tcpd' server.
#
# The Portmap line is redundant, but it is left to remind you that
# The New Secure Portmap uses hosts. Deny and hosts. Allow. In particle
# You shoshould know that NFS uses Portmap!

Sshd: all clients add this line to shield all SSH connection requests.

[Root @ sample ~] # Vi/etc/hosts. Allow allow modify permit rules and add corresponding lines at the end of the text

#
# Hosts. Allow this file describes the names of the hosts which are
# Allowed to use the local Inet services, as decided
# By the '/usr/sbin/tcpd' server.
#

Sshd: 192.168.0. Allow add this line, only allow SSH connection requests from the Intranet

Restart the SSH service

After modifying the SSH configuration file, you must restart the SSH service to make the new settings take effect.

[Root @ sample ~] #/Etc/rc. d/init. d/sshd restart the SSH server

Stopping sshd: [OK]
Starting sshd: [OK] the restart SSH server is successfully restarted.

At this time, on the remote terminal (Personal PC, etc.), you cannot log on to the server by using the SSH client software with a normal password. In order for the customer to log on to the server, we will create a public key and private key for SSH to log on to the SSH server as a "key" for the client.

Establishment of the public key and private key of SSH2

Logon is a general user who establishes a public key and a private key based on this user. (The following uses the centospub user as an example)

[Root @ sample ~] # Centospub login: general user centospub

[Centospub @ sample ~] $ Ssh-keygen-t rsa keystore creates a public key and a private key
Generating public/private RSA key pair.
Enter file in which to save the key (/home/KAZ/. Ssh/id_rsa): the name of the keystore key file. Press enter by default.
Created directory '/home/KAZ/. Ssh'
Enter passphrase (empty for no passphrase): enter your password
Enter same passphrase again: enter the password again
Your identification has been saved in/home/KAZ/. Ssh/id_rsa.
Your public key has been saved in/home/KAZ/. Ssh/id_rsa.pub.
The key fingerprint is:
TF: RS: E3: 7 S: 28: 59: 5 S: 93: Fe: 33: 84: 01: CJ: 65: 3B: 8e centospub@sample.centospub.com

Then confirm the establishment of the public key and the key, and some processing corresponding to the client.

[Centospub @ sample ~] $ Cd ~ /. Enter the directory of the user's SSH configuration file.

[Centospub @ sample. Ssh] $ LS-l volumes List objects
Total 16
-RW ------- 1 centospub 951 Sep 4 id_rsa keys confirm that the private key has been created
-RW-r -- 1 centospub 241 Sep 4 id_rsa.pub secret confirm that the public key has been created

[Centospub @ sample. Ssh] $ cat ~ /. Ssh/id_rsa.pub> ~ /. Ssh/authorized_keys export public key content to the corresponding file

[Centospub @ sample. Ssh] $ Rm-f ~ /. Ssh/id_rsa.pub keystore Delete the original public key file

[Centospub @ sample. Ssh] $ chmod 400 ~ /. Ssh/authorized_keys secret set the attribute of the new public key file to 400

Then, the private key is securely transferred to the PC on which you want to connect to the server through SSH. The following uses a 3.5-inch disk as the media:

Centospub @ sample. Ssh] $ exit quit normal user logon (return to root logon)

[Root @ sample ~] # Mount/mnt/floppy/ephemeral mount the floppy drive

[Root @ sample ~] # Mv/home/centospub/. Ssh/id_rsa/mnt/floppy/volumes move the created private key to a floppy disk

[Root @ sample ~] # Umount/mnt/floppy/uninstall detach a floppy disk drive

In this way, we can connect to the server through the SSH client on a remote terminal through the private key corresponding to the centospub user. However, the private key generated by the server cannot be directly applied by the client... For more information, see the next section.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.