Detailed explanation of SSH password-free login configuration under Linux

Detailed explanation of SSH password-free login configuration under Linux

Assume there are two Linux servers A and B, and we want to be able to log in to the other server from one server via SSH without a password.

The information of the two servers is as follows:

Hostname IP address Password-free login username
server1 192.168.12.11 guest1
server2 192.168.12.12 guest2

Environment settings (root permissions)

1. Turn off firewall and SELinux

Redhat uses SELinux to enhance security. The way to disable it is:

a. Permanently modify the SELINUX=enforcing in the /etc/selinux/config file to SELINUX=disabled, then reboot.

b. Provisional effect
setenforce 0
To turn off the firewall:

a. Enable permanently: chkconfig iptables on
Disable: chkconfig iptables off

b. Temporary start: service iptables start
Shutdown: service iptables stop
The two servers need to be configured separately, and the firewall and SELinux need to be turned off.

2. Set the host name

Edit the /etc/sysconfig/network file using the command: vim /etc/sysconfig/network , and set the format to: HOSTNAME=[host name] .
Set the host name of server A to server1.

Set the host name of server B to server2.

3. Configure hosts

Edit the /etc/hosts file using the command: vim /etc/hosts , and add the following configuration to the hosts files of the two servers:

192.168.12.11 server1
192.168.12.12 server2 

4. Configure sshd

Edit the /etc/ssh/sshd_config file of both servers using the command: vim /etc/ssh/sshd_config .

Remove the “#” comments from the following 3 lines:

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

Restart the sshd service using the command: /sbin/service sshd restart .

Key Settings

1. Create a password-free login account

Use command:

useradd guest1 //Create a new user passwd guest1 //Set the new user login password 

Similarly, create a guest2 account in server2.

2. Generate a secret key

Switch from the root user to the account to be logged in without a password, use the command: su guest1.

Execute the command: ssh-keygen -t rsa

No password is required. Just press Enter. After the command is executed, two files will be generated in the guest1 user's home directory (/home/guest1/.ssh):

id_rsa: private key id_rsa.pub: public key 

Follow the same steps to generate a key file for the guest2 account in server2.

3. Import the public key into the authentication file

Use command:

cat /home/guest1/.ssh/id_rsa.pub >> /home/guest1/.ssh/authorized_keys
ssh guest2@server2 cat /home/guest2/.ssh/id_rsa.pub >> authorized_keys 

Use the command cat authorized_keys to view the contents of the authorized_keys file as follows:

4. Set file access permissions

Use command:

chmod 700 /home/guest1/.ssh
chmod 600 /home/guest1/.ssh/authorized_keys

After completing the above settings, server1 can log in to the local machine without a password, using the command: ssh guest1@server1.

Note: When the host name information is missing in the known_hosts file, the following message will be prompted. Enter yes to write the host name into the known_hosts file and log in successfully.

At this point, the SSH password-free login configuration of the host server1 is complete. Next, configure server2.

5. Copy the authentication file to other hosts

Execute the following command to copy the generated authorized_keys and known_hosts files from server1 to server2.

# scp [local file to be transferred] [remote host username]@remote host ip or host name:[destination location to which the file is to be transferred]
scp /home/guest1/.ssh/authorized_keys guest2@server2:/home/guest2/.ssh/authorized_keys
scp /home/guest1/.ssh/known_hosts guest2@server2:/home/guest2/.ssh/known_hosts 

After the copying is complete, use the following command to set file access permissions.

chmod 700 /home/guest2/.ssh
chmod 600 /home/guest2/.ssh/authorized_keys

Then, execute the ssh guest1@server1 command to use the guest2 account of server2 to log in to the guest1 account of server1 without a password.

At this point, the SSH password-free login of the two servers has been set up. If an error occurs, please check the above steps carefully.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • When a Linux (Ubuntu) user enters an incorrect password N times in a row to log in, the system will be automatically locked for X minutes
  • How to install binary MySQL on Linux and crack MySQL password
  • Forgot the root password in Linux? Enter single user mode Switch run level Switch user
  • Solution to forgetting the password of the pagoda panel in Linux 3.X/4.x/5.x

<<:  MySQL 8.0.12 Installation and Configuration Tutorial

>>:  No-nonsense quick start React routing development

Recommend

Detailed tutorial on installing Docker and nvidia-docker on Ubuntu 16.04

Table of contents Docker Installation Nvidia-dock...

Solution to Ubuntu 20.04 Firefox cannot play videos (missing flash plug-in)

1. Flash plug-in package download address: https:...

Detailed tutorial on installing MySQL 5.7.20 on RedHat 6.5/CentOS 6.5

Download the rpm installation package MySQL offic...

Several ways to implement 0ms delay timer in js

Table of contents queueMicrotask async/await Mess...

How to use js to communicate between two html windows

Scenario: When page A opens page B, after operati...

Raspberry Pi msmtp and mutt installation and configuration tutorial

1. Install mutt sudo apt-get install mutt 2. Inst...

Implementation example of JS native double-column shuttle selection box

Table of contents When to use Structural branches...

Element-ui directly clicks on the cell in the table to edit

Table of contents Achieve results Implementation ...

Nodejs combined with Socket.IO to realize websocket instant communication

Table of contents Why use websocket Socket.io Ope...

Docker container custom hosts network access operation

Adding the extra_hosts keyword in docker-compose....

How does Vue track data changes?

Table of contents background example Misconceptio...

How to create a flame effect using CSS

The main text starts below. 123WORDPRESS.COM Down...

Complete steps to use element in vue3.0

Preface: Use the element framework in vue3.0, bec...

The whole process of Vue page first load optimization

Table of contents Preface 1. Image Optimization 2...