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

  • FreePBX 17 How To Add a Trunk
  • Docker Container Onboot Policy - How to make sure a container is always running
  • FreePBX 17 How To Add Phones / Extensions and Register
  • Warning: The driver descriptor says the physical block size is 2048 bytes, but Linux says it is 512 bytes. solution
  • Cisco How To Use a Third Party SIP Phone (eg. Avaya, 3CX)
  • Cisco Unified Communication Manager (CUCM) - How To Add Phones
  • pptp / pptpd not working in DD-WRT iptables / router
  • systemd-journald high memory usage solution
  • How to Install FreePBX 17 in Linux Debian Ubuntu Mint Guide
  • How To Install Cisco's CUCM (Cisco Unified Communication Manager) 12 Guide
  • Linux Ubuntu Redhat How To Extract Images from PDF
  • Linux and Windows Dual Boot Issue NIC Won't work After Booting Windows
  • Cisco CME How To Enable ACD hunt groups
  • How to install gns3 on Linux Ubuntu Mint
  • How to convert audio for Asterisk .wav format
  • Using Cisco CME Router with Asterisk as a dial-peer
  • Cisco CME How To Configure SIP Trunk VOIP
  • Virtualbox host Only Network Error Failed to save host network interface parameter - Cannot change gateway IP of host only network
  • Cisco CME and C7200 Router Testing and Learning Environment on Ubuntu 20+ Setup Tutorial Guide
  • Abusive IP ranges blacklist