SSH remote execution script without password

Source: Internet
Author: User

SSH login with no password and remote execution script to use public and private keys. Linux can use Ssh-keygen to generate public/private key pair, below I take CentOS7 as an example.

Test environment: Machine A (10.0.224.80); machine B (192.168.7.172). Now want to a through SSH password-free on B to execute the command remotely.

1, first in a machine to generate a public/private key pair: (-p for password,-p ' means null password, a return can be.) In ~/.ssh will generate Id_rsa and id_rsa.pub two files)

[Email protected] home]#Ssh-keygen-T Rsa-p"'Generating Public/private RSA key pair. Enterfile inch whichTo save the key (/root/.SSH/Id_rsa): (direct carriage return)Your identification has been savedinch/root/.SSH/Id_rsa. Your public key has been savedinch/root/.SSH/id_rsa.pub.The Key fingerprint is:e8:a1:c0:cc:d 0:2c: the: the: *: 0f:c7: the: 2b: -: -: the[email protected]the key's Randomart image is:+--[RSA2048]----+|       +...O E. ||  ++ =.       * . || B O. o O | |.  *     +         ||   = O S | | .         O.    || .          .                 ||                 || |+-----------------+
[Email protected] home]# CD ~/.ssh/
[email protected]. ssh]# LL
Total Dosage 12
-RW-------. 1 root root 1675 April 5 13:27 Id_rsa
-rw-r--r--. 1 root root 395 April 5 13:27 id_rsa.pub
-rw-r--r--. 1 root root 2492 April 5 10:52 known_hosts


2, copy the Id_rsa.pub under the A machine to the B machine

[Email protected]. SSH SCP ~/. ssh/id_rsa.pub [email protected]192.168. 7.172:/home[email protected]192.168. 7.172 '  id_rsa.pub                                                                     395     0. 4kb/s   :  XX    


3, on the B machine, will be copied from a machine id_rsa.pub added to the ~/.ssh/authorzied_keys file, and give permission

cat /home/id_rsa.pub >> ~/. ssh/chmod ~/. ssh/authorized_keys

4, test (note, the first time you need to enter a Yes manually)

Execute remote command

[Email protected] home]#SSH[Email protected]192.168.7.172 "cd/home; LS"Calico.TarLaizynagiosubuntu.Tar[email protected] home]#SSH[Email protected]192.168.7.172 "ifconfig ens33"Ens33:flags=4163<UP,BROADCAST,RUNNING,MULTICAST> MTU theinet192.168.7.172Netmask255.255.255.0Broadcast192.168.7.255Inet6 fe80::20c:29ff:fecd:7e7c Prefixlen -ScopeID0x20<link>etherxx: 0c: in: cd:7e:7c Txqueuelen +(Ethernet) RX packets216165bytes20851014(19.8MiB) RX Errors0Dropped0Overruns0Frame0TX Packets198202bytes19741868(18.8MiB) TX Errors0Dropped0Overruns0Carrier0Collisions0[email protected] home]#


Password-free, Telnet

[Email protected] home]#SSH[Email protected]192.168.7.172 LastLogin: Tue APR5  A:Geneva: -  .From192.168.7.146[[Email protected]~]#ifconfigEns33ens33:flags=4163<UP,BROADCAST,RUNNING,MULTICAST> MTU theinet192.168.7.172Netmask255.255.255.0Broadcast192.168.7.255Inet6 fe80::20c:29ff:fecd:7e7c Prefixlen -ScopeID0x20<link>etherxx: 0c: in: cd:7e:7c Txqueuelen +(Ethernet) RX packets217411bytes20974871(20.0MiB) RX Errors0Dropped0Overruns0Frame0TX Packets199328bytes19856521(18.9MiB) TX Errors0Dropped0Overruns0Carrier0Collisions0

SSH remote execution script without password

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.