648

I changed my permissions in my .ssh folder and now when I use a piece of software that uses my private key, I have to type my password each time. What should my permissions be on my id_rsa file to not have to type a password each time I use an app that uses it?

Currently my permissions are set to:

-rw-------@ 1 Jody  staff   114 Nov  4 23:29 config
-rw-------  1 Jody  staff  1743 Oct 21  2009 id_rsa
-rw-------@ 1 Jody  staff   397 Oct 21  2009 id_rsa.pub 
-rw-------@ 1 Jody  staff  3855 Sep 13 22:35 known_hosts
4
  • 15
    #!/bin/bash find .ssh/ -type f -exec chmod 600 {} \;; find .ssh/ -type d -exec chmod 700 {} \;; find .ssh/ -type f -name "*.pub" -exec chmod 644 {} \;
    – Akhil
    Commented Apr 4, 2020 at 5:33
  • Thank you @AkhilJalagam, your code just saved me some thinking. I love stuff like that. I know how to do it but why should I if it's already here :D
    – ThaJay
    Commented May 20, 2021 at 9:05
  • 6
    Simpler version: cd ~ && chmod 600 ~/.ssh/* && chmod 700 ~/.ssh && chmod 644 ~/.ssh/*.pub
    – Joshua M
    Commented Jun 5, 2023 at 3:47
  • Should pin @JoshuaM's comment as the answer
    – Egret
    Commented Mar 30 at 3:30

7 Answers 7

1056

Typically you want the permissions to be:

Item Sample Numeric Bitwise
SSH folder ~/.ssh 700 drwx------
Public key ~/.ssh/id_rsa.pub 644 -rw-r--r--
Private key ~/.ssh/id_rsa 600 -rw-------
Home folder ~ 755 at most drwxr-xr-x at most

I am assuming that you mean that you have to enter your system/user password each time, and that previously you did not have to. cdhowie's response is assuming you set a password/passphrase when generating your keys. If you did then as he says you will have to enter your password every time unless you use an ssh agent.

21
  • 27
    I found elsewhere that if using the authorized_keys file, that it should be chmod'd to 640, ie -rw-r----- . Commented Sep 11, 2014 at 21:19
  • 12
    Where I can find this info in man pages?
    – Sonique
    Commented Nov 17, 2014 at 15:56
  • 217
    I have come back to this post about 30 times now. I cant believe I cant remember it.
    – JREAM
    Commented Apr 2, 2015 at 21:35
  • 12
    The only important things are that nothing in .ssh is writeable to anyone else and none of the secret keys are readable to anyone else. Commented Sep 30, 2015 at 11:56
  • 6
    @Cerin execute permission on a directory grants the ability to list immediate child files/dirs of that directory, files inside the folder don't "inherit" the execute bit of their parent folder.
    – Thomas
    Commented Jan 29, 2017 at 8:42
153

I was struggling with this forever and finally figured out what is needed. Replace $USER everywhere with the SSH username you want to log into on the server. If you're trying to login as root you would need to use /root/.ssh etc., instead of /home/root/.ssh which is how it is for non-root users.

  • Home directory on the server should not be writable by others: chmod go-w /home/$USER
  • SSH folder on the server needs 700 permissions: chmod 700 /home/$USER/.ssh
  • Authorized_keys file needs 644 permissions: chmod 644 /home/$USER/.ssh/authorized_keys
  • Make sure that user owns the files/folders and not root: chown user:user authorized_keys and chown user:user /home/$USER/.ssh
  • Put the generated public key (from ssh-keygen) in the user's authorized_keys file on the server
  • Make sure that user's home directory is set to what you expect it to be and that it contains the correct .ssh folder that you've been modifying. If not, use usermod -d /home/$USER $USER to fix the issue
  • Finally, restart ssh: service ssh restart
  • Then make sure client has the public key and private key files in the local user's .ssh folder and login: ssh [email protected]
6
  • Regarding your first paragraph, I am able to ssh with public/private keys with a user on my local linux box (e.g. abc), different from the user on the remote server (e.g. [email protected]). I just had to make sure the local user owned the local .ssh files (e.g. abc:abc, not root:abc)`
    – MW Millar
    Commented Dec 22, 2015 at 9:41
  • 1
    Thanks for putting all the steps and commands for newbies, Alex. Yours is one of the most helpful answers here.
    – Nav
    Commented Mar 4, 2016 at 6:06
  • 9
    +1. "Authorized_keys file needs 644 permissions" <= that was crucial! Commented Jun 4, 2017 at 17:46
  • If you're giving .ssh directory 700 mode, then there is no point in giving r-- to group and others, because only you can "go through" .ssh then (assuming no hard links exists for these files). The same for accepted answer. Default 755 is enough. Commented Aug 21, 2017 at 9:39
  • 1
    400 for the pem files are sufficient in my experience.
    – A T
    Commented Nov 14, 2018 at 12:24
78

Am posting this as a separate answer since I wanted to see man page recommendations translated into permissions.

Summary based on the man page quotes (linked at the end):

Directory or File Man Page Recommended
Permissions
Mandatory
Permissions
~/.ssh/ There is no general requirement to keep the entire contents of this directory secret, but the recommended permissions are read/write/execute for the user, and not accessible by others. 700
~/.ssh/authorized_keys This file is not highly sensitive, but the recommended permissions are read/write for the user, and not accessible by others 600
~/.ssh/config Because of the potential for abuse, this file must have strict permissions: read/write for the user, and not writable by others. 600
~/.ssh/identity
~/.ssh/id_dsa
~/.ssh/id_rsa
These files contain sensitive data and should be readable by the user but not accessible by others (read/write/execute) 600
~/.ssh/identity.pub
~/.ssh/id_dsa.pub
~/.ssh/id_rsa.pub
Contains the public key for authentication. These files are not sensitive and can (but need not) be readable by anyone. 644

All the man page quotes are from http://linuxcommand.org/lc3_man_pages/ssh1.html

2
  • 6
    Let's not forget known_hosts > "~/.ssh/known_hosts Contains a list of host keys for all hosts the user has logged into that are not already in the systemwide list of known host keys. [...] This file should be writable only by root/the owner and can, but need not be, world-readable." (linux.die.net/man/8/sshd)
    – xtian
    Commented Mar 10, 2021 at 20:55
  • 1
    What about 440 for the private key(id_rsa)? There is a group of 'n' users - which is same person but different accounts so x4x should be fine? Also, the private key doesn't have to be modified so why setting 6xx?
    – Alan
    Commented Apr 1, 2022 at 19:52
39

Also ensure that your home directory is not writeable by other users.

chmod g-w,o-w ~
1
  • 11
    FYI, this command assumes you are logged in as the user and not root
    – Alex W
    Commented Jun 9, 2015 at 18:40
6

Permissions shouldn't have anything to do with this. Your private key is encrypted with the password, so you need to enter it for the private key to be decrypted and usable.

You might consider running an ssh agent, which can cache decrypted keys and will supply them to applications that need them.

5
  • Thanks for the additional info about the ssh agent. Looks like there is one built into Leopard so I think I'll do that. Having a bit of trouble with it but I'll ask another question.
    – Jody G
    Commented Nov 26, 2010 at 22:18
  • 9
    Do not underestimate permissions. They definitely still come into play.
    – Alex W
    Commented May 15, 2015 at 19:49
  • @AlexW They do come into play with other aspects of ssh, but not the one asked about in the question.
    – cdhowie
    Commented May 24, 2015 at 23:43
  • 3
    If you have no password on private keys (whink of automated remote called scripts), it won't help you. Permissions are necessary here.
    – nerdoc
    Commented Jan 7, 2016 at 23:13
  • "I have to type my password each time. What should my permissions be on my id_rsa file to not have to type a password each time I use an app that uses it?" Commented Oct 8, 2018 at 3:11
5

Felipe is correct -- the directory containing your .ssh directory must not be writeable by group or other. Thus chmod go-w ~ is the next logical thing to try if you are still prompted for a password when ssh'ing after running ssh-keygen -t rsa; cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys, assuming you don't assign a passphrase in the ssh-keygen command, and your .ssh directory is in your home directory.

2

It is worth to mention that the current OpenSSH manual (as of OpenSSH 9.0/9.0p1 (2022-04-08)) generally assumes that the owner of authorized_keys file is the user who authenticates, hence 600 or rw- --- --- in the table ("...read/write for the user, and not accessible by others.").

The main reason is that the SSH daemon/server by OpenSSH generally operates in this order I believe (assuming that the ssh service is running under UID 0):

  1. Checks whether the user who tries to authenticate exists in the environment;

    2.1. In case there's no such, invalidates the request (e.g. May 01 01:12:34 host sshd[123456]: Invalid user user3 from x.x.x.x port 12345);

  2. Creates a separate sshd process with the UID and GID of the authenticating user;

  3. In case of pubkey, the new process tries to read the "authorized_keys" in the configured paths.

    3.1. In case the new process cannot read "authorized_keys" file, exists, and that invalidates the request.

  4. Verifies keypair...


# pstree -pugT:

systemd(1,1)-+...
             `-sshd(40976,40976)-+-sshd(194915,194915)---sshd(194939,194915,user1)-+-sshd(938266,938266)
                                 |                                                   `-tmux: client(194943,194943)
                                 `-sshd(2129889,2129889)---sshd(2130234,2129889,user2)---sshd(2130615,2130615)

A source investigation is required to confirm the above, though to summarize, an OpenSSH server currently reads "authorized_keys" file as the authenticating user's UID and primary GID.

If the mode of "authorized_keys" is 600 and the file's owner is not the UID of the authenticating user, the authentication should fail. This may be a case when "authorized_keys" file were created by root for example.

This release deprecates the sshd_config UsePrivilegeSeparation option, thereby making privilege separation mandatory. Privilege separation has been on by default for almost 15 years and sandboxing has been on by default for almost the last five.

Source: https://www.openssh.com/releasenotes.html


Related: https://serverfault.com/a/861842/345785 (...OpenSSH in version 7.5 deprecated the UsePrivilegeSeparation option...)

You must log in to answer this question.