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

  • How high can a Xeon CPU get?
  • bash fix PATH environment variable "command not found" solution
  • Ubuntu Linux Mint Debian Redhat Youtube Cannot Play HD or 4K videos, dropped frames or high CPU usage with Nvidia or AMD Driver
  • hostapd example configuration for high speed AC on 5GHz using WPA2
  • hostapd how to enable and use WPS to connect wireless devices like printers
  • Dell Server Workstation iDRAC Dead after Firmware Update Solution R720, R320, R730
  • Cloned VM/Server/Computer in Linux won't boot and goes to initramfs busybox Solution
  • How To Add Windows 7 8 10 11 to GRUB Boot List Dual Booting
  • How to configure OpenDKIM on Linux with Postfix and setup bind zonefile
  • Debian Ubuntu 10/11/12 Linux how to get tftpd-hpa server setup tutorial
  • efibootmgr: option requires an argument -- 'd' efibootmgr version 15 grub-install.real: error: efibootmgr failed to register the boot entry: Operation not permitted.
  • Apache Error Won't start SSL Cert Issue Solution Unable to configure verify locations for client authentication SSL Library Error: 151441510 error:0906D066:PEM routines:PEM_read_bio:bad end line SSL Library Error: 185090057 error:0B084009:x509 certif
  • Linux Debian Mint Ubuntu Bridge br0 gets random IP
  • redis requirements
  • How to kill a docker swarm
  • docker swarm silly issues
  • isc-dhcp-server dhcpd how to get longer lease
  • nvidia cannot resume from sleep Comm: nvidia-sleep.sh Tainted: Linux Ubuntu Mint Debian
  • zfs and LUKS how to recover in Linux
  • [error] (28)No space left on device: Cannot create SSLMutex Apache Solution Linux CentOS Ubuntu Debian Mint