1. Automatic authentication to remote machine
Note:- with this you don't need to type password every time to do the ssh connection.
Step One—Create the RSA Key Pair (your machine)
ssh-keygen -t rsa
Step Two—Store the Keys and Passphrase (your machine)
Once you have entered the Gen Key command, you will get a few more questions:
Enter file in which to save the key (/home/demo/.ssh/id_rsa):
You can press enter here, saving the file to the user home (in this case, my example user is called demo).
Enter passphrase (empty for no passphrase):
The entire key generation process looks like this:
ssh-keygen -t rsa
Generating public/private rsa key pair. Enter file in which to save the key (/home/demo/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/demo/.ssh/id_rsa. Your public key has been saved in /home/demo/.ssh/id_rsa.pub. The key fingerprint is: 4a:dd:0a:c6:35:4e:3f:ed:27:38:8c:74:44:4d:93:67 demo@a The key's randomart image is: +--[ RSA 2048]----+ | .oo. | | . o.E | | + . o | | . = = . | | = S = . | | o + = + | | . o + o . | | . o | | | +-----------------+
The public key is now located in /home/demo/.ssh/id_rsa.pub. The private key (identification) is now located in /home/demo/.ssh/id_rsa.
Step Three—Copy the Public Key (your machine to remote_machine)
Once the key pair is generated, it’s time to place the public key on the server that we want to use.
You can copy the public key into the new machine’s authorized_keys file with the ssh-copy-id command. Make sure to replace the example username and IP address below.
2. Custom host name for ssh connection
Note:- instead of typing ``ssh firstname.lastname@example.org`` you can use ``ssh host_name`` to connect easily.
First we need to modify the config file in our machine to automatically resolve the ip address of the
host_name when we will ssh into it.
The general format looks like this
Host firsthost SSH_OPTION_1 custom_value SSH_OPTION_2 custom_value SSH_OPTION_3 custom_value Host secondhost ANOTHER_OPTION custom_value Host *host ANOTHER_OPTION custom_value Host * CHANGE_DEFAULT custom_value
To add a new host name consider this example:
Host my_remote_machine HostName 192.168.0.142 User neo
This host allows us to connect as email@example.com by typing this on the command line:
ssh -X nitin@<your-ip>
-X ensures you are doing display forwarding. to check if display forwarding is successful run
xclock if you are able to see the clock then it is successful.
SSH remote port forwarding
ssh -R 3000:localhost:8888 nitin@<your-ip>
port 3000 of remote system will be forwarded to
port 8888 on your localhost.
jupyter port forward example
suppose jupyter notebook is running on port 888 of remote system and youwant to access it on your local system
ssh -N -L localhost:3000:localhost:8888 sasha@remote
now, goto localhost:3000 on your local system and you will be able to access it.
-L binds the local_address:port1 to a remote_address:port2. To be specific, it specifies that the connections for the socket on the local host are to be forwarded to the remote host. The socket then listens to the specified bind address.
-N specifies not to execute a remote command. This is useful when forwarding ports.