Home >Operation and Maintenance >Linux Operation and Maintenance >Password-free login under Linux (super detailed)

Password-free login under Linux (super detailed)

不言
不言Original
2018-06-05 14:56:285637browse

This article mainly introduces the relevant information on password-free login under Linux. It is very good and has reference value. Friends who are interested should take a look together

1.Linux Generate keys under

ssh-keygen command manual, through the "man ssh-keygen" command:

Through the command" ssh-keygen -t rsa"

## After generation, a ".ssh" folder will be generated in the user's root directory

Entering ".ssh" will generate the following files

Authorized_keys: stores the public key for remote password-free login. This file is mainly used to record the information of multiple machines. Public key


id_rsa: generated private key file


id_rsa.pub: generated public key file


know_hosts: known Host public key list

If you want the ssh public key to be effective, at least the following two conditions must be met:

1) The permissions of the .ssh directory must be 700


2 ) .ssh/authorized_keys file permission must be 600

2. Remote password-free login

Schematic diagram:

The following methods are commonly used:

 

2.1 Through ssh-copy-id

Command:

ssh-copy-id -i ~/.ssh/id_rsa.put fe1be4b6c1a31021ce15f9d3346405e5

Example:

[root@test .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.91.135 
root@192.168.91.135's password: 
Now try logging into the machine, with "ssh '192.168.91.135'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@test .ssh]# ssh root@192.168.91.135
Last login: Mon Oct 10 01:25:49 2016 from 192.168.91.133
[root@localhost ~]#

Common errors:

[root@test ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.91.135

   -bash: ssh-copy-id: command not found //Prompt command does not exist

   

Solution: yum -y install openssh-clients

 2.2 Write the content to the other party's file through scp

Command:

scp -p ~/.ssh/id_rsa.pub root@721b019b2c3c3f62b979c6ee04afb641:/root/.ssh/authorized_keys

Example:

[root@test .ssh]# scp -p ~/.ssh/id_rsa.pub root@192.168.91.135:/root/.ssh/authorized_keys
root@192.168.91.135's password: 
id_rsa.pub 100% 408 0.4KB/s 00:00 
[root@test .ssh]# 
[root@test .ssh]# 
[root@test .ssh]# 
[root@test .ssh]# ssh root@192.168.91.135
Last login: Mon Oct 10 01:27:02 2016 from 192.168.91.133
[root@localhost ~]#

can also be divided into two steps:

$ scp ~/.ssh/id_rsa.pub root@721b019b2c3c3f62b979c6ee04afb641:pub_key //Copy the file to the remote server

$ cat ~/pub_key >>~/.ssh/authorized_keys //Copy the content Append to the authorized_keys file, but you need to log in to the remote server to execute this command

  

##2.3 Implement batch password exemption through Ansible

2.3.1 Add the machine hosts that require password-free operations to /etc/ansible/hosts:

 [Avoid close]

 192.168.91.132

 192.168.91.133
192.168.91.134

2.3.2 Execute the command for password-free operation

ansible 4179fd5bdbf839b03206a46852a78368 -m authorized_key -a "user=root key='{{ lookup ('file','/root/.ssh/id_rsa.pub') }}'" -k

Example:

[root@test sshpass-1.05]# ansible test -m authorized_key -a "user=root key='{{ lookup('file','/root/.ssh/id_rsa.pub') }}'" -k
  SSH password: ----->输入密码
  192.168.91.135 | success >> {
  "changed": true, 
  "key": "ssh-rsa    AAAAB3NzaC1yc2EAAAABIwAAAQEArZI4kxlYuw7j1nt5ueIpTPWfGBJoZ8Mb02OJHR8yGW7A3izwT3/uhkK7RkaGavBbAlprp5bxp3i0TyNxa/apBQG5NiqhYO8YCuiGYGsQAGwZCBlNLF3gq1/18B6FV5moE/8yTbFA4dBQahdtVP PejLlSAbb5ZoGK8AtLlcRq49IENoXB99tnFVn3gMM0aX24ido1ZF9RfRWzfYF7bVsLsrIiMPmVNe5KaGL9kZ0svzoZ708yjWQQCEYWp0m+sODbtGPC34HMGAHjFlsC/SJffLuT/ug/hhCJUYeExHIkJF8OyvfC6DeF7ArI6zdKER7D8M0SM  WQmpKUltj2nltuv3w== root@localhost.localdomain", 
  "key_options": null, 
  "keyfile": "/root/.ssh/authorized_keys", 
  "manage_dir": true, 
  "path": null, 
  "state": "present", 
  "unique": false, 
  "user": "root"
  }
  [root@test sshpass-1.05]#

2.4 Manual copy and paste method Copy the contents of the local id_rsa.pub file to the ~/.ssh/authorized_keys file of the remote server

The above is the detailed content of Password-free login under Linux (super detailed). 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