• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

How to attach a public key to a specific user, so that I don't have to type the user name every time?

I've put the user's public key in the authorized_keys file, but when the user logs in, he has to ssh user_name@server to login.  Is there a way to juse type ssh server instead?  Thanks.
1 Solution
The ssh command will default to:

    1) Logging in to the remote server as the same username as the currently logged in local user.
    2) Using the ~/.ssh/config config file

So if you're logged into Server A as the user "johndoe", then when you execute

ssh serverb.mycompany.com

It will attempt to login as "johndoe" on Server B, by default.

If you want to override this behavior, you specify the username e.g.

   ssh root@serverb.mycompany.com

You can also create a per-user default ssh config file,  ~/.ssh/config to override the default flags that SSH uses.

Read the MAN page here:

Note that the ~/.ssh/config file has special requirements for the permissions. You MUST set the permissions so that the user has access, and everybody else does NOT have access, so when you create the file, you should run chmod on it, e.g.:

     touch ~/.ssh/config
     chmod u=rw,g-rwx,o-rwx ~/.ssh/config

You could put the following directives inside the ~/.ssh/config file:

User janedoe

Open in new window

And then, by default, the ssh command will attempt to login to remote servers as the user "janedoe", instead of the currently logged in local user, "johndoe".
thomaszhwangAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now