SSH (Secure Shell)¶
Introduction¶
SSH
, short forSecure Shell
, is a cryptographic network protocol used for secure communication over an unsecured network.- It allows users to securely access and control remote systems over an encrypted connection. In this tutorial, we'll cover the basics of SSH, including how to set it up, connect to remote servers, and perform common tasks.
SSH Key Generation¶
SSH keys
are a pair of cryptographic keys used to authenticate and establish a secure connection between two computers. Follow these steps to generate SSH keys:
# Generate SSH key pair
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This command will create a public key
(id_rsa.pub
) and a private key
(id_rsa
) in the ~/.ssh/
directory by default. -c "your_email@example.com"
is the comment to the key
Common SSH Commands¶
Here are some common SSH
commands:
ssh
: Connect to a remote server.ssh username@remote_host ## Example ssh john@example.com
scp
: Securely copy files between local and remote systems.scp source_file username@remote_host:destination_file ## Example scp /path/to/local/file.txt john@example.com:/path/to/remote/directory/file.txt
sftp
: Securely transfer files between local and remote systems.sftp username@remote_host ## Example sftp john@example.com
ssh-keygen
: GenerateSSH key
pairs.ssh-keygen -t rsa -b 4096 -C "your_email@example.com" ## Example ssh-keygen -t rsa -b 4096 -C "john@example.com"
ssh-add
: AddSSH private keys
to the SSH authentication agent.ssh-add /path/to/private_key ## Example ssh-add ~/.ssh/id_rsa
ssh-copy-id
: Copy yourpublic SSH key
to a remote server's authorized_keys file.ssh-copy-id username@remote_host ## Example ssh-copy-id john@example.com
exit
: close the current connection
Connecting to a Remote Server¶
To connect to a remote server using SSH
, you'll need the server's IP address
or domain name and your SSH credentials. Use the following command:
ssh username@remote_host
Replace username
with your username on the remote server and remote_host
with the server's IP address or domain name.
Connects Server using Public & Private keys¶
- Generate pair of keys using
ssh-keygen
- Copy the public SSH key to the server using
ssh-copy-id username@remote_host
- Connect Server without password verify using
ssh username@remote_host
SSH Configuration¶
SSH configurations
are stored in the ~/.ssh/config
file. You can customize SSH behavior by modifying this file.
Here's an example configuration [The default Port for SSH is 22
]:
Host example
HostName example.com
User username
Port 22
IdentityFile ~/.ssh/id_rsa
This configuration allows you to connect to the remote server by typing ssh example
.
Host gitlab.com
Hostname gitlab.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab