translations: [ es/Español ] · [ fr/Français ] · [ de/Deutsch ]
Table of contents
Secure Shell (SSH) is a powerful tool for accessing remote machines securely. It provides a secure channel over an unsecured network, offering encrypted communication between the client and the server. Here’s a comprehensive guide to utilizing SSH on Linux, covering connection, known_hosts, and essential commands.
Connecting via SSH
Syntax
To initiate an SSH connection, use the following syntax:
ssh username@hostname
Replace username with your username on the remote machine and hostname with the IP address or domain name of the server.
Example
Let’s say your username on the remote machine is user and the server’s IP address is 192.168.1.100. To connect, use:
ssh user@192.168.1.100
Password-based Authentication
Upon the first connection, SSH prompts for the user’s password. It’s worth noting that passwords are not visible when typed, providing an extra layer of security. Subsequent logins might not ask for the password if SSH keys are set up.
SSH Keys and Key-based Authentication
SSH keys enhance security by replacing passwords with cryptographic keys. The steps to generate and use SSH keys are:
Generate SSH Key Pair:
ssh-keygen -t rsa -b 4096This command generates an RSA key pair (
id_rsaandid_rsa.pubfiles) in the~/.ssh/directory by default.Copy Public Key to Remote Machine: Use
ssh-copy-idto add the public key to the remote machine’sauthorized_keysfile:ssh-copy-id user@hostnameKey-based Authentication: Once the key is copied, SSH won’t prompt for passwords during subsequent logins.
Managing known_hosts
What is known_hosts?
The known_hosts file stores host keys of remote servers. When connecting to a server, SSH checks this file to verify the server’s identity. If the server’s key changes, SSH displays a warning.
Handling known_hosts Issues
Sometimes, SSH might throw warnings due to host key changes, IP changes, or when connecting to a new server. To resolve these issues:
Removing Entries: To remove a specific entry from
known_hosts, use:ssh-keygen -R hostnameClearing Entire
known_hosts: This command clears the entireknown_hostsfile:> ~/.ssh/known_hosts
Essential SSH Commands
ssh-agent
The ssh-agent is a program to hold private keys used for public key authentication. It allows keys to be used without re-entering passphrases.
eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa
scp - Secure Copy
scp securely transfers files between a local and a remote host.
scp /path/to/local/file user@hostname:/path/to/remote/directory
sshfs - SSH File System
sshfs enables mounting a remote filesystem over SSH.
sshfs user@hostname:/remote/directory /local/mount/point
Utilizing SSH on Linux provides a secure and versatile way to interact with remote systems. By understanding its fundamentals and commands, users can navigate and manage remote machines efficiently and securely.
Created on: Jan 7, 2024
Discover More within this Subject:
- Usage of Tar Command for File Archiving in Linux
- Ways to Check Linux Distribution and Kernel Version
- Understanding `kill` Command in Linux
- Understanding `ps` Command in Linux
- Essential Linux Network Commands for Webmasters