Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create script/shortcut to login via ssh

My issue is that everytime I have to login to a given account on a linux server (there are many) I have to go pull a text file not I have to look at the username and ip.

Example: "ssh [email protected] -pxxxxx"

I want to make my life a little easier by creating a shortcut, e.g. "ssh some_user"...

I searched and could not find an answer, likely not using the right terminology.

Thanks!

like image 803
James Robert Casey Jones Avatar asked Mar 24 '26 00:03

James Robert Casey Jones


1 Answers

You can use the ssh client configuration file (.ssh/config). If you have to type ssh -p 1234 [email protected], you can populate your config file with

host server
    hostname my.server.with.a.long.name.com
    user mylogin
    port 1234

Then you can simply type ssh server and it will have the same effect. You can have as many entries in your .ssh/config file as you want and even use wildcards (*)

If you are using a recent version of bash, you can furthermore make use of the command_not_found_handle function:

command_not_found_handle () {
    if grep "host $1" ~/.ssh/config &>/dev/null; then
        ssh $@
    else
       printf "Sorry: Command not found: $1\n"
       return 127
    fi
}

Then you can connect simply with

server
like image 183
damienfrancois Avatar answered Mar 27 '26 03:03

damienfrancois



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!