SSH is one of the main key services in Linux/Unix based systems. SSH represents Secure Socket Shell. SSH protocol is used to access the remote server/system with an encrypted method of login using the default TCP/IP port 22 or a custom based port number. This service is mainly used by System and Network Administrators to execute commands and manage servers/systems. We can also say SSH is a replacement for Telnet, rlogin, and rsh which is a completely insecure protocol to connect the system across the network.
This guide will help you on how to login to the remote server (let's say a Web Server) from your local system/host (client PC). All the below examples of ssh commands are tested on RHEL/CentOS 7.6. You can use the same guide for all the RHEL / CentOS / Fedora versions, but for other distros, there might be a small type of difference in using it. Therefore, it is highly recommended to read the guidelines.
This guide assumes that you already have some servers at a remote location which is running on Linux (CentOS / RHEL), it could be either a Web server or Mail Server or DNS Server. Our goal is to access the remote server using the 'ssh' command.
Global Syntax of ssh command:
ssh [OPTIONS] [USER@] :HOST
MY Lab:
Webserver:
Operating System : CentOS Linux 7 (core)
Hostname : web.linuxteck.com
IP Address : 192.168.1.100
Localhost/system (client pc)
Operating System : CentOS Linux 7 (core)
Hostname : r001.linuxteck.com
IP Address : 192.168.1.200
SSH client : An active ssh client like " Terminal for Linux/Mac and Putty for Windows"
Note:
1. How do you find the version of your ssh client?
# ssh -V
Output:
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
Note:
2. How to access the remote server using the default ssh command?
Note:
# ssh 192.168.1.100 OR # ssh web.linuxteck.com
Note:
WARNING:
3. How to verify the fingerprint using the ssh command in the terminal?
# ssh-keygen -l -f /etc/ssh/ssh_host_ecdsa_key.pub
Output:+
256 SHA256:4YNXabAuI4gYnC7ZIcTRbPlCMapOvrMwW4E/kKACb4s no comment (ECDSA)
Note:
# ssh-keygen -l -f /etc/ssh/ssh_host_ecdsa_key.pub -E md5
Output:
256 MD5:5f:39:21:43:92:6e:7e:f3:17:35:6b:30:85:44:ee:38 no comment (ECDSA)
4. How to specify a particular user to access the remote server?
# ssh -l linuxteck 192.168.1.100 OR ssh linuxteck@192.168.1.100
Output:
linuxteck@192.168.1.100's password: (Enter password)
Last login: Mon Apr 6 02:44:44 2020 from 192.168.1.100
Note:
5. How to access a remote server with a customized port number?
# ssh -l linuxteck 192.168.1.100 -p 18765 OR # ssh linuxteck@192.168.1.100 -p 18765
Output:
linuxteck@192.168.1.100's password: (Enter password)
Last login: Mon Apr 6 02:54:44 2020 from 192.168.1.100
Note:
6. How to execute a command without logging into the remote server?
# ssh -l linuxteck 192.168.1.100 uname -r OR # ssh linuxteck@192.168.1.100 uname -r
Output:
linuxteck@192.168.1.100's password:
3.10.0-862.14.4.el7.x86_64
Note:
7. How do you execute multiple commands without logging into your remote server?
# ssh -l linuxteck 192.168.1.100 "uname -r; hostname" OR # ssh linuxteck@192.168.1.100 "uname -r; hostname"
Output:
linuxteck@192.168.1.100's password:
3.10.0-862.14.4.el7.x86_64
web.linuxteck.com
Note:
8. How to transfer a file from your localhost to a remote server?
# scp dmo3c_test.sql linuxteck@192.168.1.100:/home/apps/production/sql/dump
Output:
linuxteck@192.168.1.100's password:
dmo3c_test.sql 100% 70KB 3.3MB/s 00:00
Note:
9. How to access the remote server using the sftp command?
# sftp linuxteck@192.168.1.100
Output:
linuxteck@192.168.1.100's password:
Connected to 192.168.1.100.
sftp> ls (# ls command is to list the files)
php
share
tandd
sftp> pwd (#pwd command is to check the path of the current working directory)
Remote working directory: /home/apps
sftp>
Note:
10. Some of the most used general shell commands in Linux :
$ cd
change directory:- It is used to change the working directory. It is also referred to as "chdir".
$ cp
copy:- It is used to copy files and directories from one place to another.
$ chmod
change permission:- It is used to change the read, write, or execution permission of a file or directory.
$ chown
change owner:- It is used to change the ownership of a file or directory
$ grep
search pattern of a file:- It is used to search for a given file pattern by specific users
$ wc
word count:- It is mainly used for counting, either words, characters, or lines.
$ mkdir
create directory: It is used to create a directory
$ rm
delete file:- It is used to delete files and directories
$ touch
Timestamps: It is used for timestamps of a file like "create, update, modify".It can also be used to create an empty file.
$ more
It is used to view the contents of a text file one full screen at a time, Using the spacebar key to help move to the next page.
$ yum
Package management tool:- It is used to install / update / erase rpm packages
$ wget
It is used to download files from the internet.
$ vi
It is used to edit all kinds of Unix files. The Unix 'vi' editor is a standard text editor.
$ vim
Vi Improved text editor. It is also used to edit any kind of Linux files.
$ ls
It is used to list all the files and directories. To learn more about the 'ls' command with various options, click here
$ cat
It is used to concatenate and display files. To learn more about the 'cat' command with various options, click here
$ du
Disk usage:- This is used to estimate filespace usage. To learn more about the 'df' command with various options, click here
$ df
Disk free: It is used to estimate the server/storage disk space. To learn more about the 'du' command with various options, click here
$ find
It is used to search all the files and directories. To learn more about finding commands with various options, click here
$ cron
It is used to schedule repetitive jobs/tasks at regular intervals. To learn more about cron commands with various options, click here
$ curl
It is used to transfer files with URLs between remotes to the local system. To learn more about curl commands with various options, click here
$ ps
It is used to monitor all the currently running activities like USER, PID, %CPU, etc, etc. To learn more about PS commands with various options, click here
Note:
Thank you for taking the time to read! I hope this article will help you to understand 'ssh' client command with examples. Drop me your feedback/comments. If you like this article, kindly share it and it may help others as well.
A few ssh references are collected from this site
Thank you!
4 replies on “10 basic and most useful 'ssh' client commands in Linux”
Sir
This is very useful for beginners.
Yours faithfully
S Govardhan
Thanks, Govardhan!
Thanks for sharing this howto articles. Very helpful.
I used Linux during my graduation days. After seeing your article, I feel like using it again. Thanks for sharing 🙂
Regards,
Jainish