The Wayback Machine - https://web.archive.org/web/20241126230807/https://www.geeksforgeeks.org/ssh-command-in-linux-with-examples/
Open In App

How to use SSH to connect to a remote server in Linux | ssh Command

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Secure Shell, commonly known as SSH, is like a super-secure way to talk to faraway computers, called servers. It’s like a secret tunnel on the internet that keeps your conversations safe and private. Imagine you’re sending a letter, and instead of sending it openly, you put it in a magic envelope that only you and the person you’re sending it to can open. That’s what SSH does for your computer talks.

This article is here to help beginners, those who are just starting with this stuff, to understand how to use SSH. We’ll show you the steps to use a special command (think of it like a secret handshake) to connect your computer to a faraway server in the world of Linux. By the end of this guide, you’ll be more confident in using SSH to make your computer talks safe and secure when dealing with those remote servers.

What is SSH ?

SSH, or Secure Shell, constitutes a cryptographic network protocol designed to enable secure communication between two systems over networks that may not be secure. This protocol is widely employed for remote access to servers and the secure transmission of files between computers. In essence, SSH acts as a secure conduit, establishing a confidential channel for communication in scenarios where the network may pose security risks. This technology is instrumental for professionals seeking a reliable and secure method of managing servers and transferring sensitive data across computers in a controlled and protected manner. ssh runs at TCP/IP port 22. 

Syntax of SSH Command in Linux

The basic syntax for using the SSH command is as follows:

ssh [username]@[hostname or IP address]

Here,

Replace [username] with your remote server username, and [hostname or IP address] with the server’s hostname or IP address.

Prerequisites

Before delving into the world of SSH, it’s essential to ensure that certain prerequisites are in place. Here’s a checklist to guarantee a smooth connection:

  1. Remote Computer Status:
    • Make sure the remote computer is turned on and has an active network connection. SSH relies on network connectivity, and the remote server needs to be accessible.
  2. Identification Information:
    • Obtain the IP address or the name of the remote machine. This information is crucial for directing your SSH connection to the correct server.
  3. Permission to Access:
    • Ensure that you have the necessary permissions to access the remote computer. This typically involves having a valid username and password for the remote server.
  4. Firewall Settings:
    • Check the firewall settings on both your local machine and the remote server. SSH connections use a specific port (usually port 22), so it’s crucial to ensure that your firewall allows SSH traffic. Adjustments may be required to permit secure communication.

Install SSH Component on Linux

Setting up SSH on Linux may be necessary, as some distributions don’t come with it pre-installed. Installing OpenSSH, a widely used SSH implementation, or opting for a graphical user interface (GUI) solution like the PuTTY client for Ubuntu can address this. Here’s a step-by-step guide on installing and configuring OpenSSH on both the client and server sides:

Installing it on Both Client and Server

For Debian/Ubuntu-based Systems, open the terminal and run:

sudo apt install openssh-client openssh-server

For Red Hat-based systems like CentOS or Fedora, use either of the following commands:

sudo dnf install openssh-clients openssh-server

or

sudo yum install openssh-clients openssh-server

Note : To check the status of running server after installation we can use this command “systemctl status sshd”

In case the service is not running, run it with the following command:

sudo systemctl start sshd

The command does not print an output.

To have the service start automatically on boot, run:

sudo systemctl enable sshd

Enabling the sshd service starts it during the boot process.

How to Use SSH to Connect to a Remote Server in Linux

In this example we access Ubuntu or Red hat Linux machine via the Windows command prompt using `ssh`

For example: If our IP address is “10.143.90.2” and username is “Jayesh”

Syntax to use ssh to connect to a remote server:

ssh jayesh@10.143.90.2

Add your username in place of “Jayesh” and add your IP address in place of “10.143.90.2”

ssh to linux system from windows

ssh to linux system from windows

command consists of 3 different parts:

  • ssh command instructs the system to establish an encrypted secure connection with the host machine.
  • user_name represents the account that is being accessed on the host.
  • host refers to the machine which can be a computer or a router that is being accessed. It can be an IP address (e.g., 192.168.1.24) or domain e.g., www.domainname.com).

Note: After logging into the host computer, commands will work as if they were written directly to the host terminal. Using a public-private key pair or SSH key pair to login into the remote host is more secure as compared to using passwords. 

How to create public-private keys?

For generating public-private keys use the command:

ssh-keygen
ssh-keygen

ssh-keygen

The private key must remain hidden while the public key must be copied to the remote host. After copying the public key to the remote host, the connection will be established using SSH keys and not the password. 

Options available in ssh

Note: Here instead of user and host add username and IP address you want to connect to. And localhost is IP of our local system.

Options Description  Syntax
-1  Forces ssh to use protocol SSH-1 only.
ssh -1 user@host

-2  Forces ssh to use protocol SSH-2 only.
ssh -2 user@host

-4 Allows IPv4 addresses only.
ssh -4 user@host

-6 Allows IPv6 addresses only.
ssh -6 user@host

-A Authentication agent connection forwarding is enabled.
ssh -A user@host

-a Authentication agent connection forwarding is disabled.
ssh -a user@host

-C Compresses all data (including stdin, stdout, stderr, and data for forwarded X11 and TCP connections) for a faster transfer of data.
ssh -C user@host

-c Selects the cipher specification for encrypting the session. Specific cipher algorithm will be selected only if both the client and the server support it.
ssh -c aes256-cbc user@host

-f Requests ssh to go to background just before command execution.
ssh -f user@host command

-g Allows remote hosts to connect to local forwarded ports.
ssh -g -L 8080:localhost:80 user@host

-n  Prevents reading from stdin.
ssh -n user@host command

-p  Port to connect to on the remote host.
ssh -p 2222 user@host

-q Suppresses all errors and warnings
ssh -q user@host

-V Display the version number.
ssh -V

-v  Verbose mode. It echoes everything it is doing while establishing a connection. It is very useful in the debugging of connection failures.
ssh -v user@host

-X Enables X11 forwarding (GUI Forwarding).
ssh -X user@host

The Three Major Encryption Techniques Used by SSH.

SSH is significantly more secure than the other protocols such as telnet because of the encryption of the data. There are three major encryption techniques used by SSH:

  • Symmetrical encryption: This encryption works on the principle of the generation of a single key for encrypting as well as decrypting the data. The secret key generated is distributed among the clients and the hosts for a secure connection. Symmetrical encryption is the most basic encryption and performs best when data is encrypted and decrypted on a single machine.
  • Asymmetrical encryption: This encryption is more secure because it generates two different keys: Public and Private key. A public key is distributed to different host machines while the private key is kept securely on the client machine. A secure connection is established using this public-private key pair.
  • Hashing: One-way hashing is an authentication technique which ensures that the received data is unaltered and comes from a genuine sender. A hash function is used to generate a hash code from the data. It is impossible to regenerate the data from the hash value. The hash value is calculated at the sender as well as the receiver’s end. If the hash values match, the data is authentic.

How to use SSH to connect to a remote server in Linux | ssh Command – FAQs

What is the full form of SSH?

SSH stands for “Secure Shell”.

What is SSH used for?

SSH is used to securely connect to a remote system or server. It can be used to transfer data between two connected systems.

What port does SSH run on?

SSh runs at TCP/IP port 22.

How can we access a Linux Machine via the Windows command prompt using SSH?

We can access a Linux machine via the windows command by using SSH

syntax:

ssh user_name@host(IP/Domaimn_name)

How can we create public-private keys using SSH?

By using SSH by using the command

ssh-keygen

What are the three major encryption techniques used by SSH?

The three major encryption techniques used by SSH are:

  • Symmetrical encryption
  • Asymmetrical encryption
  • Hashing

How do I use SSH to connect to a remote server in Linux?

Replace `username` with your actual username and `remote_server_ip` with the IP address or domain of the remote server.

ssh username@remote_server_ip

What is the SSH command for connecting to a server with a specific port?

This command connects to the remote server using port 2222. Adjust the port number as needed.

ssh -p 2222 username@remote_server_ip

How can I use SSH to transfer files between my local machine and a remote server?

Using SCP for file transfer. This command securely copies the local file to the specified destination on the remote server. Adjust the file paths and names accordingly.

scp local_file.txt username@remote_server_ip:/path/to/destination/

Conclusion 

In this article we discussed Secure Shell (SSH) which is like a secret, safe tunnel for computers to talk securely over the internet. This guide is for beginners, helping them use SSH to connect their computer to faraway servers in the Linux world. It covers everything from the basic SSH command to prerequisites like checking your internet connection and having the right permissions. You’ll learn how to install SSH on Linux, create secure keys, and use them for safer logins. The article also includes frequently asked questions with simple answers, making sure you can confidently and securely manage remote servers using SSH in Linux. It’s like giving your computer a secret code to talk safely on the internet!



Previous Article
Next Article

Similar Reads

How To Generate SSH Key With ssh-keygen In Linux?
Secure Shell(SSH) is a cryptographic network protocol used for operating remote services securely. It is used for remote operation of devices on secure channels using a client-server architecture that generally operates on Port 22. SSH is the successor of Telnet. SSH uses public and private keys to validate and authenticate users. ssh-keygen is use
4 min read
How to Use Glances to Monitor Remote Linux in Web Server Mode?
Glances is a system monitoring tool for Linux machines, it is used to monitor system resources in web server mode or through the web browser. It is an alternative to top and htop monitoring tools. This tool has various features and also provides bits of information on a single screen. Glances is a cross-platform, free and open-source tool for monit
3 min read
How to Connect an Android Phone to Linux via KDE Connect?
Nowadays while working on a PC most of the time we need to use our phones to transfer data and files to or from a computer. Therefore there is always a need for software that connects our mobile phones to a PC so that we can not only share our files between them but also gain complete remote control of them in order to see important notifications a
3 min read
Enabling Remote Desktop and SSH access to Kali
Remote access and control of computers is a key feature in modern connected and internet-based societies especially in the field of information technology and cybersecurity for working from home. Some of the most influential protocols that make this possible include the remote desktop and SSH which have truly transformed the way we operate computer
5 min read
Linux SSH Server (sshd) Configuration and Security Options With Examples
SSH is short for Secure Shell or Secure Socket shell. According to Wikipedia, the Secure Shell Protocol is a cryptographic network protocol for operating network services securely over an unsecured network. sshd is short for Secure shell daemon. SSH is one of the most reliable ways that you can choose to secure your Linux server-Virtual Private Ser
3 min read
How to Fix "Ssh: Connect To Host 'Hostname' Port 22: Connection Timed Out"
SSH (Secure Shell) is a widely used protocol that provides secure and encrypted communication for accessing and managing remote servers. With robust encryption mechanisms, SSH allows users to connect securely over unsecured networks, run commands, transfer files, and manage services remotely. However, users may sometimes encounter connection issues
5 min read
Shell in a Box - Remote Linux Server via Web Browser
Secure Shell (SSH) is employed to access Linux-based machines, while Remote Desktop Protocol (RDP) is employed for accessing Windows-based machines. Putty is a widely recognized SSH client for Windows. Similarly, to remotely access a Linux server via a web browser, the shellinabox application allows the utilization of SSH. What is shellinabox?Shell
3 min read
How to Control Systemd Services on Remote Linux Server
Linux, SysV, and LSB init scripts are compatible with Systemd, a system and service manager. Aggressive parallelization capabilities are offered by Systemd, which also offers on-demand daemon starting and uses Linux cgroups to keep track of processes. Systemd also supports system snapshotting and restore, maintains mount and automount points, and i
2 min read
How to connect a Local Jupyterlab to a Remote Kernel?
In the era of emerging technologies like Data Analytics, Data Science, Machine Learning, etc., we have seen and come across scenarios, demanding powerful tools that can accelerate and streamline the process of work. One such useful tool that we encountered is JupyterLab. In this article, we will explore the various concepts related to JupyterLab No
7 min read
How to Use Rsync to Make a Remote Linux Backup
Imagine losing all your data by accidentally doing sudo rm -rf /someDirectory, sucks right? But it happens to the best of us. Now, if it was a Windows system, we could have used numerous backup options like OneDrive to secure our data. But what about Linux? Fortunately, we have some very robust tools to backup our Linux system too, and one of them
4 min read
How to Download a File from a Server with SSH / SCP?
Secure Copy Protocol (SCP) is a secure file transfer protocol that allows you to transfer files between computers over a secure connection. It is based on the Secure Shell (SSH) protocol and is commonly used to transfer files between servers and local computers. One of the main benefits of using SCP is that it encrypts the data being transferred, e
3 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a Linux and macOS cat command that improved its precursor: the syntax highlighted file contents can be displayed with its help. Therefore, in the case of source code and configuration files, it is extremely helpful. It can highlight over 10 languages, like JavaScript, Java, Ruby, Python, Go, C, and JSON. Syntaxccat [OPTIONS] [FILE...]Basic
3 min read
Shell Script to Download Files From a Source Directory on a Remote FTP Server
In this article, we are going to write a shell script to download files from a certain directory in a server to a local computer using FTP. It is recommended to have some knowledge of FTP before going through this article, but it is not necessary. Before writing a shell script, we will see how to download a file directly using commands, then we wil
3 min read
Remote Server Management from the Ubuntu Terminal
Controlling servers from a distance is an extremely important job for system admins, users, and programmers. It guarantees that the servers are operating correctly, and safely, and are current with the newest software updates. A highly effective tool for handling remote servers is the Ubuntu terminal. This guide will take you through the process of
5 min read
Disconnecting Inactive SSH Connections in Linux
SSH or Secure Shell is a cryptographic network protocol that establishes a secure connection between systems remotely. Any user can use this protocol to manage the system remotely but mainly system administrators use it because it transmits data over encrypted channels, which increases its security at a high level. SSH can be used to manage the sys
2 min read
How to Disable SSH Root Login in Linux?
SSH or secure shell is a network protocol established between two computers on a network to communicate with each other and share data. This protocol is useful in the case of remote access to the machine. SSH makes the work easy and accessible anywhere. Here we will see how to disable SSH Root Login in Linux. Installation of Openssh-server and Open
1 min read
How to Create SSH Tunneling or Port Forwarding in Linux?
SSH is a secure shell standard client utility for Linux. It is used to establish secure connections to remote (or even local) ssh servers. But some programs are not designed flexible enough to be processed by ssh trivial way: the program can work with local connections only or some related network addresses be hard to code defined. That is why SSH
6 min read
How to configure SSH Client in Linux ?
At times we may need to access multiple remote systems with different IP addresses/hostnames, usernames, non-standard-ports with various command-line options. One way is to create a bash alias for each remote connection. But we have an easy and much better solution to this problem. OpenSSH allows to create per-user configuration file to store diffe
4 min read
How to execute commands remotely using SSH in Linux?
Many times users need to work in remote systems. For which they have to log in to the remote server, execute certain commands and come out of that session. Is it possible to perform all these actions locally? Yes, it's possible using ssh client. In this article, we will see different ways of running remote commands locally using ssh. Pre-requisite
2 min read
How to Prevent a Background Process From Being Stopped after Closing SSH client in Linux
What is Background Process? A Background Process is an ongoing process running independently, and that is not visible to the users as it runs without any user interaction after the terminal has been closed. Some of the well-known examples of Background processes are the web servers like Nginx and Apache. The Nginx discovers the file and sends it to
4 min read
How to change the default SSH port in Linux
SSH (Secure Shell) is a network protocol used to securely connect to the remote server where the data between the server and client is transferred in an encrypted format. In the world of Linux system administration and security, one essential practice is changing the default SSH port. This article will guide you through the process of enhancing you
7 min read
How to Enable and Start SSH on Kali Linux
Secure Shell (SSH) is a protocol that provides a secure way to access a remote computer. It allows you to remotely log in to a system and execute commands as if you were sitting at the console. In this guide, we will explain how to enable and start SSH on Kali Linux, a popular Linux distribution used for penetration testing and digital forensics. W
5 min read
How to generate SSH Key Fingerprint in Linux?
A popular protocol for safe remote access to servers and other systems is called Secure Shell (SSH). The SSH key fingerprint is one of the essential elements of SSH authentication. We will discuss the definition of an SSH key fingerprint, its creation process, its significance for SSH authentication, and security and verification issues in this art
8 min read
Push Codes from Linux (UBUNTU) To GitHub using SSH KEY
GitHub, one of the most popular platforms for version control and collaboration, offers multiple authentication methods for interacting with repositories. SSH keys provide a secure and convenient way to authenticate and push code from a local machine to GitHub. In this article, we'll learn the step-by-step process of pushing code from Linux (Ubuntu
3 min read
How to Find All Failed SSH login Attempts in Linux?
The content has been removed.
1 min read
Rdiff-backup - A Local and Remote Backup Tool for Linux
The Rdiff-backup(Reverse differential backup tool) is a backup tool that backs up one directory to another, either locally or remotely. It is a powerful tool written in Python, that works best with Linux. It works with Windows and Mac OS X as well. Users have reported that it works well on the cross-platforms like FreeBSD. It works on the protocol
4 min read
How to Compare Local and Remote Files in Linux
In this article, we will discuss how to compare or differentiate between local and remote files in Linux. Programmers and writers often want to know the difference between two files or two copies of the same file when writing program files or regular text files. The discrepancy between the contents of two Linux machine files is referred to as a dif
3 min read
How To Setup an Iptables Firewall to Enable Remote Access to Services in Linux
A system administrator can modify the IP packet filter rules of the Linux kernel firewall, which are implemented as various Netfilter modules, using the user-space utility application iptables. The filters are arranged in several tables, each of which has a set of rules on how to handle packets of network data. Using a set of programmable table rul
5 min read
How To Solve Telnet Connection Refused By Remote Host In Linux
When trying to access a remote server over Telnet, encountering a “connection refused” error is common. This issue can disrupt administrative tasks and block access to vital servers, networking equipment, and other devices. Telnet, though considered less secure, is still widely used for managing older systems and devices that require remote connect
5 min read
three90RightbarBannerImg