Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I need to login to a user that I've created on a remote host running Ubuntu. I can't use an ssh key because the ssh login will happen from a bash script ran within a server that I won't have access to (think continuous integration server like Bamboo).

I understand this isn't an ideal practice, but I want to either set the remote host to not ask for the password or be able to login with something like ssh --passsword foobar user@host, kind of like MySQL allows you to do for logins.

I'm not finding this in man ssh and I'm open to any alternatives to getting around this issue.

share|improve this question

marked as duplicate by Eric Renouf, vonbrand, don_crissti, cuonglm, Ramesh Oct 25 '15 at 17:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

On Ubuntu, install the sshpass package, then use it like this:

sshpass -p 'YourPassword' ssh user@host

sshpass also supports passing the keyboard-interactive password from a file or an environment variable, which might be a more appropriate option in any situation where security is relevant. See man sshpass for the details.

share|improve this answer
Althought is not recommended and not a good practice this is exactly the answer to the question. Consider using keys as stated above. But if there's a major tech issue this is the solution asked – theist Mar 15 '13 at 17:14
I only upvote the answer. Not "rational why don't you do it this way instead" answers. Just the answer. hence, I upvoted you :) – Henley Chiu Mar 2 '14 at 1:05
Also note that other users on your machine will probably be able to see your password by running w. – wchargin Dec 11 '14 at 17:23
@WChargin For a more detailed explanation of how to "secure" the password from process listings by other users, have a look at this similar question. – likeitlikeit Dec 11 '14 at 21:09
@Brad If you have root on the box, you can install sshpass using yum --enablerepo=epel install sshpass. – likeitlikeit Oct 12 '15 at 10:40

If your alternative is to put a password into a script or ssh command line or plain text file, then you're MUCH better off using an ssh key instead. Either way, anyone who has access to the account where the ssh client script is stored would be able to use that to get into the server, but at least in the case of an ssh key, OpenSSH supports it properly, you don't grant access by other means than ssh, it's more easily revoked if necessary, etc...

You will have to explain why you have a requirement to not use an ssh key.

Consider also using a forced command (command="..." in the .ssh/authorized_keys file) so that the client only has access to run the command they need on the server rather than a full shell.

share|improve this answer
The remote host is actually a VM used by other engineers with no resources worth risking other than copies of test automation code. For the sake of the discussion, let say the only access I have is to add the script file, not add ssh keys in ~/.ssh/. – Michael M May 16 '12 at 0:20
That's highly contrived. A somewhat less contrived scenario would be that a misguided administrator of the server disabled ssh key logins (PubkeyAuthentication no in /etc/ssh/sshd_config). In either case, the better solution is to fix the underlying problem that prevents you from doing ssh key logins. Failing that, consult the question pointed to by Gilles. – Celada May 16 '12 at 0:29
@MichaelM you dont have to add ssh keys in ~/.ssh/. Add the key wherever you want and use ssh -i /path/to/id_rsa – Patrick May 16 '12 at 2:27
Loging in to a server with a keypair is much easier to script than a password. If it is the first time you're setting up keys for use with SSH, you might want to look for a good howto. – jippie May 16 '12 at 6:58
@MichaelM if the only access you have is to add the script file, then you can hardcode the key in the script file: echo -----BEGIN RSA PRIVATE KEY----- > ${IDENTITY_FILE} ; echo MIIEoQIBAAKCAQEAv1tQry1qWlLn1Kp3uX2/4bT0z9Cbre/zj1fnchVinPqBHrd1 >> ${IDENTIFY_FILE} ... – emory May 18 '12 at 0:20

First of, like the other respondents, I recommend just using ssh keys. But I will assume that the person controlling the server is simply not going to allow you to use ssh key authentication and you must use password authentication.

You can use ControlMaster and ControlPath.

Let A be the server that you wont have access to (think continuous integration server like Bamboo) and C be the remote host running Ubuntu.

Now let B be some computer that you control. If you can not provide a suitable B computer, this answer will not work.

  1. Create a key pair and add the public part to B's authorized_keys file. Give A the private key. Now you can log into B from A without a password.
  2. On B manually ssh -M -S /tmp/controlpath C and enter your password at the prompt. After that you should be able to log into C from A without a password ssh -S /tmp/controlpath C

In the script on A you can write ssh B ssh C dostuff.

Every time you reboot B, you will have to reestablish the connection ssh -M -S /tmp/controlpath C

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.