- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Linux Commands Using Secure Shell (ssh)
Introduction
There are many ways we can access or communicate between two systems [Linux to Linux, Windows to Linux] through CLI. Secure Shell (ssh) is very useful tool to access any other Linux system. If two Linux systems are connected through any network then we can use ssh to access other system. There are some basic prerequisites to avail this facility
ssh client software Example: default ssh binary in Linux, putty, mobaxterm etc.
ssh server running in remote Linux system.
IP or host name of remote system.
Let us discuss on Linux system to Linux system communication using ssh.
Configure ssh Set Up
From the prerequisites ,
We need to install putty or mobaxterm if client system is windows. For Linux client system there is nothing to be installed, as by default ssh command is available.
We can run below command to check if ssh is already installed or not.
$ ssh
usage: ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port] [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-L address] [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address] [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]] [user@]hostname [command]
Then in Linux server side, we need to install “openssh-server” package. We can use below command to install.
$ sudo apt-get install openssh-server
Let us assume,
System A is client and system B is server.
System B ip is “192.168.1.102” and user name is “rian”. We will access system B from system A.
Command to Login to Remote Linux system
Here is the minimum command to access system B.
$ ssh <user name of system B>@<IP address or hostname of system B: 192.168.1.102>
Actual command is
$ ssh rian@192.168.1.102
A password will be prompted for user “rian”. After we give correct password , ssh is successful to ip 192.168.1.102 or system B.
Output
The authenticity of host '192.168.1.102 (192.168.1.102)' can't be established. ECDSA key fingerprint is SHA256:YdQpl9u/N2+Dc6hPeDr8e5IdDXUJ3POj30prm1XJA2s. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.1.102' (ECDSA) to the list of known hosts. rian@192.168.1.102's password: Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.15.0-45-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage 447 packages can be updated. 385 updates are security updates. New release '18.04.6 LTS' available. Run 'do-release-upgrade' to upgrade to it. Last login: Thu Feb 11 22:20:03 2016 from 192.168.1.4
Execute Command in Remote System Using ssh
If we want to run any remote command without logged into remote system , then we can use below syntax.
$ ssh <user name of system B>@<IP address of system B: 192.168.1.102> <command full path>
Here is the actual command [example: ls]
$ ssh rian@192.168.1.102 /bin/ls
Output
rian@192.168.1.102's password: Desktop Documents Downloads examples.desktop Music Pictures Public Templates tutorialpoint Videos
Access GUI Based Application in Remote System
In case we need to open any GUI based application like firefox from remote system then we have to ssh with –X option.
$ ssh –X rian@192.168.1.102
Now, we can open “firefox” browser from remote system in ssh terminal. If we do ssh without –X then we cannot open gui-based application.
$ firefox
Copy Over ssh Connection
If we want copy any file from system A to system B over ssh using “scp” command, we can use below syntax.
$ scp system_a_file <user name of system B>@<system B ip address:system b path>
Here is the actual command.
$ scp examples.desktop rian@192.168.1.102:/tmp/
Output
rian@192.168.1.102's password: examples.desktop 100% 8980 4.4KB/s 00:02
Port Forwarding Using ssh
To access a port below command can be used. This is very useful features
$ ssh -L 8888:localhost:8080 <user name of system B>@<ip or hostname of user B>
ssh with Custom Port
By default ssh listens to port number 22. But we can use custom ssh port using below command.
$ ssh <user@remote ip> -p 3423
Restart ssh Service
Here is the command to restart sshd service if something changed in ssh config file or any other reason.
$ sudo sshd service restart
ssh Common error
Sometime we get below error while doing ssh to remote system.
ssh: connect to host 192.168.1.107 port 22: No route to host
The possible reasons are remote ip is not reachable. We should do ping to remote ip and then try ssh.
Conclusion
From this article, we have come know many usages of ssh command. Using ssh we can access another Linux system in a faster way and also perform some actions like copy, execute command etc.