How to save a frequently used SSH host for access via terminal?
Solution 1
Environment variables are for the purpose of communicating information to multiple commands/processes that you start in shell that commands or processes expect to be there in the environment. Usually such variables include options, such as LESS
variable passing frequently-used options to less
pager, PERL5LIB
for finding perl modules in non-standard locations, LC_LANG
to communicate which language a command should use for the output.
If the URL is for your own use with a specific command, use an alias such as alias au='firefox https://askubuntu.com
or a function such as open_url(){ firefox "$@" }
to open arbitrary URL that you provide on command-line as positional parameter to the function.
In certain cases such as ssh
, you can define connection properties in configuration files as explained in Lekensteyn's answer:
-
define
~/.ssh/config
file with the following contentsHost meh HostName meh.example.com User admin Port 1234 IdentityFile ~/.ssh/id_rsa
use
ssh meh
to connect to the host using the config file.
Solution 2
Also I would suggest to create an alias.
Edit your .bashrc
(or maybe .profile
or similar file) in your home directory. Add several aliases like:
alias go='ssh url1'
alias go2='ssh url2'
Then, relogin/reconnect and enter go
or go2
.
Solution 3
I'd suggest combining Sergiy's and Lewis's answers for maximum laziness efficiency:
First create a host entry for ssh
:
define ~/.ssh/config
file with the following contents
Host meh
HostName meh.example.com
User admin
Port 1234
IdentityFile ~/.ssh/id_rsa
Now ssh meh
works, but that could still be quite long. There is auto-completion (after ssh
[Blank]), but it's still an awful lot to type.
So let's also define an alias
:
Edit your .bashrc
in your local home directory. Add an alias like:
alias meh='ssh meh'
Now you can connect to "meh.example.com" by simply typing meh
in your terminal window.
If instead of "meh" you want to use a longer string, you can actually use [Tab] key to autocomplete.
Or if you are really lazy, just define a single character as an alias:
alias m='ssh meh'
So, if you type m and hit Enter/Return, your ssh connection will start immediately!
Solution 4
Yet another option is to use the history features of the shell. You can Ctrl-r in bash and type a unique substring of the last time you ssh'ed to that place. If there's no unique substring, you can just use whatever qualifies best as a rare substring and browse through the history entries by repeatedly pressing Ctr-r until you get to it. For example, you might type Ctrl-r and "monkeys" or "codemonkeys" or "odemon" or "kong" and get
ssh [email protected]
from the last, and hopefully only time you typed that whole.
With the history features of the shell, there's really no reason to type any long command more than once.
Solution 5
I usually just create a .sh
which contains a ssh command. For example:
# file name: mywebsite-live.sh
ssh [email protected] # or whatever the domain is.
And then to run it I simply do ./mywebsite-live.sh
I realise that it's probably a pretty crappy solution but it works for me and the hope is that it might work for someone else too.
Related videos on Youtube
Vaish MK
Updated on September 18, 2022Comments
-
Vaish MK over 1 year
I have a host that I frequently
ssh
into. But I don't want to enter it again and again. Should I use an environment variable for this or is there a better way?I wouldn't mind a generalized solution on saving frequently used variables that exist outside of a single bash session.
-
Admin over 5 yearsHow about saving the params in
~/.ssh/config
? Some examples are here: nerderati.com/2011/03/17/… -
Admin about 5 yearsTo expand on Roger's comment, URLs are those things that have the format of
<protocol>://<host>:<port>/<path>
(simplified example). The syntax ssh uses to specify locations is different.ssh ssh://user@server:22/
would be invalid. -
Admin about 5 yearsPossible duplicate of Host alias for ssh, and this and probably a few more.
-
-
Giacomo Alzetta over 5 yearsI'd also use
IdentitiesOnly yes
. -
Pelle about 5 yearsI think autocomplete for ssh-config 'just works' on ubuntu, and has worked as far as I can remember, the only place where I know it doesn't work out-of-the-box is MacOS
-
Robert Riedl about 5 years@Pelle, yes that's true! I better rephrase that... But you have to type out the whole of
ssh
plus the blank and at least the first character.. which is so much work if you have to do it often.. and as sysadmin you should be lazy ;) -
Roger Lipscombe about 5 yearsAlso note that by default on Ubuntu, tab completion works with ssh hosts named in the config file.
-
Jon Bentley about 5 years@Dubu I don't understand your comment. The answer clearly contains URLs. Are you saying it doesn't?
-
Jon Bentley about 5 yearsIt's not clear from your answer what the benefit of combining the two approaches is, over just using the bash alias (of course I agree there is a benefit).
-
Robert Riedl about 5 years@Jon Bentley, hm... I'll have a look tomorrow and see how I can make this clear.
-
Jon Bentley about 5 yearsSure. And just to elaborate on my comment a little, I'm contrasting this approach with doing a
alias meh='ssh [email protected] -p 1234 -i ~/.ssh/id_rsa'
which would achieve the same thing. The benefit I am thinking of of using the config file is that you can enforce theIdentitiesOnly yes
option. Also that it feels more "correct" to store your SSH hosts inconfig
rather than in some bash alias. -
Admin about 5 yearsI think the URL comment comes from referring to the connection properties in the config file as a "URL" which is not strictly accurate. I changed that wording.
-
linolino about 5 years@Pelle for me it not the case that it 'just works'. Having a large list of hosts blacklisted (0.0.0.0 meh.bad.domain). I have my RaspberryPi in ./ssh/config and just do "ssh pi" so 6 characters for my lazyness.