Wednesday, February 3, 2021

Linux 101 - SSH and SCP

SSH

The SSH Protocol, also called Secure Shell, is a method for secure remote login from one computer to another. Unlike telnet, it is secure. (Refer this article)

 

How to use SSH to connect to your server

    1. Install openssh-server


            $ sudo apt install openssh-server

    2. Check if the SSH service is up or not

            
$ systemctl status sshd.service
            ssh.service - OpenBSD Secure Shell server
                Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: >
                Active: active (running)

        or 


            $ ps -ef | grep ssh
            root 670 1 0 00:11 ? 00:00:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startup

        3. Create host-only adapter in VM

        (VirtualBox) Network -> Enable Network Adapter -> Host-only Adapter

 
 $ ifconfig
                enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
                    inet 192.168.56.103  netmask 255.255.255.0  broadcast 192.168.56.255
                    inet6 fe80::2e13:8bcb:9fc2:32ec  prefixlen 64  scopeid 0x20<link>
                    ether 08:00:27:eb:44:5b  txqueuelen 1000  (Ethernet)
                    RX packets 30  bytes 2989 (2.9 KB)
                    RX errors 0  dropped 0  overruns 0  frame 0
                    TX packets 66  bytes 7752 (7.7 KB)
                    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    4. Testing connection from cmd 


            $ ping 192.168.56.103
            Pinging 192.168.56.103 with 32 bytes of data:
            Reply from 192.168.56.103: bytes=32 time=1ms TTL=64

    5. Establish the SSH connection 


            $ ssh fcheng@192.168.56.103



Using public and private key to establish SSH connection 

    1. Client generate private and public key (rsa key pair)


 $ cd ~/.ssh
            $ ssh-keygen -t rsa -b 4096
            $ cat id_rsa.pub

    2. Forward and save public key to server (Ubuntu)


            // Client
            $ ssh-copy-id fcheng@192.168.56.103

            // Checking in Server
            $ cat ~/.ssh/authorized_keys

    3. Client initiates SSH connection to server

    4. Server send an random message to client

    5. Client use its private key to encrypt the message and send it back to server

    6. Server decrypted message with public key

    7. If the raw message equals to decrypted message, then client is authenticated

 

How to use Putty

    1. Using PuTTYgen to generate public/private key pair

    2. Save private key in disk

    3. Copy public key and paste it to server


 $ vim ~/.ssh/authorized_keys

        // remove groups permission
        $ chmod 600 ~/.ssh/authorized_keys

    4. In Putty, from Connection -> SSH -> Auth, browse the private key you stored before 

    5. Then, you don't need to enter password anymore

 

SCP

    scp source_file_name username@destination_host:destination_folder

        -v: Provide the detail information of scp process

        -p: Provide time and speed information of scp process

        -C: Make file transfer faster


// cmd
    $ scp hello.txt fcheng@192.168.56.103:Downloads

No comments:

Post a Comment