Linux Connect Remote Server using SSH
Using SSH to connect to remote servers in Linux is straightforward and involves running the ssh
command with the server’s IP address or hostname. Let’s go through the process with example commands and expected outputs.
Step 1: Basic SSH Connection
To connect to a remote server, you’ll need the server’s IP address or hostname and a user account with SSH access. The general syntax for connecting to a server is:
Example:
Example Output:
- If it’s the first time connecting to the server, you’ll see a security prompt asking if you trust the host. Type
yes
to continue. - You’ll then be prompted to enter the password for the specified user (
john
in this case). After entering the password, you’ll have access to the server’s command line.
Step 2: Connecting to a Server on a Different Port
By default, SSH runs on port 22. If the server uses a different port, specify it with the -p
flag.
Command:
Example Output:
Here, the SSH connection is made on port 2222
.
Step 3: Using SSH Key Authentication
If SSH key-based authentication is set up, you won’t need to enter a password each time. The SSH command will use your key by default if it’s located in ~/.ssh/id_rsa
.
Command:
Example Output:
Since SSH key authentication is used, the server grants access without asking for a password.
Step 4: SSH with a Different Key File
If you’re using a non-default key file, specify it with the -i
option:
Command:
Example Output:
Here, ~/.ssh/other_key
is used for authentication.
Step 5: Running a Command on the Remote Server
You can execute a single command on the remote server without starting an interactive session. This is useful for automation or quick checks.
Command:
Example Output:
The uptime
command runs on the remote server, and the output is displayed directly on your local machine.
Step 6: Transferring Files with SCP
You can use scp
(Secure Copy Protocol) to copy files between your local machine and the remote server. Here’s an example of copying a file from the local machine to the server:
Command:
Example Output:
This output shows the transfer progress and confirms the file was successfully copied.
These examples demonstrate basic SSH usage in Linux for remote connections and commands. SSH offers flexible, secure access to remote servers, making it essential for system administration and file management across networked devices.