I'm using Ubuntu 11.10 and ssh
for connecting to many servers daily, so I put their parameters in the .ssh/config
file like this:
Host Home
User netmoon
Port 22
HostName test.com
Is there a way to put passwords for each connection in this file, so that, when the server asks for the password, the terminal enters its password and sends it to the server?
I need this because sometimes I'm away from the PC and when I get back, type a password, and press Enter the terminal says CONNECTION CLOSED
.
P.S. I don't want to use a public/private key pair.
Trading off security for convenience never ends well...
Could you use
ssh-copy-id
from theopenssh-client
package?From
man ssh-copy-id
:If you don't really want to use a public/private key pair, you can write an
expect
script to enter the password for you automatically depending on the destination address.Edit: What I mean is that you can have a script that, on one hand, uses
expect
to enter the password for you and, on the other hand, reads the password for a given user and host from a configuration file. For example, the following python script will work for the sunny day scenario:and the configuration file format would be as follows:
Note: As explained, the python script would need to be much more complex to handle all the possible errors and question messages from ssh and all the possible URLs (in the example it's assumed that it will be something like
user@host
, but the user part isn't used most of the times), but the basic idea would still be the same. Regarding the configuration file, you may use a different configuration file or use.ssh/config
and write your own code to parse that file and get the password for a given user and host.How about ProxyCommand:
You can use
ssh -W
instead ofnc
as well:There also is
sshpass
program for that. How to use:sshpass -p MyPa55word ssh [email protected]
No. This is not possible I'm afraid.
The only real alternative is to use private keys but you've said you don't want to (why not?).
You can create a simple ssh script replacement in /usr/local/bin:
And then in your ~/.ssh/config file you can use
Answering the question you asked, no it's not possible to configure a default password in an ssh config file.
But if indeed, as you say, it's "because sometimes I stand away from the PC and when I go back, type a password and press Enter the terminal says
CONNECTION CLOSED
", then why not prevent closing the session instead? SSH can keep connections alive for you.I use an application from VanDyke Software called SecureCRT.
http://www.vandyke.com/products/securecrt/
It is not free, but very reasonably priced. I have used it for years (running on Windows, or using Wine) for remote access, terminal emulation, and (dispersed) network management. They finally released a native Linux version of this at the beginning of 2011.
It has support for complex login settings (or scripts), stored passwords (or certificates), tabbed multiple sessions, etc.
At startup you can choose which remote target (and protocol) from a structured list (tree view) of stored remote (or local) machines, or just create a connection (which is then stored).
I have found it particularly useful for remote sites with advanced authentication, non-standard ports, or firewall-access negotiation.
If you are doing remote access a lot (part of your main role), then this application will justify its expense in the first month of use.
Inspired by @Arek Burdach's answer and others' wrapper, I've wrote a wrapper that should be more robust which facilitated ssh own command parsing.
UPDATE 2021-04-26: Fixed wrapper when a host only matches the prefix (e.g. Host is none)
Here's how it work:
debug1: /home/misty/.ssh/config line 42: Applying options for XXXXXX
. But the ssh output the errlog with \r, so we replaced them before passing into grep.According to my test, ssh will only recognize the first -o ProxyCommand=XXX in commandline, even if there's second ProxyCommand option in the cmdline, or there's ProxyCommand in ~/.ssh/config, so our method will be very very stable ;)
For ssh
For scp:
Thanks, Arek for the inspiration...
Rather than running another shell process, this is just a function running in the current bash shell. It runs a single
awk
command to parse the config file and figure out if it should take the password from a shell variable or from the password written cleartext into the ssh config file (withawk
in aneval
instead ofdeclare
due to issues I hit usingdeclare
).I tried so many ways of using
sshpass
directly in anssh
config file using ProxyCommand, but nothing seemed to work as expected, except when I could log in to a box via RSA and then I needed to send a password to open my encrypted directory. However, my function below seems to work for me in all cases, even for Cygwin.Then a
~/.ssh/config
section looks like this:If a
#Passvar
exists in the config section this overrides the#Password
.$MYPASS_ENVVAR
is the environment variable holding your password.Enjoy!
And something to handle most scp scenarios (not fully fleshed but at least a start...)