Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 17 down vote accepted

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
share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.