How to set up ssh password-free login to Linux server

How to set up ssh password-free login to Linux server

Every time you log in to the test server, you always need to enter a password for ssh login. It's okay if there are few logins, but if there are too many logins, every extra line of command is redundant.

RSA authentication login method

Creating a key pair

Enter the following command in the client (local machine) terminal

ssh-keygen -t [rsa|dsa]

RSA and DSA represent different algorithms

For example:

ssh-keygen -t rsa

Just keep pressing Enter (no need to set a password)

The key file and private key file id_rsa, id_rsa.pub will be generated (if dsa is used, id_dsa, id_dsa.pub will be generated)

The generation location is in the /root/.ssh/ folder (I use the root user so under root, the generation process will prompt the file location)

.ssh is a hidden folder, use ls -a to view it

Put the public key in the server's specified location

Method 1: Direct copy

1. Copy the public key to the .ssh folder of the root user of the server (copy it to the .ssh folder of the user you log in as)

scp /root/.ssh/id_rsa.pub [email protected]:/root/.ssh/

2. Install the public key

Log in to the server cd /root/.ssh/

cat id_rsa.pub >> authorized_keys

Method 2: Use ssh-copy-id command to copy (recommended)

One command is OK

ssh-copy-id [email protected]

verify

If you do not enter a password, it will succeed, otherwise it will fail.

Precautions

The above operation has been tested and there is no problem

There will be differences depending on the version of Linux and the user used.

If there is a problem, consider the following two points

1. File permission issues for id_rsa.pub and authorized_keys

chmod 600 authorized_keys
chmod 700 ~/.ssh

2. ssh configuration file

 vim /etc/ssh/sshd_config
 #Enable RSA authentication, the default is yes
 RSAAuthentication yes
 Enable public key authentication, default is yes
 PubkeyAuthentication yes
 #root user ssh loginPermitRootLogin yes

(I have not modified these configurations, mine is redhat7.2)

Customize a simple shell script

Create a file in the common folder

touch 164.sh

Editing a file

File content ssh [email protected]

 vim 164.sh
 #Add content ssh [email protected]
 Save and exit: wq

Increase the user's execution permissions

chmod u+x 164.sh

It's more fun to use it with the tab key

./164.sh

Expect command password-free login method

Maybe you think it is not good to operate the server system, of course you can just operate it locally.

Using the expect command only requires local operation, provided that you have the expect command locally

1. Create a file

touch expectssh.sh

Add the following content and change the user, ip, and password to your own

 #!/usr/bin/expect -f
 set username root 
 set hostname 172.16.0.164 
 set password 123456
 spawn ssh $username@$hostname
 set timeout 1
 expect { "yes/no" 
  {send "yes\r";exp_continue}
 }
 expect "$username@$hostname's password:"
 send "$password\r"
 interact

2. Add execution permissions to expectssh.sh

chmod u+x expectssh.sh

3. Execute commands

expect expectssh.sh

(1. Must be executed using the expect command 2. You can wrap it in another layer and use bash parsing to execute)

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:
  • Tutorial on configuring SSH and Xshell to connect to the server in Linux (with pictures)
  • Linux server SSH cracking prevention method (recommended)
  • Install OpenSSH on Windows and log in to the Linux server by generating an SSH key
  • How to upload files and folders to Linux server via SSH
  • Paramiko module under Python implements ssh connection to log in to Linux server
  • Using winscp and batch processing under Windwos to upload files to Linux server through SSH port
  • Four tips for configuring secure SSH access on Linux servers
  • Linux ssh server configuration code example

<<:  Tutorial on how to use profile in MySQL

>>:  In-depth analysis of the diff algorithm in React

Recommend

3 ways to correctly modify the maximum number of connections in MySQL

We all know that after the MySQL database is inst...

A brief analysis of the configuration items of the Angular CLI release path

Preface Project release always requires packaging...

Detailed explanation of Javascript Echarts air quality map effect

We need to first combine the air quality data wit...

Simple implementation method of two-way data binding in js project

Table of contents Preface Publish-Subscriber Patt...

Docker completely deletes private library images

First, let’s take a look at the general practices...

XHTML Getting Started Tutorial: What is XHTML?

What is HTML? To put it simply: HTML is used to m...

Elementui exports data to xlsx and excel tables

Recently, I learned about the Vue project and cam...

innodb_flush_method value method (example explanation)

Several typical values ​​of innodb_flush_method f...

Common writing examples for MySQL and Oracle batch insert SQL

Table of contents For example: General writing: S...

HTML code example: detailed explanation of hyperlinks

Hyperlinks are the most frequently used HTML elem...

MySQL login and exit command format

The command format for mysql login is: mysql -h [...

Detailed explanation of binary and varbinary data types in MySQL

Preface BINARY and VARBINARY are somewhat similar...

Sample code for making a drop-down menu using pure CSS

Introduction: When I looked at interview question...

How to deploy SpringBoot project using Dockerfile

1. Create a SpringBooot project and package it in...