16

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.

1
  • I would even claim that aliasing any system command to itself with extra parameters is always a bad idea. You're going to have weird behavior in a year from now when you forget you've done this and spend two weeks pulling out your hair until you finally realize that you sabotaged yourself. – dovetalk Jul 20 '20 at 23:24
42

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

You can also add sections that apply to multiple hosts, e.g.

HOST rasbpi1 rasbpi2 rasbpi3
    USER pi

Read more about the format by executing man ssh_config or here

1
  • 2
    Note 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 Nov 24 '17 at 9:55
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.

1
  • Using the ~/.ssh/config file is by far the preferred approach, as yours can have unintended side effects. – dovetalk Mar 5 '20 at 3:23
0

How to have a default user, 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.