Glam Prestige Journal

Bright entertainment trends with youth appeal.

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

3 Answers

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

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

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy