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
server_to_connect_to="somebody@$server_to_connect_to"
fi
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
?