As a part of OS user authentication there is a possibility to exchange the keys between ID’s on same or different OS so that they can communicate without password . it is call password less authentication . Using this one can login to the server with our any “Entering password” / do file transfer from the script where we don’t require to Enter password .
In this Post we will use same Server with 2 ID’s . We will see how to connect one to another with password then will see how we can configure password less authentication .
In the organisation we may have server to server authentication mostly . some time with in the same server between multiple id’s it may need to exchange for ease of day to day operations .
You want to use Linux and OpenSSH to automate your tasks. Therefore you need an automatic login from host A / user source to Host B / user destination. You don’t want to enter any passwords, because you want to call ssh from a within a shell script.
How to Create a New User in Linux
To create a new user account, invoke the useradd
command followed by the name of the user.
For example to create a new user named username
you would run:
sudo useradd username
[root@ip-172-31-14-154 ~]# useradd source [root@ip-172-31-14-154 ~]# id source uid=1002(source) gid=1003(source) groups=1003(source) [root@ip-172-31-14-154 ~]# [root@ip-172-31-14-154 ~]# [root@ip-172-31-14-154 ~]# useradd destination [root@ip-172-31-14-154 ~]# id destination uid=1003(destination) gid=1004(destination) groups=1004(destination)
Set some password for each of the source and destination id’s using passwd userid command
root@ip-172-31-14-154 destination]# passwd source Changing password for user source. New password: BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word Retype new password: passwd: all authentication tokens updated successfully. [root@ip-172-31-14-154 destination]# [root@ip-172-31-14-154 destination]# [root@ip-172-31-14-154 destination]# passwd destination Changing password for user destination. New password: BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word Retype new password: passwd: all authentication tokens updated successfully. [root@ip-172-31-14-154 destination]# password of source is zaq12wsx and password of destination is mko09ijn Let us see how we can do ssh from source to destination . Some OS will have tectia where sshg3 and scpg3 are available instead of ssh and scp
[source@ip-172-31-14-154 ~]$ ssh destination@ip-172-31-14-154
The authenticity of host 'ip-172-31-14-154 (172.31.14.154)' can't be established.
ECDSA key fingerprint is SHA256:bASX/U9HJi3iu0CUsUY+VcYlZR4mE8/b1tJQcl69RpM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'ip-172-31-14-154,172.31.14.154' (ECDSA) to the list of known hosts.
destination@ip-172-31-14-154: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
[source@ip-172-31-14-154 ~]$
Update /etc/ssh/sshd_config with PasswordAuthentication yes if it was no and then restart sshd service to get rid of the above error
[root@ip-172-31-14-154 destination]# grep -i PasswordAuthentication /etc/ssh/sshd_config
#PasswordAuthentication yes
PasswordAuthentication yes
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication, then enable this but set PasswordAuthentication
[root@ip-172-31-14-154 destination]# systemctl restart sshd
[root@ip-172-31-14-154 destination]#
SSH from Source to Destination . Now it will ask to enter destination id password
[source@ip-172-31-14-154 ~]$ ssh destination@ip-172-31-14-154 destination@ip-172-31-14-154's password: [destination@ip-172-31-14-154 ~]$
With Password we are able to authenticate from source to destination id successfully . Now we will see how we can make this password less using the public and private keys of the id’s .
Let’s do the password authentication by generating a pair of public and private keys of id’s and exchange for authentication using below command
ssh-keygen -t rsa -b 4096
bit size can be 2048,1024,3072 or 4098 or any other bit size that supports
Run the command for source id
source@ip-172-31-14-154 ~]$ ssh-keygen -t rsa -b 4096 Generating public/private rsa key pair. Enter file in which to save the key (/home/source/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/source/.ssh/id_rsa. Your public key has been saved in /home/source/.ssh/id_rsa.pub. The key fingerprint is: SHA256:jtcQnDlr00yb91sOVgNn4dH4Gj0cqu79GJ2pZ+Kv3mA source@ip-172-31-14-154.us-east-2.compute.internal The key's randomart image is: +---[RSA 4096]----+ | +.| | . o o.+| | * . .o*.| | B o .=oo| | S = o +o| | + + o .o.+| | . o o E++.| | . .oo*B | | ..oBO+.| +----[SHA256]-----+ [source@ip-172-31-14-154 ~]$ cd .ssh/ [source@ip-172-31-14-154 .ssh]$ ls -lrt total 12 -rw-r--r--. 1 source source 192 May 2 04:49 known_hosts -rw-r--r--. 1 source source 776 May 2 05:04 id_rsa.pub -rw-------. 1 source source 3422 May 2 05:04 id_rsa [source@ip-172-31-14-154 .ssh]$
Run the same command for destination id
[destination@ip-172-31-14-154 ~]$ ssh-keygen -t rsa -b 4096 Generating public/private rsa key pair. Enter file in which to save the key (/home/destination/.ssh/id_rsa): Created directory '/home/destination/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/destination/.ssh/id_rsa. Your public key has been saved in /home/destination/.ssh/id_rsa.pub. The key fingerprint is: SHA256:V8THYedkP6waQjWcxK+4uFITKsk6ul7gXbqW6m+KDO8 destination@ip-172-31-14-154.us-east-2.compute.internal The key's randomart image is: +---[RSA 4096]----+ | +=o.o.+| | .=o.+=.| | . o. oo| | .. . .. .| | .. ... S.o... | |. o+o. o o..o | |...+o . o .. | |++.+.. . . | |BOEo ... | +----[SHA256]-----+ [destination@ip-172-31-14-154 ~]$ ssh-keygen command will create 2 files one id_rsa [ Private key ] and id_rsa.pub [ public key ] Note : For Source id to connect to Destination then source public key need to upload to destination When i try to connect still asking password . so trying to un on debug mode vith -vvv
[source@ip-172-31-14-154 .ssh]$ ssh destination@ip-172-31-14-154 -vvv OpenSSH_8.0p1, OpenSSL 1.1.1g FIPS 21 Apr 2020 debug1: Reading configuration data /etc/ssh/ssh_config debug3: /etc/ssh/ssh_config line 52: Including file /etc/ssh/ssh_config.d/05-redhat.conf depth 0 debug1: Reading configuration data /etc/ssh/ssh_config.d/05-redhat.conf debug2: checking match for 'final all' host ip-172-31-14-154 originally ip-172-31-14-154 debug3: /etc/ssh/ssh_config.d/05-redhat.conf line 3: not matched 'final' debug2: match not found debug3: /etc/ssh/ssh_config.d/05-redhat.conf line 5: Including file /etc/crypto-policies/back-ends/openssh.config depth 1 (parse only) debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config debug3: gss kex names ok: [gss-curve25519-sha256-,gss-nistp256-sha256-,gss-group14-sha256-,gss-group16-sha512-,gss-gex-sha1-,gss-group14-sha1-] debug3: kex names ok: [curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1] debug1: configuration requests final Match pass debug1: re-parsing configuration debug1: Reading configuration data /etc/ssh/ssh_config debug3: /etc/ssh/ssh_config line 52: Including file /etc/ssh/ssh_config.d/05-redhat.conf depth 0 debug1: Reading configuration data /etc/ssh/ssh_config.d/05-redhat.conf debug2: checking match for 'final all' host ip-172-31-14-154 originally ip-172-31-14-154 debug3: /etc/ssh/ssh_config.d/05-redhat.conf line 3: matched 'final' debug2: match found debug3: /etc/ssh/ssh_config.d/05-redhat.conf line 5: Including file /etc/crypto-policies/back-ends/openssh.config depth 1 debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config debug3: gss kex names ok: [gss-curve25519-sha256-,gss-nistp256-sha256-,gss-group14-sha256-,gss-group16-sha512-,gss-gex-sha1-,gss-group14-sha1-] debug3: kex names ok: [curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1] debug2: resolving "ip-172-31-14-154" port 22 debug2: ssh_connect_direct debug1: Connecting to ip-172-31-14-154 [172.31.14.154] port 22. debug1: Connection established. debug1: identity file /home/source/.ssh/id_rsa type 0 debug1: identity file /home/source/.ssh/id_rsa-cert type -1 debug1: identity file /home/source/.ssh/id_dsa type -1 debug1: identity file /home/source/.ssh/id_dsa-cert type -1 debug1: identity file /home/source/.ssh/id_ecdsa type -1 debug1: identity file /home/source/.ssh/id_ecdsa-cert type -1 debug1: identity file /home/source/.ssh/id_ed25519 type -1 debug1: identity file /home/source/.ssh/id_ed25519-cert type -1 debug1: identity file /home/source/.ssh/id_xmss type -1 debug1: identity file /home/source/.ssh/id_xmss-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_8.0 debug1: Remote protocol version 2.0, remote software version OpenSSH_8.0 debug1: match: OpenSSH_8.0 pat OpenSSH* compat 0x04000000 debug2: fd 4 setting O_NONBLOCK debug1: Authenticating to ip-172-31-14-154:22 as 'destination' debug3: hostkeys_foreach: reading file "/home/source/.ssh/known_hosts" debug3: record_hostkey: found key type ECDSA in file /home/source/.ssh/known_hosts:1 debug3: load_hostkeys: loaded 1 keys from ip-172-31-14-154 debug3: order_hostkeyalgs: prefer hostkeyalgs: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521 debug3: send packet: type 20 debug1: SSH2_MSG_KEXINIT sent debug3: receive packet: type 20 debug1: SSH2_MSG_KEXINIT received debug2: local client KEXINIT proposal debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,ext-info-c debug2: host key algorithms: ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa debug2: ciphers ctos: aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc debug2: ciphers stoc: aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc debug2: MACs ctos: hmac-sha2-256-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha2-256,hmac-sha1,umac-128@openssh.com,hmac-sha2-512 debug2: MACs stoc: hmac-sha2-256-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha2-256,hmac-sha1,umac-128@openssh.com,hmac-sha2-512 debug2: compression ctos: none,zlib@openssh.com,zlib debug2: compression stoc: none,zlib@openssh.com,zlib debug2: languages ctos: debug2: languages stoc: debug2: first_kex_follows 0 debug2: reserved 0 debug2: peer server KEXINIT proposal debug2: KEX algorithms: curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1 debug2: host key algorithms: rsa-sha2-512,rsa-sha2-256,ssh-rsa,ecdsa-sha2-nistp256,ssh-ed25519 debug2: ciphers ctos: aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc debug2: ciphers stoc: aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc debug2: MACs ctos: hmac-sha2-256-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha2-256,hmac-sha1,umac-128@openssh.com,hmac-sha2-512 debug2: MACs stoc: hmac-sha2-256-etm@openssh.com,hmac-sha1-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512-etm@openssh.com,hmac-sha2-256,hmac-sha1,umac-128@openssh.com,hmac-sha2-512 debug2: compression ctos: none,zlib@openssh.com debug2: compression stoc: none,zlib@openssh.com debug2: languages ctos: debug2: languages stoc: debug2: first_kex_follows 0 debug2: reserved 0 debug1: kex: algorithm: curve25519-sha256 debug1: kex: host key algorithm: ecdsa-sha2-nistp256 debug1: kex: server->client cipher: aes256-gcm@openssh.com MAC: <implicit> compression: none debug1: kex: client->server cipher: aes256-gcm@openssh.com MAC: <implicit> compression: none debug1: kex: curve25519-sha256 need=32 dh_need=32 debug1: kex: curve25519-sha256 need=32 dh_need=32 debug3: send packet: type 30 debug1: expecting SSH2_MSG_KEX_ECDH_REPLY debug3: receive packet: type 31 debug1: Server host key: ecdsa-sha2-nistp256 SHA256:bASX/U9HJi3iu0CUsUY+VcYlZR4mE8/b1tJQcl69RpM debug3: hostkeys_foreach: reading file "/home/source/.ssh/known_hosts" debug3: record_hostkey: found key type ECDSA in file /home/source/.ssh/known_hosts:1 debug3: load_hostkeys: loaded 1 keys from ip-172-31-14-154 debug3: hostkeys_foreach: reading file "/home/source/.ssh/known_hosts" debug3: record_hostkey: found key type ECDSA in file /home/source/.ssh/known_hosts:1 debug3: load_hostkeys: loaded 1 keys from 172.31.14.154 debug1: Host 'ip-172-31-14-154' is known and matches the ECDSA host key. debug1: Found key in /home/source/.ssh/known_hosts:1 debug3: send packet: type 21 debug2: set_newkeys: mode 1 debug1: rekey out after 4294967296 blocks debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug3: receive packet: type 21 debug1: SSH2_MSG_NEWKEYS received debug2: set_newkeys: mode 0 debug1: rekey in after 4294967296 blocks debug1: Will attempt key: /home/source/.ssh/id_rsa RSA SHA256:jtcQnDlr00yb91sOVgNn4dH4Gj0cqu79GJ2pZ+Kv3mA debug1: Will attempt key: /home/source/.ssh/id_dsa debug1: Will attempt key: /home/source/.ssh/id_ecdsa debug1: Will attempt key: /home/source/.ssh/id_ed25519 debug1: Will attempt key: /home/source/.ssh/id_xmss debug2: pubkey_prepare: done debug3: send packet: type 5 debug3: receive packet: type 7 debug1: SSH2_MSG_EXT_INFO received debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521> debug3: receive packet: type 6 debug2: service_accept: ssh-userauth debug1: SSH2_MSG_SERVICE_ACCEPT received debug3: send packet: type 50 debug3: receive packet: type 51 debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password debug3: start over, passed a different list publickey,gssapi-keyex,gssapi-with-mic,password debug3: preferred gssapi-with-mic,publickey,keyboard-interactive,password debug3: authmethod_lookup gssapi-with-mic debug3: remaining preferred: publickey,keyboard-interactive,password debug3: authmethod_is_enabled gssapi-with-mic debug1: Next authentication method: gssapi-with-mic debug1: Unspecified GSS failure. Minor code may provide more information No Kerberos credentials available (default cache: KCM:) debug1: Unspecified GSS failure. Minor code may provide more information No Kerberos credentials available (default cache: KCM:) debug2: we did not send a packet, disable method debug3: authmethod_lookup publickey debug3: remaining preferred: keyboard-interactive,password debug3: authmethod_is_enabled publickey debug1: Next authentication method: publickey debug1: Offering public key: /home/source/.ssh/id_rsa RSA SHA256:jtcQnDlr00yb91sOVgNn4dH4Gj0cqu79GJ2pZ+Kv3mA debug3: send packet: type 50 debug2: we sent a publickey packet, wait for reply debug3: receive packet: type 51 debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password debug1: Trying private key: /home/source/.ssh/id_dsa debug3: no such identity: /home/source/.ssh/id_dsa: No such file or directory debug1: Trying private key: /home/source/.ssh/id_ecdsa debug3: no such identity: /home/source/.ssh/id_ecdsa: No such file or directory debug1: Trying private key: /home/source/.ssh/id_ed25519 debug3: no such identity: /home/source/.ssh/id_ed25519: No such file or directory debug1: Trying private key: /home/source/.ssh/id_xmss debug3: no such identity: /home/source/.ssh/id_xmss: No such file or directory debug2: we did not send a packet, disable method debug3: authmethod_lookup password debug3: remaining preferred: ,password debug3: authmethod_is_enabled password debug1: Next authentication method: password destination@ip-172-31-14-154's password:
chmod 0600 /home/your_home/.ssh/authorized_keys
After that go to /etc/ssh/sshd_config
PubkeyAuthentication yes
systemctl restart sshd
Source Public key is updated on the destination authorized keys . Now try
destination@ip-172-31-14-154 .ssh]$ cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCqLtvCpcoVJi5NmVNEwgf1+RyNvpvVF5iIKGRAZYZ
[destination@ip-172-31-14-154 .ssh]$ ls -lrt
total 12
-rw-r--r--. 1 destination destination 781 May 2 05:05 id_rsa.pub
-rw-------. 1 destination destination 3434 May 2 05:05 id_rsa
-rw-------. 1 destination destination 776 May 2 05:45 authorized_keys
[destination@ip-172-31-14-154 .ssh]$
[source@ip-172-31-14-154 ~]$ ssh destination@ip-172-31-14-154
Last login: Sun May 2 05:45:50 2021 from 172.31.14.154
[destination@ip-172-31-14-154 ~]$
YESSSS Successful after a couple of issues . Now source is able to connect to destination without password . scp also now works without password authentication
[source@ip-172-31-14-154 ~]$ scp /tmp/1 destination@ip-172-31-14-154:/home/destination 1 100% 0 0.0KB/s 00:00 [source@ip-172-31-14-154 ~]$ [source@ip-172-31-14-154 ~]$
Now we can do vice versa . Means upload destination public key [.pub file ] to source and configure in authorized_keys then destination will be able to connect to source without password .Lets do it quickly .
source@ip-172-31-14-154 ~]$ scp destination@ip-172-31-14-154:/home/destination/.ssh/id_rsa.pub .
id_rsa.pub 100% 781 686.2KB/s 00:00
[source@ip-172-31-14-154 ~]$ cat id_rsa.pub >> .ssh/authorized_keys
[source@ip-172-31-14-154 ~]$
[destination@ip-172-31-14-154 ~]$ ssh source@ip-172-31-14-154 The authenticity of host 'ip-172-31-14-154 (172.31.14.154)' can't be established. ECDSA key fingerprint is SHA256:bASX/U9HJi3iu0CUsUY+VcYlZR4mE8/b1tJQcl69RpM. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'ip-172-31-14-154,172.31.14.154' (ECDSA) to the list of known hosts. source@ip-172-31-14-154's password: Last login: Sun May 2 05:44:10 2021 [source@ip-172-31-14-154 ~]$ [source@ip-172-31-14-154 ~]$ [source@ip-172-31-14-154 ~]$ ls -lrt total 4 -rw-r--r--. 1 source source 781 May 2 05:54 id_rsa.pub [source@ip-172-31-14-154 ~]$
[source@ip-172-31-14-154 .ssh]$ ls -lrt
total 20
-rw-r–r–. 1 source source 776 May 2 05:04 id_rsa.pub
-rw——-. 1 source source 3422 May 2 05:04 id_rsa
-rw-r–r–. 1 source source 776 May 2 05:14 destination@localhost
-rw-r–r–. 1 source source 363 May 2 05:15 known_hosts
-rw-rw-r–. 1 source source 781 May 2 05:55 authorized_keys
[source@ip-172-31-14-154 .ssh]$ chmod 600 authorized_keys
[source@ip-172-31-14-154 .ssh]$ cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDnTQ1Wba93oW7t+Em/X1Mgx
k/8hJT/zc/0f3lFwQJ/9rOHux/GT9IyFknduQuauNWz5MxnrK5wNs7w== destination@ip-172-31-14-154.us-east-2.compute.internal
[source@ip-172-31-14-154 .ssh]$
Finally we are able to connect to both id’s vice versa without password . SCP also now can without password authentication .
[source@ip-172-31-14-154 .ssh]$ ssh destination@ip-172-31-14-154
Last login: Sun May 2 05:56:42 2021 from 172.31.14.154
[destination@ip-172-31-14-154 ~]$ ssh source@ip-172-31-14-154
Last login: Sun May 2 05:57:09 2021 from 172.31.14.154
[source@ip-172-31-14-154 ~]$
If SSH Tectia installed then sshg3 instead of ssh and scpg3 instead of scp tools available . rest of the steps remain same .
Let’s try good Luck !!!