11 Useful “ssh” and “scp” Commands in Linux

Introduction

In this post, we are going to learn some useful ssh command and scp command with examples. SSH is a protocol that stands for Secure Shell which is a client-side command tool used to take remote session of Linux system securely. It uses Symmetrical and Asymmetrical encryption method to transfer data over the network. SSH is a highly recommended tool for taking a remote session of a server as it is the most secure way of connecting any server. SSH is the replacement of 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 as SSH-1 and SSH-2. The difference between SSH-1 and SSH-2 is that SSH-2 supports Public Key Certificate while SSH-1 does not.

scp is a command line utility that is used to copy data from one Linux system to another 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.

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 ssh command

As we all know that ssh is used to take remote console of other Linux system securely, So let’s take remote console of pc2 from pc1 you can use 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 remote console of the system with currently logged in username. For example, here in pc1 I logged in using username “root” and I want to take remote session of the pc2 machine using 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 already taken remote console use command logout. Refer 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 ssh command with option -V. Refer 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 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 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 remote system to 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 scp command with option -r. Refer the Sample Output below. Here I am copying a directory i.e. data from remote system i.e pc2 to 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]#

8. Copy data from Local System to Remote System Recursively using scp command

To copy a directory with all its content recursively from local system to remote system you can use scp command with option -r. Refer 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    

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

You can take the remote session of a server and run a command simultaneously. For Example, here I am taking remote of system pc2 and executing a command i.e. ifconfig. Refer 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)

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

To take remote of the system with 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

11. Copy Hidden Files & Directories using scp command.

To copy hidden file and directories you can use ssh command with option -rp. one thing keep in mind that during copy hidden files and directories using scp command always put . (dot) 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
Related Post