LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-10-2016, 09:58 AM   #1
postcd
Member
 
Registered: Oct 2013
Posts: 527

Rep: Reputation: Disabled
How to make public key SSH access working please?


Hello,

i want to access server via SSH without need to enter password, so i thought i will generate public key and share it with server from which i will be accessing, but it do not works. Says: "# ssh -l root localhost -p 7000
Permission denied (publickey)."

why i am connecting localhost? because i set port forwarding from localhost to destination LAN/firewalled by executing command ssh -fN -R 7000:localhost:22 username@PublicServerIPhere on the server i want to connect. It should work, password access worked, but not public key, returns "Permission denied (publickey)."

Error appears even after i logged in server i want to connect and executed ssh-keygen and then ssh-copy-id IPOfTheServerFromWhichINeedPasswordLesAccess
it worked and when i re-run, it says: "All keys were skipped because they already exist on the remote system."
So key should be coppied.

Where is the problem please? This is sshd_config of the server i am trying to connect (one that refuse ssh pub. key connection and allows password connection):

Quote:
cat /etc/ssh/sshd_config|grep -v "#"|grep " "
Port 22
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
UsePrivilegeSeparation yes
KeyRegenerationInterval 3600
ServerKeyBits 1024
SyslogFacility AUTH
LogLevel INFO
LoginGraceTime 120
PermitRootLogin without-password
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
IgnoreRhosts yes
RhostsRSAAuthentication no
HostbasedAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
PasswordAuthentication no
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
UsePAM no
source server (from which i am connecting) is CentOS(redhat), destination server which refusing pub.key access is Debian.

Thank you for help :-/
 
Old 11-10-2016, 10:29 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
Allowing root login is a security hazard.
 
Old 11-10-2016, 01:03 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by postcd View Post
Says: "# ssh -l root localhost -p 7000
Permission denied (publickey)."
The configuration PermitRootLogin without-password means that you can connect root if you have keys set up, password authenticatin is blocked. You also need a public - private key pair for the root account and then place the public key on the remote server in the root account.

As smallpond points out, allowing root login is a hazard, even if using keys mitigates that some. Which problem are you trying to solve?
 
Old 11-10-2016, 01:19 PM   #4
postcd
Member
 
Registered: Oct 2013
Posts: 527

Original Poster
Rep: Reputation: Disabled
From Your kind reply, i am unsure what exactly i should do. From what you said i think i already did what you said but it returns mentioned error.
 
Old 11-10-2016, 01:27 PM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
On the client system, as root, you'll need to create a key pair for root to use.

Then you'll need to connect to the remote machine as a regular user and use "sudo" or "su" to add the public key to the file /root/.ssh/authorized_keys. For example,

Code:
sudoedit /root/.ssh/authorized_keys
That's because you need two key pairs. One to create the tunnel, the other to connect to root. It seems you have the first key pair, if you can log in already as a normal user using the key.

What do the following do for you?

Code:
ssh username@PublicServerIPhere
ssh -i ~/.ssh/your_key_name username@PublicServerIPhere
 
Old 11-10-2016, 01:56 PM   #6
postcd
Member
 
Registered: Oct 2013
Posts: 527

Original Poster
Rep: Reputation: Disabled
@Turbocapitalist

Thank you for help. Indeed it seems that i have to share the key both ways in this case, not only from server to the client but vice versa too (clients key to the server).

i had to set:
Quote:
PermitRootLogin yes
PasswordAuthentication yes
UsePAM yes
then service ssh reload

then i was able to push the key to the server from client by command: ssh-copy-id "127.0.0.1 -p 7000"
entering password

then change back sshd_config to:
Quote:
PermitRootLogin without-password
PasswordAuthentication no
UsePAM no
service ssh reload

then after such change no one is prompted for password and only keys in server's authorized_keys file are allowed.

That is what i wanted. Thx

Last edited by postcd; 11-10-2016 at 01:57 PM.
 
Old 11-11-2016, 08:13 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Probably the most-common mistake is to fail to set the permissions of the .ssh directory (in the target user's home directory) to 700 = rwx------. If this is not the case, the contents of the directory (and therefore, your keys) will be ignored.

You do not have to restart sshd after making any such change.

When you attempt a password-free login, sshd looks for .ssh/authorized_keys in the target user's home, and checks that directory's permissions-mask.

You must also ensure that the connecting client is running a "ssh-agent" daemon, which actually supplies your key to the remote when you attempt to connect.

Last edited by sundialsvcs; 11-11-2016 at 08:16 AM.
 
Old 11-11-2016, 11:01 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by postcd View Post
then after such change no one is prompted for password and only keys in server's authorized_keys file are allowed.

That is what i wanted. Thx
Great. By the way you can use more than one key and give each one a special purpose so they cannot be used for general login. See the "AUTHORIZED_KEYS FILE FORMAT" section in the manual page for "sshd". The part you would look for there is command="command"

If you don't mind telling, why are you using remote root access? There is often a better way.
 
Old 11-11-2016, 11:51 AM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by Turbocapitalist View Post
Great. By the way you can use more than one key and give each one a special purpose so they cannot be used for general login. See the "AUTHORIZED_KEYS FILE FORMAT" section in the manual page for "sshd". The part you would look for there is command="command"

If you don't mind telling, why are you using remote root access? There is often a better way.
Good luck here. The OP has been asking about doing this (and has received ANSWERS for it) for years now..you even answered one two years ago:
http://www.linuxquestions.org/questi...em-4175525156/
http://www.linuxquestions.org/questi...ts-4175525205/

And they've been advised numerous times to not log in as root.
 
Old 11-11-2016, 12:26 PM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by TB0ne View Post
Good luck here. The OP has been asking about doing this (and has received ANSWERS for it) for years now..you even answered one two years ago
LMAO. Good point. Now the OP has keys working, by 2018 or so they'll be able to leave remote root logins behind.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] ssh public/private key not working ilesterg Linux - Security 20 11-11-2014 01:32 AM
[SOLVED] Ssh authentication using public key not working! shivaa Linux - Newbie 13 03-07-2013 11:17 PM
SSH public key authentication not working to0 Red Hat 6 12-06-2012 02:14 AM
SSH access method: public-key + password together.... MCD555 Linux - Security 4 05-27-2009 02:46 AM
upgraded ssh, now can't make public key connection BrianK Linux - Networking 0 04-10-2004 05:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration