Skip to content

Latest commit

 

History

History
168 lines (139 loc) · 3.65 KB

File metadata and controls

168 lines (139 loc) · 3.65 KB

🔐 **SSH Command **

🖥️ 1. SSH Command Syntax

The basic structure of an SSH command:

ssh [options] user@hostname

📌 Components:

  • ssh → The command itself
  • user → Username of the remote system
  • hostname → IP address or domain of the remote system
  • options → Additional parameters (e.g., port, identity file)

🌐 2. Connecting to a Remote Server

Basic Connection

ssh user@remote_host

➡️ Connects to the remote server as user

📌 Specify a Port

ssh -p 2222 user@remote_host

➡️ Connects via port 2222 (instead of default 22)

🔑 Using an Identity File (Private Key)

ssh -i /path/to/private_key user@remote_host

➡️ Uses an SSH private key for authentication

Execute a Command on the Remote Server

ssh user@remote_host "ls -l"

➡️ Runs the ls -l command remotely and displays output locally


🔑 3. SSH Key-Based Authentication

🔹 Generate an SSH Key Pair

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

➡️ Creates a 4096-bit RSA key pair

📤 Copy SSH Key to Remote Server

ssh-copy-id user@remote_host

➡️ Enables passwordless authentication

Manually Add Public Key

cat ~/.ssh/id_rsa.pub | ssh user@remote_host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

➡️ Manually appends the public key


🔄 4. SSH Tunneling & Port Forwarding

🔹 Local Port Forwarding

ssh -L 8080:localhost:80 user@remote_host

➡️ Forwards local port 8080 to remote port 80

🔹 Remote Port Forwarding

ssh -R 9090:localhost:3000 user@remote_host

➡️ Allows access to localhost:3000 from remote host on 9090

🌍 Dynamic Port Forwarding (SOCKS Proxy)

ssh -D 9090 user@remote_host

➡️ Creates a SOCKS proxy on port 9090


⚙️ 5. SSH Config File for Easier Access

Save SSH settings for quick access in ~/.ssh/config:

Host myserver
    HostName remote_host
    User username
    Port 2222
    IdentityFile ~/.ssh/id_rsa

➡️ Now you can connect with:

ssh myserver

📂 6. Secure File Transfers (SCP & SFTP)

🔄 Copy Files with SCP

scp file.txt user@remote_host:/path/to/destination

➡️ Uploads file.txt to the remote server

📁 Copy a Directory

scp -r folder/ user@remote_host:/path/to/destination

⬇️ Download a File from Remote Server

scp user@remote_host:/path/to/file.txt .

📡 Using SFTP

sftp user@remote_host
sftp> put local_file.txt  # Upload file
sftp> get remote_file.txt # Download file
sftp> exit

🚀 7. Managing SSH Sessions

🛠️ Keep Connection Alive

ssh -o ServerAliveInterval=60 user@remote_host

➡️ Prevents SSH from timing out

Multiplexing SSH Connections (Faster SSH)

Add this to ~/.ssh/config for faster multiple SSH connections:

Host *
    ControlMaster auto
    ControlPath ~/.ssh/socket-%r@%h:%p
    ControlPersist 10m

🎭 Background SSH Sessions

ssh -f -N user@remote_host

➡️ Runs SSH in background mode


8. Closing SSH Connections

🔚 Exit SSH Session

exit

or

Ctrl + D

⚠️ Kill a Stuck SSH Session

~.

➡️ Instantly terminates the SSH session