SSH Automatic/Passwordless Logon - Setup Public Key Encryption In Single Command

This is a very simple solution, but most guides out there make you login twice (once to scp the key) and once to put the key in authorized_keys.  There\'s no need for that. 

If you don\'t already have a ~/.ssh/id_rsa.pub just type "ssh-keygen -t rsa" and keep hitting enter until it\'s done :)

Just use this code to easily enable passwordless login with SSHD

key=`cat ~/.ssh/id_rsa.pub`;ssh user@192.168.5.25 "echo $key >> ~/.ssh/authorized_keys"

Copy and paste the above into your shell, just be sure to change the "user@" portion to the correct user and the 192.168.5.25 to the server that you want to login without any password authentication.

Also be very sure to keep the backticks and the double quotes ", otherwise you\'ll find it won\'t work properly and tha tthe "~" will expand to the relative path on your current host, which is not what we want.  The double quotes force the shell to pass the command as it should be to the target host.

An Even Better Way (just copy the below it prompts you for the login info so there\'s no need to hand edit anything.

echo "Enter Target Info (eg. root@192.168.1.25";read ipaddress;key=`cat ~/.ssh/id_rsa.pub`;ssh $ipaddress "mkdir ~/.ssh;echo $key >> ~/.ssh/authorized_keys"

Here is a handy bash script for automatic SSH Login/Keyexchange

I just prompts for the login details eg. root@192.168.5.25 and then all you have to do is enter your password and the next time you try to login it will be automatic.  Remember to run "ssh-keygen -t rsa" if you haven\'t created your public/private keys in .ssh yet.

#!/bin/bash
#(c) 2016 - realtechtalk.com
#free to distribute by keeping above credit
echo "Enter the Host: username@ipaddress"
read ipaddress
echo "Setting Up Key Exchange with: \"$ipaddress\""
key=`cat ~/.ssh/id_rsa.pub`
target=$ipaddress
ssh "$target" "mkdir -p ~/.ssh;echo $key >> ~/.ssh/authorized_keys;echo 0 > /selinux/enforce;sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config"
#(c) 2010 - realtechtalk.com
#free to distribute by keeping above credit

*Disable SELinux or you can\'t login automatically on Centos 6.x

#!/bin/bash

#disable SELinux Immediately
echo 0 > /selinux/enforce

#disable SELinux Permanently
sed -i \'s/SELINUX=enforcing/SELINUX=disabled/g\' /etc/selinux/config
 


Tags:

ssh, passwordless, logon, encryption, commandthis, guides, login, scp, authorized_keys, id_rsa, pub, quot, keygen, rsa, enable, sshd, user, echo, shell, portion, server, password, authentication, backticks, quotes, ll, tha, tthe, expand, relative, prompts, info, edit, eg, ipaddress, mkdir, bash, keyexchange, haven, bin, realtechtalk, distribute, username, selinux, enforce, sed, enforcing, disabled, etc, config, disable, automatically, centos, permanently,

Latest Articles

  • Linux named / bind how to dump, view and clear the cache!
  • Centos 6 / 7 / 8 How To Change Default nameservers in /etc/resolv.conf when using DHCP / dhclient
  • Adobe Acrobat Reader for Linux to use and view XFA Fillable Forms
  • Debian Linux Mint Ubuntu iptables save and restore settings automatically onboot and reboot
  • Why SMART is not smart at all and doesn't properly predict disk errors that cause a kernel panic or crash
  • scp: ambiguous target error and solution
  • VirtualBox How To Add iSCSI Storage using VBoxManage
  • iSCSI on Centos 7 Configuration and Setup Guide for Initiator and Target
  • Python and BeautifulSoup4's BS4's Decompose Method To Remove Unwanted Inner Tags
  • httpd AH00534: httpd: Configuration error: No MPM loaded. solution
  • bash script to remove modules from httpd.conf that are not actually installed
  • bash scripting how to create a function
  • Centos 7 PHP MySQL Not Working Solution
  • Bash How To Cut or Split Natively And Get The LAST Field
  • Bash Script How To Manipulate Text/Strings By Searchig and Replacing Natively
  • How Does Cisco CUCM (Cisco Unified Communication Manager) Work?
  • What DNS Options Does Active Directory Offer in Windows Server 2008,2012,2016 ?
  • syntax error, unexpected T_SL in PHP Solution
  • grep regular expression match number range between specific numbers
  • bash how to print out lines of text within a range from the first occurrence