How to SSH in Linux Command

How to SSH in Linux Command Line

Remote is something very important for a server administrator. Almost anyting in server administration can be done by remoting. This method is very useful, it can save a lot of budget and resource for an IT company to spend, for instance, if all server must have a monitor for each server than i just wondering how much does the IT company will spend the money, and they need operator for each server cause it will be really hard for an employee to deal with many server at a time. All this problem can be solved by using SSH.

Table of Content

1. Introduction
1.1 What is SSH?
2. How Enable SSH in linux command Line?
2.1 SSH Client
2.2 SSH Server
3. Example of Using SSH from Command Line
4. SSH Syntax Format
5. Conclution

Introduction

What is SSH?

SSH is abbreviation of Secured Shell, SSH is a network protocol that used cryptographical standard (Hashing) to communicate between 2 PC, this SSH can be used through command line interface, remote command execution and other service  that required connection between two computer.  This connection is running through a secure connection, the two pc that is going to connected via ssh protocol, must be a client and a server PC, in this case the client will access the server from a command prompt, the server will ask for login information and the connection can be generated.

The most popular application that used Secured Shell protocol is Command Shell on Linux or Unix alike. SSH can be used too by windows user by using a very popular tool name putty, putty application for windows user can be download from this link (download putty). 

How Enable SSH in Linux Command Line?

SSH Client 

Before using SSH from linux terminal, make sure that the ssh-client application is installed in your PC. to check whether it is installed or not, just type this command in linux terminal from the client pc:
ssh
here is the result:
user@client-pc:~$ ssh
usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-E log_file] [-e escape_char]
           [-F configfile] [-I pkcs11] [-i identity_file]
           [-L [bind_address:]port:host:hostport] [-l login_name] [-m mac_spec]
           [-O ctl_cmd] [-o option] [-p port]
           [-Q cipher | cipher-auth | mac | kex | key]
           [-R [bind_address:]port:host:hostport] [-S ctl_path] [-W host:port]
           [-w local_tun[:remote_tun]] [user@]hostname [command]
if the same result shown on your terminal, then the the client SSH package is installed on your system, otherwise you can use this command to install ssh client package on your terminal: 
sudo apt-get install ssh
and here is the result:
user@client-pc:~$ sudo apt-get install ssh
[sudo] password for user:
Reading package lists... Done
Building dependency tree    
Reading state information... Done
The following packages were automatically installed and are no longer required:
  audacious audacious-plugins audacious-plugins-data galculator gtk2-engines
  gtk3-engines-unico libaudcore2 libbinio1ldbl libcddb2 libcue1 libfluidsynth1
  libguess1 libmms0 libmodplug1 libsidplayfp3 lubuntu-artwork
  lubuntu-artwork-15-04 lubuntu-default-settings lubuntu-icon-theme
  lubuntu-lxpanel-icons lxsession-data lxsession-logout
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  ncurses-term openssh-server openssh-sftp-server ssh-import-id
Suggested packages:
  rssh molly-guard monkeysphere
The following NEW packages will be installed:
  ncurses-term openssh-server openssh-sftp-server ssh ssh-import-id
0 upgraded, 5 newly installed, 0 to remove and 71 not upgraded.
Need to get 638 kB of archives.
After this operation, 3.552 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://id.archive.ubuntu.com/ubuntu/ vivid-updates/main openssh-sftp-server amd64 1:6.7p1-5ubuntu1.3 [38,4 kB]
1% [1 openssh-sftp-server 8.077 B/38,4 kB 21%]..........................

Install and Start SSH Server Service

Now, in the server side, we need to make sure that the ssh server package is installed and running. to check whether it is installed or not run this command from your terminal:
sudo service ssh start
here is the result:
user@serverpc:~$ sudo service ssh start
Failed to start ssh.service: Unit ssh.service failed to load: No such file or directory.
if you got the same result in your terminal, it indicated that your ssh server package is not installed on your server and the service is not running, then you need to install the ssh server package, to install the ssh server package run this command on your linux terminal:
sudo apt-get install openssh-server
here is the result:
user@serverpc:~$ sudo apt-get install openssh-server
Reading package lists... Done
Building dependency tree    
Reading state information... Done
The following packages were automatically installed and are no longer required:
  audacious audacious-plugins audacious-plugins-data galculator gtk2-engines
  gtk3-engines-unico libaudcore2 libbinio1ldbl libcddb2 libcue1 libfluidsynth1
  libguess1 libmms0 libmodplug1 libsidplayfp3 lubuntu-artwork
  lubuntu-artwork-15-04 lubuntu-default-settings lubuntu-icon-theme
  lubuntu-lxpanel-icons lxsession-data lxsession-logout
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  ncurses-term openssh-sftp-server ssh-import-id
Suggested packages:
  rssh molly-guard monkeysphere
The following NEW packages will be installed:
  ncurses-term openssh-server openssh-sftp-server ssh-import-id
0 upgraded, 4 newly installed, 0 to remove and 71 not upgraded.
Need to get 630 kB of archives.
After this operation, 3.441 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://id.archive.ubuntu.com/ubuntu/ vivid/main ncurses-term all 5.9+20140712-2ubuntu2 [248 kB]
Get:2 http://id.archive.ubuntu.com/ubuntu/ vivid-updates/main openssh-sftp-server amd64 1:6.7p1-5ubuntu1.3 [38,4 kB]
Get:3 http://id.archive.ubuntu.com/ubuntu/ vivid-updates/main openssh-server amd64 1:6.7p1-5ubuntu1.3 [334 kB]
Get:4 http://id.archive.ubuntu.com/ubuntu/ vivid/main ssh-import-id all 4.1-0ubuntu1 [9.344 B]
Fetched 617 kB in 7s (82,3 kB/s)                                            
Preconfiguring packages ...
Selecting previously unselected package ncurses-term.
(Reading database ... 211894 files and directories currently installed.)
Preparing to unpack .../ncurses-term_5.9+20140712-2ubuntu2_all.deb ...
Unpacking ncurses-term (5.9+20140712-2ubuntu2) ...
Selecting previously unselected package openssh-sftp-server.
Preparing to unpack .../openssh-sftp-server_1%3a6.7p1-5ubuntu1.3_amd64.deb ...
Unpacking openssh-sftp-server (1:6.7p1-5ubuntu1.3) ...
Selecting previously unselected package openssh-server.
Preparing to unpack .../openssh-server_1%3a6.7p1-5ubuntu1.3_amd64.deb ...
Unpacking openssh-server (1:6.7p1-5ubuntu1.3) ...
Selecting previously unselected package ssh-import-id.
Preparing to unpack .../ssh-import-id_4.1-0ubuntu1_all.deb ...
Unpacking ssh-import-id (4.1-0ubuntu1) ...
Processing triggers for man-db (2.7.0.2-5) ...
Processing triggers for systemd (219-7ubuntu6) ...
Processing triggers for ureadahead (0.100.0-19) ...
ureadahead will be reprofiled on next reboot
Processing triggers for ufw (0.34~rc-0ubuntu5) ...
Setting up ncurses-term (5.9+20140712-2ubuntu2) ...
Setting up openssh-sftp-server (1:6.7p1-5ubuntu1.3) ...
Setting up openssh-server (1:6.7p1-5ubuntu1.3) ...
Creating SSH2 RSA key; this may take some time ...
2048 aa:b9:19:04:8b:52:34:db:cb:ee:5a:c0:e9:95:07:7e /etc/ssh/ssh_host_rsa_key.pub (RSA)
Creating SSH2 DSA key; this may take some time ...
1024 79:21:b5:f6:75:05:37:89:27:eb:13:7a:8a:8d:ae:c3 /etc/ssh/ssh_host_dsa_key.pub (DSA)
Creating SSH2 ECDSA key; this may take some time ...
256 35:f8:97:87:28:01:da:91:ed:06:30:b3:78:59:d1:86 /etc/ssh/ssh_host_ecdsa_key.pub (ECDSA)
Creating SSH2 ED25519 key; this may take some time ...
256 19:98:00:24:43:25:d2:34:9c:6f:43:1c:90:a3:ec:12 /etc/ssh/ssh_host_ed25519_key.pub (ED25519)
Setting up ssh-import-id (4.1-0ubuntu1) ...
Processing triggers for systemd (219-7ubuntu6) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for ufw (0.34~rc-0ubuntu5) ...
Now we need to start the ssh server service, run this command on your terminal:
sudo service ssh start
if there's no error information appear, than the server is successfully start, to check the service whether it is running or not, run this command:
sudo service ssh status
here is the result:
user@severpc:~$ sudo service ssh status
● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: active (running) since Sab 2015-12-26 11:15:20 FNT; 3min 2s ago
 Main PID: 4642 (sshd)
   CGroup: /system.slice/ssh.service
           └─4642 /usr/sbin/sshd -D
Des 26 11:15:20 asus systemd[1]: Started OpenBSD Secure Shell server.
Des 26 11:15:20 asus systemd[1]: Starting OpenBSD Secure Shell server...
Des 26 11:15:20 asus sshd[4642]: Server listening on 0.0.0.0 port 22.
Des 26 11:15:20 asus sshd[4642]: Server listening on :: port 22.
Des 26 11:18:08 asus systemd[1]: Started OpenBSD Secure Shell server.
Des 26 11:18:11 asus systemd[1]: Started OpenBSD Secure Shell server.
if the same result appear on your terminal, than the service is running well...

Example of Using SSH from Command Line

Here comes the time to remote server PC from the Client PC. Before we connect the client to server via SSH, we need to know what is the ip address of the server, to check the ip address of the server, run this command on your server terminal:
ifconfig
here is the result:
user@server:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 08:9e:01:3f:4f:52
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
eth1      Link encap:Ethernet  HWaddr 6a:a8:6d:c8:fd:e5
          inet addr:192.168.1.2  Bcast:192.168.1.15  Mask:255.255.255.0
          inet6 addr: fe80::68a8:6dff:fec8:fde5/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:29486 errors:0 dropped:0 overruns:0 frame:0
          TX packets:25325 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:16714547 (16.7 MB)  TX bytes:4711083 (4.7 MB)
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:3940 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3940 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:356754 (356.7 KB)  TX bytes:356754 (356.7 KB)
wlan0     Link encap:Ethernet  HWaddr 68:94:23:01:52:d8
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
from the result, we know the ip address of the server is 192.168.1.2

SSH Syntax Format

you need to know the syntax format of SSH before we ssh the server pc, here is the format:

ssh user@ip_server -p 22

here is the explanation of each parameter
  • user -> user is the username of server pc
  • ip_server -> ip_server is the ip address of the server PC, from the result above, the server ip address is 192.168.1.2
  • -p -> "minus p" is a parameter to specify the port of ssh, by default the default port for ssh service is 22
Now lets SSH the linux server PC from the linux Client PC, run this command from the client pc from your linux terminal:
ssh user@192.168.1.2 -p 22
here is the result:
user@clientpc:~$ ssh user@192.168.1.2 -p 22
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is 35:f8:97:87:28:01:da:91:ed:06:30:b3:78:59:d1:86.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
user@192.168.1.2's password:
Welcome to Ubuntu 15.04 (GNU/Linux 3.19.0-15-generic x86_64)
 * Documentation:  https://help.ubuntu.com/
Last login: Sun Dec 20 06:44:30 2015

Conclution

Based on the result, we have successfully connected to server using ssh. now you can fully access the server shell from your terminal.

Komentar

Postingan populer dari blog ini

Which television show centered around the lives of the Carrington family?

Donald Trump will be the first to serve two non-consecutive terms since which commander in chief?