Using ssh-copy-id & ssh-keygen Commands in Linux

Secure Shell (SSH) is a crucial tool for securely connecting to remote systems. To improve security and ease of access, SSH key authentication is often preferred over password authentication. Two important commands in this process are ssh-keygen and ssh-copy-id.

Generating SSH Keys with ssh-keygen

The ssh-keygen command is used to generate SSH key pairs. To create a new key pair, run the following command in Linux environment:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  • t rsa: Specifies the RSA algorithm.
  • b 4096: Generates a key with 4096-bit encryption.
  • C “your_email@example.com“: Adds a comment for identification.

After running the command, you will be prompted to specify a file to save the key (default: ~/.ssh/id_rsa) and an optional passphrase for added security.

Copying the Public Key to a Remote Server with ssh-copy-id

Once the key pair is generated, you need to transfer the public key to the remote server. The ssh-copy-id command simplifies this process:

ssh-copy-id user@remote-server
  • user: Your username on the remote server.
  • remote-server: The hostname or IP address of the server.

This command appends your public key (id_rsa.pub) to the ~/.ssh/authorized_keys file on the remote machine, enabling passwordless authentication.

Verifying SSH Key Authentication

To check if key-based authentication is working, try connecting to the remote server:

ssh user@remote-server

If everything is set up correctly, you should be logged in without needing a password.

Conclusion

Using ssh-keygen and ssh-copy-id enhances security and convenience when managing remote servers. By configuring SSH key authentication, administrators can eliminate the risks associated with password-based logins while streamlining secure access.