💻Setting Up SSH

In this tutorial we will learn how to setup an SSH between your local machine and a server.

Generating an SSH key

If you do not have an existing .ssh directory on your computer, you can generate a new SSH key pair using the following steps:

  1. Make sure that SSH is installed on your system.

  2. Open your terminal or command prompt.

  3. Type the following command to generate a new SSH key pair:

    ssh-keygen -t ed25519 -C "email"

    Replace "email" with your email address.

  4. Follow the prompts to save the key pair to the default location (~/.ssh/id_ed25519), or specify a different location if desired.

  5. Optionally you can also pass -f to mentioned the filename

    ssh-keygen -f ~/tatu-key-ecdsa -t ecdsa -b 521

  6. You should now have a ssh key generated in your .ssh folder

Setting up your machine

Generating an RSA SSH key

To generate an RSA SSH key, you can use the ssh-keygen command with the following options:

ssh-keygen -t rsa -b 4096 -f .ssh/name_of_your_key

This will generate a new RSA SSH key pair with a key length of 4096 bits and save the private key to the file .ssh/name_of_your_key in your home directory.

Copying the public key to a remote server

To copy the public key to a remote server, you can use the scp command with the following options:

scp -P 1122 .ssh/name_of_your_key.pub username@ip:~/name_of_your_key.pub

This will copy the public key file .ssh/subnet_blockchain.pub to the remote server at IP address IP, using the SSH port 1122, and save it as name_of_your_key.pub in the home directory of the root user.

You can then use the private key file name_of_your_key on your local machine to authenticate with the remote server using SSH.

Adding the public key to the authorized keys file

After copying the public key to the remote server, you need to add it to the authorized keys file to enable SSH authentication using the new key. Here are the steps to do this:

  1. Open a SSH session to the remote server using your existing SSH credentials.

  2. Append the contents of the public key file to the authorized keys file using the following command:

    cat ~/name_of_your_key.pub >> ~/.ssh/authorized_keys

    This will add the public key to the end of the authorized keys file.

  3. Set the correct permissions on the authorized keys file using the following command:

    chmod 600 ~/.ssh/authorized_keys

    This will restrict access to the authorized keys file to the owner of the file only.

  4. Remove the public key file from the remote server using the following command:

    rm ~/name_of_your_key.pub

    This will delete the public key file from the remote server, as it is no longer needed.

You can now use the private key file name_of_your_key on your local machine to authenticate with the remote server using SSH.

Connecting to the remote server using SSH

To connect to the remote server using SSH with your newly generated key, you can use the following command:

ssh -i .ssh/name_of_your_key -p 22 root@IP

This will start an SSH session to the remote server at IP address, using the private key file .ssh/name_of_your_key for authentication and the SSH port 22.

By default the ssh port is at 22 so -p is only required if ssh is configured on a different port.

Make sure to replace root with the appropriate username if you are not logging in as the root user on the remote server.

Last updated