I want to set an alias that would by "default" use a different username then my current one. Like so:

$ echo $USER          # outputs kamil
$ alias myssh='ssh -o User=somebody'   # non-working example of what I want to do
$ myssh server        # Uses somebody@server - all fine!
$ myssh root@server   # I want it to use root@server, but it does not. It connects to `somebody@server`!

# Easy testing:
$ myssh -v root@localhost |& grep -i 'Authenticating to'
debug1: Authenticating to localhost:22 as 'somebody'
#                                          ^^^^^^^^ - I want root!

The code above does not work - the user in root@server is overwritten by -o User=somebody. I could do something along:

myssh() {
   # parse all ssh arguments -o -F etc.
   if [[ ! "$server_to_connect_to" =~ @ ]]; then    # if the use is not specified
        # use a default username if not given
   ssh "${opts[@]}" "$server_to_connect_to" "${rest_of_opts[@]}"

but the requires parsing all ssh arguments in the function to extract server name and then add username to it. The solution would be to modify ~/.ssh/config and add Host * User somebody - however I am on a machine with no write access to home directory (and no home directory at all, actually) and I can't modify the config file and I do not want to overwrite normal ssh operation anyway.

Is there a simple solution to specify "default overridable" user to connect to server without modifying ~/.ssh/config?

3 Answers 3


Don't use an alias, just configure your ssh client. Edit (or create, if it doesn't exist) ~/.ssh/config and add these lines:

Host rootServer
HostName server_to_connect_to
User root

Host userServer
HostName server_to_connect_to
User somebody

Save the file, and you can now run ssh rootServer to connect as root and ssh userServer to connect as somebody.


Maybe you could set the username using a variable, and fall back to the default:

myssh() {
  ssh -o "User=${user:-somebody}" "$@"

And use it like so:

myssh server  # use default user
user=root myssh -v server  # use root as the username
  • tip of the day: instead of ssh -o User=me, you can use ssh -l me.
    – user414777
    Jan 22, 2021 at 18:43

I can generate a temporary file and pass that file to ssh as configuration with -F with the Host * User somebody added to it.

myssh() {
        # in a subshell, so that `trap` will not affect parent
        local tmp
        tmp=$(mktemp --tmpdir .cis-ssh-config.XXXXXXXXXXX)
        trap 'rm "$tmp"' exit
            printf "%s\n" "Host *" "  User somebody"
            # silence errors, if the files doesn't exists
            cat /etc/ssh/ssh_config ~/.ssh/config 2>/dev/null ||true
        } > "$tmp"
        ssh -F "$tmp" "$@"
  • To the downvoter: Why was this answer downvoted?
    – KamilCuk
    Jan 22, 2021 at 23:06

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.