Remote Access to Server
The ssh (Secure Shell) command is used to securely connect to remote servers and manage them via the command line. It’s commonly used by system administrators and developers to access remote machines, transfer files, or run commands on remote systems.
Here’s how to use the ssh command:
Basic Syntax:
ssh [username]@[hostname_or_ip]
[username]: The username on the remote server you want to log into. [hostname_or_ip]: The IP address or domain name of the remote server. Example: To connect to a server with IP address 192.168.1.100 using the username user1, you would run:
ssh user1@192.168.1.100
Steps:
Open Terminal: On Linux or macOS, open your terminal. On Windows, you can use an SSH client like PowerShell (Windows 10/11), PuTTY, or the Windows Subsystem for Linux (WSL).
Run the SSH command: Type the ssh command followed by the username and IP address or hostname of the remote server.
First-time Connection: When you connect for the first time, you might see a message asking if you want to continue connecting. Type yes to proceed.
Password: If you’re not using an SSH key for authentication, the system will prompt you for the password of the username you’re logging in as. Enter the password.
Connected: Once authenticated, you’ll be logged into the remote machine, and you’ll be able to run commands as if you were sitting in front of it.
Using SSH with SSH Keys (more secure):
Instead of using a password, SSH keys can be used for a more secure, password-less login.
Generate SSH Key (if you don’t have one):
ssh-keygen -t rsa -b 2048
This will create a public/private key pair (by default in ~/.ssh/id_rsa).
Copy your SSH Key to the Remote Server:
ssh-copy-id user1@192.168.1.100
This command copies your public key to the remote server’s authorized keys file, allowing you to log in without a password.
Log in Using SSH Key: Now, you can log in without entering your password:
ssh user1@192.168.1.100
Common SSH Options:
-p: Specify a custom port if the server uses a non-default SSH port.
ssh -p 2222 user1@192.168.1.100
-i: Specify a custom private key file.
ssh -i /path/to/private_key user1@192.168.1.100
-v: Enable verbose mode to show debug information for troubleshooting.
ssh -v user1@192.168.1.100
Exiting an SSH Session:
To exit the SSH session and return to your local machine, simply type:
exit
Summary:
The ssh command is a powerful tool for securely managing remote systems, and it supports additional options for authentication, custom ports, and debugging. You can use it for everything from managing servers to transferring files with scp or setting up automated tasks with scripts.