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.
13 Answers
A better solution than putting an alias in your bashrc, would be to use a ssh config file
cat ~/.ssh/config
HOST * USER rootYou can also specify certain subdomains use certain users. Useful if your laptop travels between networks.
HOST 192.168.*.* USER homeuser
HOST 10.2.*.* USER workuserYou 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_rsaYou can also add sections that apply to multiple hosts, e.g.
HOST rasbpi1 rasbpi2 rasbpi3 USER piRead more about the format by executing man ssh_config or here
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 appReferences
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