Several methods of automatic login without password for Linux SSH

Source: Internet
Author: User

1. Automatic Ssh/scp method = =

A is a local host (that is, the machine used to control other hosts);
b is the remote host (that is, the controlled machine server), if the IP is 192.168.60.110;
Systems A and B are all Linux.

Run the command on a:
# ssh-keygen-t RSA (three consecutive returns, i.e. locally generated public and private keys, no password set)
# SSH [email protected] "mkdir. SSH" (need to enter a password)
# SCP ~/.ssh/id_rsa.pub [email protected]:.ssh/id_rsa.pub (need to enter a password)

command on B:
# Touch/root/.ssh/authorized_keys (if this file already exists, skip this one)
# cat/root/.ssh/id_rsa.pub >>/root/.ssh/authorized_keys (append id_rsa.pub content to Authorized_keys)

Back to a machine:

# SSH [email protected] (no password required, login successful)

If agent admitted failure to sign using the key error occurs

On the A machine:

# Ssh-add ~/.ssh/id_rsa



2. Control of n machines automatic login as described above
That requires n-pair keys (keys and public keys), and the Ssh-keygen command can change the name of the key pair, such as:
# ssh-keygen-t RSA
Generating public/private RSA key pair.
Enter file in which to save the key (/ROOT/.SSH/ID_RSA):/root/.ssh/id_rsa_192.168.60.110

So the private key and the public key name are: Id_rsa_192.168.60.110 and id_rsa_192.168.60.110.pub, and then append the contents of the Id_rsa_192.168.60.110.pub file to sever ~/. Ssh/authorized_keys file, finally, specify the local key locally with the-I parameter of the SSH command and log in:
# ssh-i/root/.ssh/id_rsa_192.168.60.110 [email protected]

The SCP is the same.
# scp-i/root/.ssh/id_rsa_192.168.60.110 filename [email protected]:/home/someone

Add two lines in the file. BASHRC, and do the same thing each time without typing such a long command:
Alias Sshcell= ' ssh-i/root/.ssh/id_rsa_192.168.60.110 [email protected] '
Alias scpcell= ' scp-i/root/.ssh/id_rsa_192.168.60.110 filename [email protected]:/home/someone '

In this way, type the instructions directly to implement SSH and SCP auto-login:
# Sshcell
# Scpcell


3. Automatic SSH/SCP Script
If you need to go from a, to B, then to C, then SSH and SCP two times, it is more troublesome.
SSH Automatic Login:
#!/usr/bin/expect-f
Set Timeout 30
Spawn ssh [email protected]
Expect "Password:"
Send "pppppp\r"
Expect "]*"
Send "ssh [email protected]\r"
Expect "Password:"
Send "pppppp\r"
Interact


SCP from a copy file to C:
#!/usr/bin/expect-f
Set Timeout 300
Set file [lindex $argv 0]
Spawn SCP $file [email Protected]:/home/weiqiong
Expect "Password:"
Send "pppppp\r"
Expect "]*"
Spawn ssh [email protected]
Expect "Password:"
Send "pppppp\r"
Expect "]*"
Send "SCP $file [email protected]:/home/weiqiong\r]
Expect "Password:"
Send "pppppp\r"
Expect "]*"
Exit
Interact

SCP copies files from C to a:
#!/usr/bin/expect-f
Set Timeout 300
Set file [lindex $argv 0]
Spawn ssh [email protected]
Expect "Password:"
Send "pppppp\r"
Expect "]*"
Send "SCP [email protected]:/home/weiqiong/$file. \ r]
Expect "Password:"
Send "pppppp\r"
Expect "]*"
Send "exit\r"
Expect "]*"
Spawn SCP [email protected]:/home/weiqiong/$file.
Expect "Password:"
Send "pppppp\r"
Interact

4. Establish SSH/SCP Channel
For example, my machine is a, the intermediary server is B, the target server is C
From a can ssh to B, from B can ssh to C, but a cannot directly SSH to C
Now showing the use of SSH channel technology to transfer files from a directly to C
1. ssh-l1234:c:22 [email protected]
Input B ' s password
(1234 is the idle port of native A, which requires root user rights on a machine, in fact, a channel is established on the native 1234 port)

2. Open a new console and type:
scp-p1234 filename [email protected]:
Input C ' s password

Several methods of automatic login without password for Linux SSH

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.