# SSH Commands 101: A Comprehensive Guide SSH (Secure Shell) is a protocol used to securely log onto remote systems. It is the most common way to access remote Linux and Unix-like servers. This guide introduces you to the basics of SSH operations, including establishing secure connections, generating SSH keys, copying files securely, and advanced SSH usage. ## Generating SSH Keys - **Generate a New SSH Key Pair:** `ssh-keygen -t rsa -b 4096` Generates a new SSH key pair using the RSA algorithm with a 4096-bit length. - **Generating a Public Key from an Existing Private Key:** `ssh-keygen -y -f /path/to/privatekey > /path/to/publickey.pub` Generates the public key from an existing private key. ## SSH Usage - **Logging into a Remote Machine:** `ssh username@hostname` Logs you into the remote machine with the specified username. - **Executing a Command on a Remote Machine:** `ssh username@hostname command` Runs a single command on the remote machine. - **Using a Private Key for Authentication:** `ssh -i /path/to/privatekey username@hostname` Specifies the private key to use when logging into the remote system. - **Copying Public Key to a Remote Host:** `ssh-copy-id -i ~/.ssh/mykey.pub username@remote_host` Automates the process of installing your public key in the `~/.ssh/authorized_keys` file on the remote host. ## SCP Usage - **Copy a File from Your Local Machine to a Remote Machine:** `scp -i /path/to/privatekey localfile username@hostname:/remote/directory` Securely copies a file from your local machine to the specified directory on the remote machine using a private key for authentication. - **Copy a File from a Remote Machine to Your Local Machine:** `scp -i /path/to/privatekey username@hostname:/remote/file localdirectory` Securely copies a file from the remote machine to the specified directory on your local machine using a private key for authentication. ## Advanced SSH Usage - **Port Forwarding:** `ssh -L localPort:remoteAddress:remotePort username@sshServer` Forward a local port to a remote address and port through an SSH server. - **Dynamic Port Forwarding (SOCKS Proxy):** `ssh -D localPort username@sshServer` Create a SOCKS proxy on the specified local port. Useful for securely browsing the internet through the server. ## Tips for Managing SSH Connections - **Using SSH Config File:** Customize connection settings in `~/.ssh/config` for easier access and command execution. - **Keep SSH Sessions Alive:** `ssh -o ServerAliveInterval=60 username@hostname` Prevents SSH sessions from timing out due to inactivity. This guide covers essential and some advanced SSH commands for secure remote connections, file transfers, and key management. As you become more familiar with these operations, they will significantly enhance your remote server management capabilities.