How to SSH in one line
How would I connect to another computer through SSH in one line? If I were to do ssh host@IP
, it would require me to enter the password in the second line. I was thinking that I could do something like this: ssh host@IP | echo password
, but that puts the password in before asking for the password.
Solution 1:
You should be using SSH keys to authenticate with rather than putting your password on the command line as it's extremely insecure.
The way this works is once you have your SSH keys set up, all you have to do is issue the command:
ssh user@host
and without typing another thing, you will be automatically logged in.
Copy SSH Public Key to Mac/FreeBSD/Linux from macOS
This assumes you have access to the remote server via password based authentication (typing in a password), and that you have already generated your private/public keypair (if not, see below). In the following example, we are using RSA. To start with let's copy the key over (be aware that the "home" directory differs between macOS, Linux, BSD, etc.):
Using SCP:
scp ~/.ssh/id_rsa.pub username@hostname:/Users/username/.ssh/
Or simply cat-ing the file to authorized_keys
(I prefer this method):
cat id_rsa.pub | ssh username@hostname ' cat >>.ssh/authorized_keys'
(Your key name may differ) If the .ssh directory does not exist on the remote server you will need to login and create it.
Now the key has been copied from the mac to the remote server. Set correct permissions for the SSH Public Key on the remote server:
chmod 600 ~/.ssh/id_rsa.pub
Next add the key to the SSH authorized_keys file, if the file does not exist create it.
If the file authorized_keys
already exists in ~/.ssh
the use the following command:
cat id_rsa.pub >> authorized_keys
If the file does not exist enter the following commands:
cat id_rsa.pub > authorized_keys
chmod 600 authorized_keys
chown user:group authorized_keys
Generate SSH Public/Private key on macOS
Open up the Terminal by going to Applications -> Utilities -> Terminal
In the terminal, use the following command to start the key generation
ssh-keygen -t rsa
Next you will be prompted to provide the location where you want to create the private key file:
Enter file in which to save the key (/Users/username/.ssh/id_rsa
):
Leave this empty to create the key in the default location, which is /Users/username/.ssh/id_rsa
. The public key file will be created in the very same location, and with the same name, but with the .PUB extension.
After you will be prompted to choose a passphrase. This is the password optional to use the private key.
Enter passphrase (empty for no passphrase):
Your SSH key is generated.
Now, keep in mind, if you put in a passphrase you will be required to enter it each time you connect. The utility ssh-agent
will keep the passphrase in memory alleviating the need to manually enter it every time you connect while you are in the same session. For more details see man ssh-agent
Solution 2:
There are several possibilities. Your example will obviously not work, but you can achieve something similar using sshpass
utility:
sshpass -p password ssh host@IP
Note, this is not recommended because the password
will be visible for other processes or in the shell history.
A much better way to do the same is to set up the passwordless authentication using SSH keys. In short:
ssh-keygen -t rsa -f ~/.ssh/id_rsa
ssh-copy-id IP