21

When I ssh between different pcs I can omit my username (tom) and just type

ssh pc_name

instead of

ssh tom@pc_name

I like this feature, and have got into the habit of using it.

Unfortunately, on one of my computers I went for the user name tommy. Everytime I connect to this computer I forget to write tommy@creative_pc and wonder why my password doesn't work. Is there a way to tell ssh what user name to use when the username is omitted?

Edit: Just found the following question that is similar: How to make ssh log in as the right user? It didn't come up on my initial search.

Tom
  • 1,317
  • 2
  • 10
  • 12

2 Answers2

36

Sure:

$ ssh -l tommy

will log you in as tommy.

You can also make this persistent per-host by having a record like this in ~/.ssh/config:

Host creative_pc
User tommy
HostName creative_pc # put the full host name here or the IP if it is static

then you just do:

$ ssh creative_pc # this is the string from Host setting

and you login there as tommy by default

vtest
  • 5,130
  • 2
  • 27
  • 27
  • 1
    Perfect response. @Tom - if you want more info, this is discussed in the ssh man page: http://linuxmafia.com/pub/os2/stahl-ssh/snafu-mirror/ssh.html – James T Snell Jul 04 '11 at 15:16
  • For those of you wanting a default login-user for ALL remote servers. Use '*' as Host wildcard (Host *). – Langusten Gustel May 23 '13 at 15:43
  • 1
    For myself, I have a single User line in the file and this set it globally. No Host required. – Robert Jun 24 '13 at 17:45
  • I believe the "HostName" field is optional. It was for me. Thanks for the tip!! – macetw Nov 03 '16 at 16:16
  • https://superuser.com/questions/64996/how-to-make-ssh-log-in-as-the-right-user/1484694#1484694 is the only appropriate answer. – hopeseekr Sep 20 '19 at 19:32
7

I have a large list of servers, so I used a shell alias to set a default user for all hosts. Put bellow line on your ~/.bashrc:

alias ssh="ssh -l default_user"

You can still set another user, using -l:

ssh server -l other_user

I started using today, seems working fine on Ubuntu 12.

  • Here's a much better way to do it: https://superuser.com/questions/64996/how-to-make-ssh-log-in-as-the-right-user/1484694#1484694 – hopeseekr Sep 20 '19 at 19:34