SSH (Secure Shell) is a protocol used to securely connect to remote systems over a network. It provides a secure channel over an unsecured network by encrypting the connection. SSH is commonly used for remote command-line login, file transfers, and secure tunneling.
- Purpose: SSH enables secure remote access to servers and devices, allowing users to execute commands, transfer files, and manage systems over a network.
- Components:
- SSH Client: The software that initiates the connection.
- SSH Server: The software running on the remote machine that accepts and manages the connection.
-
Command:
ssh username@hostname_or_ip
username
: The user account on the remote system.hostname_or_ip
: The remote system's hostname or IP address.
-
Example:
- Connects to the remote server
example.com
as the useruser
.
- Connects to the remote server
-
Command:
ssh -p port_number username@hostname_or_ip
-p port_number
: Specifies a non-default SSH port (default is 22).
-
Example:
ssh -p 2222 [email protected]
- Connects to
example.com
on port 2222.
- Connects to
-
Generate SSH Key Pair:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
-t rsa
: Specifies the RSA key type.-b 4096
: Generates a 4096-bit key (more secure).-C "[email protected]"
: Adds a comment (usually your email) to the key.
-
Copy Public Key to Remote Server:
ssh-copy-id username@hostname_or_ip
- Copies your public key to the remote server, allowing key-based authentication.
-
Example:
ssh-copy-id [email protected]
- Copies your public key to
example.com
for the useruser
.
- Copies your public key to
-
Location:
~/.ssh/config
-
Purpose: Simplifies SSH connections by storing configuration options.
-
Example Configuration:
Host example HostName example.com User user Port 2222 IdentityFile ~/.ssh/id_rsa
- Allows you to connect using
ssh example
instead of specifying the full command each time.
- Allows you to connect using
-
Command:
scp source_file username@hostname_or_ip:/remote/directory
- Copies a file from the local system to the remote system.
-
Example:
scp file.txt [email protected]:/home/user/
- Copies
file.txt
to the/home/user/
directory onexample.com
.
- Copies
-
Command:
rsync -avz source_directory username@hostname_or_ip:/remote/directory
-a
: Archive mode, preserves permissions, symlinks, etc.-v
: Verbose output.-z
: Compresses the data during transfer.
-
Example:
rsync -avz /local/dir/ [email protected]:/remote/dir/
- Synchronizes the local directory
/local/dir/
with/remote/dir/
onexample.com
.
- Synchronizes the local directory
-
Purpose: Forwards a local port to a remote network service.
-
Command:
ssh -L local_port:remote_host:remote_port username@hostname_or_ip
-L local_port:remote_host:remote_port
: Specifies local port forwarding.
-
Example:
ssh -L 8080:localhost:80 [email protected]
- Forwards local port 8080 to port 80 on
example.com
.
- Forwards local port 8080 to port 80 on
-
Purpose: Forwards a remote port to a local network service.
-
Command:
ssh -R remote_port:local_host:local_port username@hostname_or_ip
-R remote_port:local_host:local_port
: Specifies remote port forwarding.
-
Example:
ssh -R 8080:localhost:80 [email protected]
- Forwards port 8080 on
example.com
to port 80 on the local machine.
- Forwards port 8080 on
-
Purpose: Creates a SOCKS proxy to tunnel traffic through SSH.
-
Command:
ssh -D local_port username@hostname_or_ip
-D local_port
: Specifies dynamic port forwarding.
-
Example:
ssh -D 1080 [email protected]
- Sets up a SOCKS proxy on local port 1080.
- Edit SSH Configuration:
sudo nano /etc/ssh/sshd_config
- Find and set
PermitRootLogin
tono
.
- Find and set
- Edit SSH Configuration:
sudo nano /etc/ssh/sshd_config
- Change
Port 22
to a non-standard port (e.g.,Port 2222
).
- Change
- Example (Using
ufw
):sudo ufw allow 2222/tcp sudo ufw enable
- Allows traffic on the custom SSH port and enables the firewall.
- Install and Configure:
sudo apt-get install libpam-google-authenticator
- Follow instructions to set up two-factor authentication (2FA) with Google Authenticator.
SSH is a powerful tool for securely managing remote systems. By mastering SSH and its associated commands, you can effectively manage servers, transfer files securely, and even tunnel traffic through encrypted connections. Ensuring that SSH is properly secured is crucial to maintaining the integrity and security of your systems.
Next: Networking Configuration
Previous: Networking Commands