29

I have a different (assigned, non-changable) username on one of the servers I log to regularly, and I would like to avoid writing it down every time. Can I make these lines

[tohecz@localhost ~]$ ssh myserver.cz
[tohecz@localhost ~]$ ssh anotherserver.cz

behave as the following?

[tohecz@localhost ~]$ ssh tohecz@myserver.cz
[tohecz@localhost ~]$ ssh anotheruser@anotherserver.cz
9
  • 1
    Can I ask why the question got downvoted? Have I done anything wrong? If so, please exaplain it to me so that I can improve the post.
    – yo'
    Apr 24, 2014 at 12:22
  • 1
    I did not DV this but typically Q's that are very basic such as this or show little effort in searching prior to asking are viewed negatively. Also searching this site would've yielded many A's that show the below A: unix.stackexchange.com/search?q=ssh+user+host
    – slm
    Apr 24, 2014 at 12:56
  • 1
    @tohecz Looking at man ssh_config and searching for User gives the result in about 30 sec. So, as slm says, it is very basic. I'm not suggesting asking such a question is a bad thing - one does have to know where to look, and that may not be obvious. I ask basic questions sometimes too, but some people may not like it. I'm also not justifying the downvote. I don't believe in them, except for spam. Apr 24, 2014 at 14:49
  • 2
    @FaheemMitha That sounds like I should know where to look. I consider any further discussion in this manner pointless.
    – yo'
    Apr 24, 2014 at 15:03
  • 1
    @tohecz At the risk of redundancy, no disrespect or offense to you is intended. This was by way of response to you asking about the downvote. Apr 24, 2014 at 15:09

2 Answers 2

41

Add the following in your ~/.ssh/config file:

Host myserver.cz
  User tohecz

Host anotherserver.cz
  User anotheruser

You can specify a lot of default parameters for your hosts using this file. Just have a look at man ssh_config for other possibilities.

2
  • 8
    Regarding other default parameters, I like shortening the host, along the lines of Host web / Hostname www.mysite.com / User foo
    – Kevin
    Apr 24, 2014 at 17:48
  • why only link to a web site for documentation that would be installed on almost every system with the openssh client installed? links die. local man pages are forever. at least mention that the info is available by running man ssh_config.
    – cas
    Nov 10, 2015 at 9:43
1

How to have default and host-specific users

To be clear, if you want BOTH a default user AND host-specific users, you need to use Host *, and put it at the bottom of the config file:

~/.ssh/config:

#
# Aliases
#

Host dev
    Hostname dev.example.com

Host mac
    Hostname mac.local


#
# Host-specific users
#

Host dev dev.example.com
    User root

Host mac mac.local
    User me


#
# Default user
#

Host *
    User app

References

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.