337

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.

Sergey Nemchinov
  • 1,348
  • 15
  • 21
bukzor
  • 37,539
  • 11
  • 77
  • 111
  • 4
    Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. Also see [Where do I post questions about Dev Ops?](http://meta.stackexchange.com/q/134306) – jww Jan 26 '17 at 09:10
  • 8
    This is such a good question! – Nicolas S.Xu Dec 28 '18 at 03:24
  • accepted answer here is better quality than superuser ones: includes global (default) user directive use and comments added make directive priorities clear – Rondo May 03 '19 at 01:55
  • More about ssh config priorities, default and host-specific users and such: https://therootcompany.com/blog/ssh-defaults-config-and-priorities/ – coolaj86 Oct 12 '20 at 10:36

6 Answers6

600

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Learath2
  • 20,023
  • 2
  • 20
  • 30
  • 170
    It's probably worth pointing out that according to `man ssh_config`: `Since the first obtained value for each parameter is used, more host-specific declarations should be given near the beginning of the file, and general defaults at the end.` So the `Host *` section should probably go at the end. – quodlibetor Sep 23 '13 at 14:27
  • 15
    It is worth noting that config file will be processed in top-to-bottom fashion, populating each field (User, HostName...) and _skipping_ repeated fields if multiple Host directives are matched. If you define `Host *`, `User jdoe` at the top, and then define `Host example`, `HostName abc.example.com`, `User root`, attempting `ssh example` will be the same as if you entered `ssh jdoe@abc.example.com`. In order to define ssh defaults (ie. `User root`), `Host *` directive needs to be _at the bottom_ of config file. – mr.b Oct 09 '13 at 11:41
  • 19
    Also, don't forget chmod 600 on .ssh/config – Sven Jan 15 '14 at 11:09
  • Note that if there are multiple hostnames that the server is accessible by (or if you want the same default username for multiple servers), you should change the first line to `Host server1.local example.net *.example.org` and just get rid of the `HostName` line. – Lèse majesté Jun 14 '17 at 05:21
52

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
gpojd
  • 22,558
  • 8
  • 42
  • 71
  • 2
    Thanks, but I'm already familiar with this. It seems redundant to specify buck@host when it's *always* buck@host. I'm trying to find a method to represent this information in a configuration file. – bukzor Apr 17 '12 at 20:21
  • 2
    Have you considered something as simple as alias sshhostname='ssh buck@hostname'? – gpojd Apr 17 '12 at 20:24
  • Yes, I have. Learath2's answer is exactly what I was looking for. I can check this into my dotfiles repository and not worry about it ever again. – bukzor Apr 17 '12 at 20:25
  • I took the alias root because I connect to machines for different clients, where my username is different in each client environment. Another approach I suppose would be to have different local shell environments to set the ssh and related settings for each client. Or a VM per client. – Nick Spacek Mar 15 '13 at 15:15
8

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.

Pyroseza
  • 111
  • 1
  • 2
  • This is *exactly* what I'm trying to figure out! Just move my `Host *` to the end of the file and everything works like I want. – donatJ Aug 26 '22 at 16:54
6

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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
nes1983
  • 15,209
  • 4
  • 44
  • 64
  • 1
    "Identity" in this case is equivalent to the usual usage of "password". This is the data which authenticates you, but doesn't help specify who you are trying to authenticate _as_. – bukzor Apr 17 '12 at 20:19
  • Oops. There's a user setting in there as well. – nes1983 Apr 17 '12 at 20:25
0

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

All you have to do is sshez <alias> username@example.com -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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Oss
  • 4,232
  • 2
  • 20
  • 35
0

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Frank Forte
  • 2,031
  • 20
  • 19
  • 1
    This is great for one specific host, but as I keep connecting to a trillion different servers, this is rather useless. Rather set up `.ssh/config` with the appropriate settings. I came looking for the `*` wildcare (and learned that it should be at the bottom). – mazunki May 15 '20 at 22:03
  • In most/good cases there is ssh autocompletion (based on hosts declared in config, in authorized_keys, in known_hosts), so TAB can sometimes just be enough to write everything. Plus the shell keeps history of previous commands normally, so as soon as typed once you shouldn't ever have to type it in full again. – Patrick Mevzek Jan 16 '23 at 23:34