4 ways to implement SSH automatic login _linux shell

Source: Internet
Author: User
Tags ssh

1. Automatic Ssh/scp method

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

To run a command on a:
# ssh-keygen-t RSA (three consecutive returns, that is, the public and private keys are generated locally, no password is set)
# SSH root@192.168.60.110 "mkdir. SSH" (Requires a password)
# SCP ~/.ssh/id_rsa.pub root@192.168.60.110:.ssh/id_rsa.pub (need to enter password)

The Order on B:
# Touch/root/.ssh/authorized_keys (If this file is already present, 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 root@192.168.60.110 (no password required, login successful)

2. Control n machines as described above automatic login

That requires n-pair keys (keys and public keys), and the Ssh-keygen command can change the name of the key pair arbitrarily, 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

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 the contents of the Id_rsa_192.168.60.110.pub file are appended to the sever ~/. Ssh/authorized_keys file, and finally, locally use the SSH command-i parameter to specify the local key and log in:
# ssh-i/root/.ssh/id_rsa_192.168.60.110 someone@192.168.60.110

The SCP is the same.
# scp-i/root/.ssh/id_rsa_192.168.60.110 filename someone@192.168.60.110:/home/someone

Add two lines to the file. BASHRC, and do the same thing each time without typing this long command:
Alias Sshcell= ' ssh-i/root/.ssh/id_rsa_192.168.60.110 someone@192.168.60.110 '
Alias scpcell= ' scp-i/root/.ssh/id_rsa_192.168.60.110 filename someone@192.168.60.110:/home/someone '

In this way, type the instructions directly to implement SSH and SCP automatic logon:
# Sshcell
# Scpcell

3. Automatic SSH/SCP Script

If you need to from a, to B, and then to C, then need SSH and SCP two times, is more troublesome.
SSH Automatic Login:
#!/usr/bin/expect-f
Set Timeout 30
Spawn ssh Weiqiong@b
Expect "Password:"
Send "PPPPPPR"
Expect "]*"
Send "SSH weiqiong@cr"
Expect "Password:"
Send "PPPPPPR"
Interact

SCP copies files from a to C:
#!/usr/bin/expect-f
Set Timeout 300
Set file [lindex $argv 0]
Spawn SCP $file Weiqiong@b:/home/weiqiong
Expect "Password:"
Send "PPPPPPR"
Expect "]*"
Spawn ssh Weiqiong@b
Expect "Password:"
Send "PPPPPPR"
Expect "]*"
Send "SCP $file weiqiong@c:/home/weiqiongr"
Expect "Password:"
Send "PPPPPPR"
Expect "]*"
Exit
Interact

SCP copies files from C to a:
#!/usr/bin/expect-f
Set Timeout 300
Set file [lindex $argv 0]
Spawn ssh Weiqiong@b
Expect "Password:"
Send "PPPPPPR"
Expect "]*"
Send "SCP weiqiong@c:/home/weiqiong/$file. R"
Expect "Password:"
Send "PPPPPPR"
Expect "]*"
Send "Exitr"
Expect "]*"
Spawn SCP weiqiong@b:/home/weiqiong/$file.
Expect "Password:"
Send "PPPPPPR"
Interact

4. Establishment of SSH/SCP Channel

For example, my machine is a, intermediate server is B, target server is C
From a can ssh to B, from B can ssh to C, but a cannot ssh directly to C
Now demonstrates using SSH channel technology to transfer files directly from A to C

1. Ssh-l1234:c:22 userid@b
Input B ' s password
(1234 is a free port for native A, which requires root user rights on machine A, and actually establishes a channel on the native 1234 port)

2. Open a new console and type:
scp-p1234 filename Userid@localhost:
Input C ' s password

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.