Default SSH user on Linux

13

2

What is the entry I should add to my .bashrc file so I can specify a default user for making SSH connections? For example, if I want it to be root and want to SSH to x, if I type ssh x, it should default to a connection of ssh root@x.

By default, Linux seems to default to whatever user you are logged in as. I.e., if I am logged in as "peter", typing ssh x will result in a connection of ssh peter@x.

Keep in mind I still want to override the default at times, so doing something like

alias ssh='ssh root@'

is not the most ideal solution.

Peter

Posted 2014-02-11T05:48:37.933

Reputation: 566

Answers

33

A better solution than putting an alias in your bashrc, would be to use a ssh config file

cat ~/.ssh/config

HOST *  
     USER root

You can also specify certain subdomains use certain users. Useful if your laptop travels between networks.

HOST 192.168.*.*
     USER homeuser

HOST 10.2.*.*
     USER workuser

You could even configure by domains, and use different ssh keys for different domains.

HOST *.microsoft.com
     USER bill
     IdentityFile ~/.ssh/microsoft/id_rsa

HOST *.apple.com
     USER steve
     IdentityFile ~/.ssh/apple/id_rsa

spuder

Posted 2014-02-11T05:48:37.933

Reputation: 8 755

1Note that when ssh scans the config file it will use the first match it finds, so put the least specific matches at the bottom of the file. – mwfearnley – 2017-11-24T09:55:14.707

0

You can do an alias to ssh using the -l option, so:

alias ssh='ssh -l defaultuser'

The -l option gives the login user but what is interesting is that the user, if any, given before the host information overrides this. So if you start

ssh host

After setting the alias above it will login as defaultuser, while if you start

ssh newuser@host

Will anyway get newuser and not defaultuser from the "-l" option

This works at least on a few OpenSSH installations that come packed with standard Linux distros.

fede.evol

Posted 2014-02-11T05:48:37.933

Reputation: 1 718