Using SSH in Bash
To connect to a remote host, you can use the ssh command. SSH is a secure shell, which means that it uses a secure protocol to connect to the remote host. It's usually the most secure way to connect to a remote host.
In this tutorial, we'll use the ssh command to connect to a remote host.
Generating an RSA keypair
First, we'll generate an RSA keypair. To do this, we'll use the ssh-keygen command.
ssh-keygen -t rsa
By default, the ssh-keygen command generates a public/private keypair. The -t option specifies the type of key to generate. By default, the location of the keypair is in the user's home directory in the .ssh directory.
You will be prompted for a passphrase. This is the passphrase that will be used to encrypt the private key. You can just press enter to leave the passphrase blank. But if anyone got the private key, they could use it without a passphrase.
Changing the passphrase
To change the passphrase, you can use the -P option. The command to do this is:
ssh-keygen -t rsa -p "old/existing passphrase"
You will then be prompted for a new passphrase.
Connecting to a Remote Host
To connect to a remote host, you need to specify the hostname, username, and port. You might also need to specify the private key file. Here is the bash command to connect to the remote host:
ssh -l username hostname
If you needed a private key file, you would specify it like this:
ssh -l username hostname -i ~/.ssh/id_rsa
The -i
option specifies the private key file. The ~
character is a shortcut for the home directory. So in this example, the private key file is in the .ssh directory within the home directory.
Using the ssh-copy-id command
The ssh-copy-id command is used to copy the public key from your local machine to a remote host.
ssh-copy-id -i ~/.ssh/id_rsa.pub username@hostname
Using the config file
When you run the ssh
command the ~/.ssh
is created.
- Move to the
~/.ssh
foldercd ~/.ssh
- Create a
config
filetouch config
- Set permissions
chmod 600 ~/.ssh/config
- Add references in the following format:
Host hostname1
HostName 192.168.1.11
User steve
IdentityFile ~/.ssh/host1.key