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 to install Windows or other OS and then bring to another computer by using a physical drive and Virtual Machine with QEMU
  • PXE-E23 Error BOOTx64.EFI GRUB booting is 0 bytes tftp pxe dhcp solution NBP filesize is 0 Bytes
  • vagrant install on Debian Mint Ubuntu Linux RHEL Quick Setup Guide Tutorial
  • RHEL 8 CentOS 8, Alma Linux 8, Rocky Linux 8 System Not Booting with RAID or on other servers/computers Solution for dracut and initramfs missing kernel modules
  • How to Upgrade to Debian 11 from Version 8,9,10
  • Ubuntu Linux Mint Debian Redhat Cannot View Files on Android iPhone USB File Transfer Not Working Solution
  • Virtualbox Best Networking Mode In Lab/Work Environment without using NAT Network or Bridged
  • debootstrap how to install Ubuntu, Mint, Debian install
  • Linux grub not using UUID for the root device instead it uses /dev/sda1 or other device name solution
  • How To Restore Partition Table on Running Linux Mint Ubuntu Debian Machine
  • Debian Ubuntu apt install stop daemon questions/accept the default action without prompting
  • iptables NAT how to enable PPTP in newer Debian/Ubuntu/Mint Kernels Linux
  • Grandstream Phone Vulnerability Security Issue Remote Backdoor Connection to 207.246.119.209:3478
  • Linux How to Check Which NIC is Onboard eth0 or eth1 Ubuntu Centos Debian Mint
  • VboxManage VirtualBox NAT Network Issues Managment Troubleshooting
  • Dell PowerEdge Server iDRAC Remote KVM/IP Default Username, Password Reset and Login Information Solution
  • Nvidia Tesla GPUs K40/K80/M40/P40/P100/V100 at home/desktop hacking, cooling, powering, cable solutions Tutorial AIO Solutions
  • Stop ls in Linux Debian Mint CentOS Ubuntu from applying quotes around filenames and directory names
  • Thunderbird Attachment Download Error Corrupt Wrong filesize of 29 or 27 bytes Solution
  • Generic IP Camera LAN Default IP Settings DVR