SSH configuration: override the default username

UnixSshSsh Config

Unix Problem Overview


Is it possible to configure ssh to know what my username should be?

By default it uses the current username, which is not correct in my case.

I'm on a loaner laptop, and my username is loaner, but I want to tell ssh that my username is buck.

Bonus points: my username at home is bgolemon. If I could configure the username per-host that would be even better.

Unix Solutions


Solution 1 - Unix

Create a file called config inside ~/.ssh. Inside the file you can add:

Host *
    User buck

Or add

Host example
    HostName example.net
    User buck

The second example will set a username and is hostname specific, while the first example sets a username only. And when you use the second one you don't need to use ssh example.net; ssh example will be enough.

Solution 2 - Unix

If you only want to ssh a few times, such as on a borrowed or shared computer, try:

ssh buck@hostname

or

ssh -l buck hostname

Solution 3 - Unix

man ssh_config says

> User

Specifies the user to log in as. This can be useful when a > different user name is used on different machines. This saves the > trouble of having to remember to give the user name on the command line.

Solution 4 - Unix

There is a Ruby gem that interfaces your ssh configuration file which is called sshez.

All you have to do is sshez <alias> [email protected] -p <port-number>, and then you can connect using ssh <alias>. It is also useful since you can list your aliases using sshez list and can easily remove them using sshez remove alias.

Solution 5 - Unix

You can use a shortcut. Create a .bashrc file in your home directory. In there, you can add the following:

alias sshb="ssh buck@host"

To make the alias available in your terminal, you can either close and open your terminal, or run

source ~/.bashrc

Then you can connect by just typing in:

sshb

Solution 6 - Unix

If you have multiple references to a particular variable i.e. User or IdentityFile, the first entry in the ssh config file always takes precedence, if you want something specific then put it in first, anything generic put it at the bottom.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbukzorView Question on Stackoverflow
Solution 1 - UnixLearath2View Answer on Stackoverflow
Solution 2 - UnixgpojdView Answer on Stackoverflow
Solution 3 - Unixnes1983View Answer on Stackoverflow
Solution 4 - UnixOssView Answer on Stackoverflow
Solution 5 - UnixFrank ForteView Answer on Stackoverflow
Solution 6 - UnixPyrosezaView Answer on Stackoverflow