Most Useful SSH Command And SCP Command With Examples

In this article, we are going to learn some useful SSH command and SCP command with examples. SSH is a protocol stands for Secure Shell which is a nice client-side command tool is used to take remote console of Linux system securely. it uses a Symmetrical and Asymmetrical encryption method to transfer data over the network. So SSH is the highly recommended tool for remote console purpose. SSH is the replacement of the previously used unsecured remote tool like telnet. Port number of SSH is 22. The Versions of SSH are SSH Protocol Version 1 & SSH Protocol Version 2 also referred to as SSH-1 and SSH-2. The difference between SSH-1 and SSH-2 is SSH-2 supports Public Key Certificate while SSH-1 not.

SCP command tool is a very useful tool to copy data from one Linux system to another Linux system over the network securely. Like SSH SCP also uses port number 22 to connect and transfer data over the network. SCP is a nice alternative and recommended tool against unsecured data transfer protocols like FTP, Telnet and It is highly secured to transfer data over the network as it uses SSH protocol to transfer data securely.

Most Useful SSH Command And SCP Command With Examples
Most Useful SSH Command And SCP Command With Examples

So let’s have a look at some important ssh command and SCP command with examples.

I have two Linux Systems in my testing environment i.e. pc1 and pc2, Find the system details below.

Testing Environment Scenario:

PC1 :

Computer Name – pc1
IP Address – 192.168.0.105

PC2 :

Computer Name – pc2
IP Address – 192.168.0.106

1. Take remote of PC2 from PC1 using the ssh command

As we all know that ssh is used to take the remote console of another Linux system securely, So let’s take the remote console of pc2from pc1 you can use the below ssh command.

Syntax: ssh username@IP Address of the Remote Computer

[root@pc1 ~]# ssh root@192.168.0.106   # Take Remote of PC2 from PC1
root@192.168.0.106's password: 
Last login: Thu Mar  9 09:14:34 2017 from 192.168.0.107

# Remote taken Successfully, to confirm check the hostname and IP Address of the remote system as shown below.

[root@pc2 ~]# hostname 
pc2
[root@pc2 ~]# ifconfig    
eth0      Link encap:Ethernet  HWaddr 00:0C:29:01:77:E7  
          inet addr:192.168.0.106  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe01:77e7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:9688 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4695 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:11157837 (10.6 MiB)  TX bytes:370245 (361.5 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:16 errors:0 dropped:0 overruns:0 frame:0
          TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:960 (960.0 b)  TX bytes:960 (960.0 b)

2. Take Remote Console of the System with Currently Logged in Username

You can take the remote console of the system with the currently logged-in username. For example, Here in pc1 I logged in using the username “root” and I want to take remote of the pc2 using the same username i.e. “root” then we can use the below command.

[root@pc1 ~]# ssh 192.168.0.106   # Take Remote using currently logged in username
root@192.168.0.106's password: 
Last login: Thu Mar  9 09:08:54 2017 from 192.168.0.107
[root@pc2 ~]# hostname
pc2

3. Logout from Remote Console

To logout from an already taken remote console use the command logout. Refer to the sample output below.

[root@pc2 ~]# logout   # Logout from already taken remote console
Connection to 192.168.0.106 closed.

4. Check the Version of Installed ssh Package

To check the currently installed ssh package version use the ssh command with option -V. Refer to the sample output below.

[root@pc1 ~]# ssh -V   # check the currently installed ssh package version
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013

5. Copy data from one Linux system to another over Network using the SCP command

To copy data from one Linux system to another we can use SCP command, Here I am copying data i.e. touch.txt file from pc1 to pc2. Refer to the sample output below.

[root@pc1 ~]# touch text.txt   # Create a Sample text File
[root@pc1 ~]# ls
anaconda-ks.cfg  Documents    install.log.syslog  Public     Videos
data             Downloads    Music               Templates
Desktop          install.log  Pictures            text.txt
[root@pc1 ~]# scp text.txt root@192.168.0.106:/root/Desktop/   # Copy data from Local System to Remote System
root@192.168.0.106's password: 
text.txt                                      100%    0     0.0KB/s   00:00   

6. Download/Copy Data from Remote System using SCP Command

To copy data from a remote system to the local system you can use the below command, Here I am copying data from pc2 to pc1.

[root@pc1 ~]# scp root@192.168.0.106:/data/database.txt /root/   # Copy Data from Remote System to Local using scp Command
root@192.168.0.106's password: 
database.txt                                  100%    0     0.0KB/s   00:00    
[root@pc1 ~]# ls
anaconda-ks.cfg  Desktop    install.log         Pictures   text.txt
data             Documents  install.log.syslog  Public     Videos
database.txt     Downloads  Music               Templates

7. Copy data from Remote System to Local System Recursively

To copy a directory with all its content recursively you can use the SCP command with option -r. Refer to the Sample Output below. Here I am copying a directory i.e. data from a remote system i.e pc2 to the local system i.e. pc1.

[root@pc1 ~]# scp -r root@192.168.0.106:/data /root/Desktop/   # Copy directory Recursively
root@192.168.0.106's password: 
database.txt                                  100%    0     0.0KB/s   00:00    
[root@pc1 ~]# cd /root/Desktop/
[root@pc1 Desktop]# ls
data
[root@pc1 Desktop]# ls data/
database.txt
[root@pc1 Desktop]#

7. Copy data from Local System to Remote System Recursively using the SCP command

To copy a directory with all its content recursively from the local system to the remote system you can use the SCP command with option -r. Refer to the sample output below.

[root@pc1 ~]# ls apps/   
test1.txt  test2.txt  test3.txt  test4.txt  test5.txt
[root@pc1 ~]# scp -r apps/ root@192.168.0.106:/root   # Copy directory Recursively from Local System to Remote System
root@192.168.0.106's password: 
test2.txt                                     100%    0     0.0KB/s   00:00    
test1.txt                                     100%    0     0.0KB/s   00:00    
test4.txt                                     100%    0     0.0KB/s   00:00    
test5.txt                                     100%    0     0.0KB/s   00:00    
test3.txt                                     100%    0     0.0KB/s   00:00    

8. Connect and Executive a Command Simultaneously on Remote System using SSH Command

You can take the remote and run a command simultaneously. for example, here I am taking remote of system pc2 and executing a command i.e. ifconfig. Refer to the sample output below.

[root@pc1 ~]# ssh 192.168.0.106 "ifconfig"   #  take remote and run a command simultaneously
root@192.168.0.106's password: 
eth0      Link encap:Ethernet  HWaddr 00:0C:29:01:77:E7  
          inet addr:192.168.0.106  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe01:77e7/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:9870 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4845 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:11182999 (10.6 MiB)  TX bytes:393576 (384.3 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:18 errors:0 dropped:0 overruns:0 frame:0
          TX packets:18 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1058 (1.0 KiB)  TX bytes:1058 (1.0 KiB)

9. Take Remote of the System by Different Username using the ssh command

To take remote of the system with a different username you can use ssh command with option -l. Refer the Output below.

[root@pc1 Desktop]# ssh -l root 192.168.0.106   # Take remote using different username
root@192.168.0.106's password: 
Last login: Fri Mar 10 08:54:59 2017 from 192.168.0.107

10. Copy Hidden Files & Directories using the SCP command.

To copy hidden files and directories you can use ssh command with option -rp. one thing keeps in mind that during copy hidden files and directories using the SCP command always put.  at the end of the path for example “scp -rp /root/.“.

[root@pc1 ~]# scp -rp /root/. root@192.168.0.106:/hidden   # Copy hidden files and directories using scp command
root@192.168.0.106's password: 
.gtk-bookmarks                                100%  107     0.1KB/s   00:00    
.bashrc                                       100%  176     0.2KB/s   00:00    
.bash_logout                                  100%   18     0.0KB/s   00:00    
.bash_profile                                 100%  176     0.2KB/s   00:00    
database.txt                                  100%    0     0.0KB/s   00:00    
.bash_history                                 100% 3327     3.3KB/s   00:00    
database.txt                                  100%    0     0.0KB/s   00:00    
profiles.ini                                  100%  104     0.1KB/s   00:00    
.
.
.
home-2f9509b7.log                             100%   32KB  32.0KB/s   00:00    
addressbook.db                                100%   12KB  12.0KB/s   00:00    
addressbook.db.summary                        100%   86     0.1KB/s   00:00    
.ICEauthority                                 100% 1244     1.2KB/s   00:00    
[root@pc1 ~]# ssh 192.168.0.106
root@192.168.0.106's password: 
Last login: Fri Mar 10 08:56:14 2017 from 192.168.0.107
[root@pc2 ~]# ls -a /hidden/
.                data             .gnote              Pictures
..               database.txt     .gnupg              Public
anaconda-ks.cfg  .dbus            .gtk-bookmarks      .pulse
apps             Desktop          .gvfs               .pulse-cookie
.bash_history    Documents        .ICEauthority       .ssh
.bash_logout     Downloads        install.log         .tcshrc
.bash_profile    .esd_auth        install.log.syslog  Templates
.bashrc          .gconf           .local              text.txt
.cache           .gconfd          .mozilla            Videos
.config          .gnome2          Music
.cshrc           .gnome2_private  .nautilus

Also Read – How to install PuTTY SSH and Telnet Client in Ubuntu

We tried to include all possible ssh command and SCP command if some command missed out please let us know in the comment box below so that we can include that in this article.

That’s all, In this article, we have explained the Most Useful SSH Command And SCP Command With Examples. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.

Share this:
WhatsApp Channel Join Now
Telegram Channel Join Now
Instagram Channel Join Now

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.