Unix SSH without password Unix SSH without password unix unix

Unix SSH without password


Might not be a 100% relevant but I had to do the same on Linux.

First off you want to make a new user account on the other server that has SSH access and generate an SSH keypair,
even if you're going to do this as root,
keypairs are far superior over standard passwords because they're stronger,
and they allow you to log in automatically over SSH.

There's no real way of automating the password entry process (at least, not on Linux), hence the reason SSH keys are required to do this.

You can basically send a chain of commands as a parameter to the SSH tool.
Like so,
ssh user@host "ls; cat *; yes;"
Hope that helped.


Try this:

  1. Copy your SSH public key to your clipboard (output of cat ~/.ssh/id_rsa.pub). If you don't have an SSH key pair then generate it with this tutorial.
  2. Paste your public key to your server's ~/.ssh/authorized_keys file. If it doesn't have one, create it with nano ~/.ssh/authorized_keys and paste it there.
  3. In your computer, you can run the script in the server with the following command:

    ssh user@server_ip 'bash -s' < local_script.sh

    Or if you have a single command to run then this will do:

    ssh user@server_ip "echo Test | tee output.log"
  4. If you don't like SSH asking you for the password all the time, use ssh-agent

For SQL-specific scripts, you can put all your SQL commands in a single file, say query.sql. You should copy query.sql to your server (scp query.sql user@server_ip:~/) and then run

ssh user@server_ip "mysql -uyourusername -pyourpassword < query.sql | tee output.log"

The output will be saved in output.log. Check this answer too.


There is a Linux command ssh-copy-id will do this for you, it is also available to Mac as a homebrew formula.