1

I am following the well-accepted answer to terminal - How to ssh to remote web server without using a password - Ask Different to ssh to remote sever without password

I have installed ssh configure of github on both local and remote. so this is the second ssh configuration

also reference to Linux/Mac Tutorial: SSH Key-Based Authentication - How to SSH Without a Password - YouTube

  1. Generate a fresh key pair on local mac:

    $ sudo ssh-keygen -t rsa -b 4096 -f ~/.ssh/webserver
    Generating public/private rsa key pair.
    /Users/me/.ssh/webserver already exists.
    Overwrite (y/n)? y
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /Users/me/.ssh/webserver.
    Your public key has been saved in /Users/me/.ssh/webserver.pub.
    The key fingerprint is:
    SHA256:Ux2ntGEN66Jvpr/eJyWagEHO3frJP51SlIJGP5zGBic [email protected]
    The key's randomart image is:
    +---[RSA 4096]----+
    |            *o.  |
    |      .   E+.B.  |
    |     + . o.O=. . |
    
    me at Max-2018 in ~/.ssh
    $ ls
    MyKeyPair.pem      config  id_rsa.pub   webserver
    SecondKeyPair.pem  id_rsa  known_hosts  webserver.pub
    
  2. copy the new key to remote server (CentOS)

    $ scp webserver.pub [email protected]:/root/.ssh/
    [email protected]'s password:
    webserver.pub                100%  745    45.3KB/s   00:00
    [root@iz2ze9wve43n2nyuvmsfx5z .ssh]# cat webserver.pub  >> authorized_keys
    [root@iz2ze9wve43n2nyuvmsfx5z .ssh]# cat authorized_keys
    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC8iFaLTdhR878LvznCn2PoiIG0ve5at7Bm4NtmLSmoCdNc7iAWoqQLtvx0WoX00TY0+GtaOF3n3P+O0LflIFV5B+m8Wh1PrpOP2fzBSrwQEL3UUtQ/Ti5vUd7FE2LUla8gjFglV83HFTFRwe2O4G6OutwmrBm0+
    
  3. change permissions of .ssh

    #on the remote
    [root@iz2ze9wve43n2nyuvmsfx5z ~]# chmod 700 ~/.ssh
    [root@iz2ze9wve43n2nyuvmsfx5z ~]# chmod 700 ~/.ssh/*
    #on the local
    $ chmod 700 ~/.ssh
    $ sudo chmod 700 ~/.ssh/*
    
  4. config

    $ cat ~/.ssh/config
    #the above part is the newly added.
    Host myserver.com
     IdentityFile ~/.ssh/webserver
     User root
    
    Host *
     AddKeysToAgent yes
     UseKeychain yes
     IdentityFile ~/.ssh/id_rsa
    

Till now, finished all the steps from the answer "This will get you the password-less, keypair-based authentication you desire."

  1. Unfortunately:

    $ ssh [email protected]
    [email protected]'s password:
    
  2. I followed the thumbs YouTube instructions Linux/Mac Tutorial: SSH Key-Based Authentication - How to SSH Without a Password - YouTube

    change the /etc/ssh/sshd_config

    sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
    
    #Change to no to disable tunnelled clear text passwords
    PasswordAuthentication no
    # and restart
    [root@iz2ze9wve43n2nyuvmsfx5z ~]# sudo systemctl restart sshd
    
  3. Try again from the local

    [root@iz2ze9wve43n2nyuvmsfx5z ~]# ssh [email protected]
    Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
    
  4. I search solutions to use scp

    ssh-copy-id

    $ ssh-copy-id -i webserver.pub [email protected]
    /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "webserver.pub"
    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
    [email protected]: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
    
  5. I check another video SSH without password | Tutorial - YouTube

    which achieve it just use step 1 and step 2,

    changmod to 600
    
    $ sudo ls -al .ssh 
    total 32
    drw-------  10 me staff  320 Dec  1 00:13 .
    drwxr-xr-x+ 67 me staff 2144 Dec  1 00:01 ..
    -rw-------   1 me staff 1696 Sep  1 10:15 MyKeyPair.pem
    -rw-------   1 me staff 1692 Sep  1 23:21 SecondKeyPair.pem
    -rw-------   1 me staff  137 Dec  1 00:01 config
    -rw-------   1 me staff 3243 Feb 27  2018 id_rsa
    -rw-------   1 me staff  749 Feb 27  2018 id_rsa.pub
    -rw-------   1 me staff 3760 Nov  5 19:33 known_hosts
    -rw-------   1 me staff 3389 Nov 30 23:22 webserver
    -rw-------   1 me staff  745 Nov 30 23:22 webserver.pub
    
    $ sudo ssh [email protected]
    [email protected]: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
    

What's the problem with my trying?

$ ssh [email protected] -vvv
OpenSSH_7.8p1, LibreSSL 2.7.3
debug1: Reading configuration data /Users/me/.ssh/config
debug1: /Users/me/.ssh/config line 2: Applying options for myserver.com
debug1: /Users/me/.ssh/config line 7: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug1: Connecting to myserver.com port 22.
debug1: Connection established.
debug1: identity file /Users/me/.ssh/webserver type 0
debug1: identity file /Users/me/.ssh/webserver-cert type -1
debug1: identity file /Users/me/.ssh/id_rsa type 0
debug1: identity file /Users/me/.ssh/id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.8
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH_7.0*,OpenSSH_7.1*,OpenSSH_7.2*,OpenSSH_7.3*,OpenSSH_7.4*,OpenSSH_7.5*,OpenSSH_7.6*,OpenSSH_7.7* compat 0x04000002
debug3: fd 5 is O_NONBLOCK
debug1: Authenticating to myserver.com:22 as 'root'
debug3: hostkeys_foreach: reading file "/Users/me/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /Users/me/.ssh/known_hosts:12
debug3: load_hostkeys: loaded 1 keys from myserver.com
debug3: order_hostkeyalgs: prefer hostkeyalgs: [email protected],[email protected],[email protected],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,[email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,ext-info-c
debug2: host key algorithms: [email protected],[email protected],[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected],[email protected],[email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa
debug2: ciphers ctos: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
debug2: ciphers stoc: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
debug2: MACs ctos: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,[email protected],zlib
debug2: compression stoc: none,[email protected],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,[email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: host key algorithms: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519
debug2: ciphers ctos: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected],aes128-cbc,aes192-cbc,aes256-cbc,blowfish-cbc,cast128-cbc,3des-cbc
debug2: ciphers stoc: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected],aes128-cbc,aes192-cbc,aes256-cbc,blowfish-cbc,cast128-cbc,3des-cbc
debug2: MACs ctos: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: MACs stoc: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
debug2: compression ctos: none,[email protected]
debug2: compression stoc: none,[email protected]
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: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
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:qeZuH78znSba00AGex2oz/uhGTgSuzt0hnl8cT9uup4
debug3: hostkeys_foreach: reading file "/Users/me/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /Users/me/.ssh/known_hosts:12
debug3: load_hostkeys: loaded 1 keys from myserver.com
debug3: hostkeys_foreach: reading file "/Users/me/.ssh/known_hosts"
debug3: record_hostkey: found key type ECDSA in file /Users/me/.ssh/known_hosts:8
debug3: load_hostkeys: loaded 1 keys from 39.105.51.157
debug1: Host 'myserver.com' is known and matches the ECDSA host key.
debug1: Found key in /Users/me/.ssh/known_hosts:12
debug3: send packet: type 21
debug2: set_newkeys: mode 1
debug1: rekey after 134217728 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 after 134217728 blocks
debug2: key: /Users/me/.ssh/webserver (0x7fc3c8701580), explicit
debug2: key: /Users/me/.ssh/id_rsa (0x7fc3c8412d90), explicit
debug3: send packet: type 5
debug3: receive packet: type 7
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
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
debug3: start over, passed a different list publickey,gssapi-keyex,gssapi-with-mic
debug3: preferred publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering public key: RSA SHA256:Ux2ntGEN66Jvpr/eJyWagEHO3frJP51SlIJGP5zGBic /Users/me/.ssh/webserver
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
debug1: Offering public key: RSA SHA256:tAc3DhpRdOeAORzhbMTFq0yG+dPFkXmWMYO0dGE/veE /Users/me/.ssh/id_rsa
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
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
[email protected]: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

The sshd_config on the server:

[root@iz2ze9wve43n2nyuvmsfx5z ssh]# cat sshd_config
#   $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#ListenAddress 0.0.0.0
#ListenAddress ::

HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile  .ssh/authorized_keys

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PermitEmptyPasswords no

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes

# GSSAPI options
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PAM authentication via ChallengeResponseAuthentication may bypass
# If you just want the PAM account and session checks to run without
# and ChallengeResponseAuthentication to 'no'.
# WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
# problems.
UsePAM yes

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation sandbox
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS

# override default of no subsystems
Subsystem   sftp    /usr/libexec/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#   X11Forwarding no
#   AllowTcpForwarding no
#   PermitTTY no
#   ForceCommand cvs server
UseDNS no
AddressFamily inet
PermitRootLogin yes
SyslogFacility AUTHPRIV
PasswordAuthentication no

Alternative test:

$ /usr/sbin/sshd -d -p 2222
debug1: sshd version OpenSSH_7.8, LibreSSL 2.7.3
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.

$ sudo /usr/sbin/sshd -d -p 2222
Password:
debug1: sshd version OpenSSH_7.8, LibreSSL 2.7.3
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.
11
  • Your permissions are wrong. Change your key perms to 600 instead of 700
    – ryekayo
    Nov 30, 2018 at 16:25
  • 3
    Also check the PermitRootLogin setting in sshd_config Nov 30, 2018 at 16:28
  • yes, and I also want to have more secure so to use pub and private key. @StephenHarris Nov 30, 2018 at 16:43
  • Run the ssh server in debug mode and see the output. Often easier to run a separate debug server on another port (like /usr/sbin/sshd -d -p 2222). Then watch as you connect to that server from your client.
    – BowlOfRed
    Dec 1, 2018 at 0:17
  • I test to append the result. Honestly I cannot really read the report. It seems that I have to learn the complete ssh theory to solve a problem. @BowlOfRed Dec 1, 2018 at 2:49

3 Answers 3

2

Based on the steps you followed your id_rsa should be set to 600 not 700. I am willing to bet that this is the reason you can't SSH into the server. The step I am referring to is:

[root@iz2ze9wve43n2nyuvmsfx5z ~]# chmod 700 ~/.ssh
[root@iz2ze9wve43n2nyuvmsfx5z ~]# chmod 700 ~/.ssh/*
#on the local
$ chmod 700 ~/.ssh
$ sudo chmod 700 ~/.ssh/*

The second chmod command should be set to 600.

4
  • I changed to 600 but cannot have permission. Nov 30, 2018 at 16:37
  • Im not sure what you mean by that.. But regardless, the key absolutely needs to be set to 600 anyways. Can you run ssh <user>@<server> -vvv and post the output in your question?
    – ryekayo
    Nov 30, 2018 at 20:55
  • yes, thank you for the patience and guideline. Nov 30, 2018 at 23:47
  • The message in the verbose seems to point to a key mismatch. I would recommend generating a new key pair. Remember your public key is what you want to place in your authorized_key file, keep your private key in your local PC's .ssh directory.
    – ryekayo
    Dec 3, 2018 at 15:02
0

If you are sshing from Mac to Linux:

Turn PasswordAuthentication on temporarily, on the server.

ssh-keygen -b 4096 -t rsa **on the Mac. Like you did**..

Make sure to use that new key.

Then make sure to set:

ServerKeyBits 4096 **in sshd_config on the Linux server**.

Then ssh-copy-id someuser@somehost Then ssh someuser@somehost and turn PasswordAuthentication off

This should get it going. Also make sure you have a few ciphers in common.

I know this works, because: https://serverfault.com/a/940634/496863

0
$ sudo /usr/sbin/sshd -d -p 2222
Password:
debug1: sshd version OpenSSH_7.8, LibreSSL 2.7.3
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.

From your logs, it appears that you don't have any server keys. I'm not certain from your question how sshd was installed. Normally when you install the package, the postinstall scripts will create these keys. Perhaps you installed through some other method?

But if you don't have these keys, you need to create at least one of them.

Use ssh-keygen to create the key, then restart sshd

$ sudo ssh-keygen -f /etc/ssh/ssh_host_ed25519_key -N '' -t ed25519

Reference: https://linux-audit.com/using-ed25519-openssh-keys-instead-of-dsa-rsa-ecdsa/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .