How to Install and Configure SSH Server on a Ubuntu Desktop

ssh-protocol

We usually use SSH to access Cloud Servers or IoT gateways – like Raspberry Pi, but sometimes a situation arises where we need to SSH into our desktop for some or other reasons. Or maybe you want to install SSH Server to a Ubuntu OS for a completely different purpose. That’s fine because here we will discuss how to install and configure SSH Server for your Ubuntu Machine.

You can use this method for any version of Ubuntu, Xubuntu, Lubuntu or any other OS based on Debian. Know more about terms like – LINUX, UNIX, Debian, Ubuntu, Kernel, GNOME, GNU, APT, RPM, YUM, GNOME

ssh-protocol

3 Steps to Install and Configure SSH on Ubuntu via Terminal

  1. Install SSH-Server : sudo apt install openssh-server
  2. After installation the SSH service will start automatically, you can verify it by running sudo systemctl status ssh it will show “active running”. Press “q” to quit
  3. Now you need to enable ssh from firewall by using this command : sudo ufw allow ssh

That’s all now you can connect to your ubuntu machine by using ssh username@host-ip

Enable SSH in Raspbian/Ubuntu without Keyboard & Monitor

ssh-into-raspberry-pi

Many times, a situation arises for an IOT developer where he/she wants to ssh onto a newly flashed raspberry pi running on Ubuntu/Raspbain but without a Monitor and keyboard or any other input device.

ssh-into-raspberry-pi

The first and foremost thing to SSH into a pi is to open port 22. But its always closed for security purposes. When we have a pi with a keyboard and monitor its easy to open the port but that isn’t the case always. Here we will explain how to open port 22 and enable ssh without any input device.

Open Port 22 in Raspbian/Ubuntu for SSH

Without wasting time we will straight tell you to steps to open port 22

  1. Prepare your sd card using Etcher and your OS image (or Mount flashed SD card)
  2. Navigate to the SD card [boot] using your OS file manager or terminal
  3. Create a new empty file named ssh, without any extension, inside the boot directory [touch /boot/ssh]
  4. Remove the SD card from your computer and put it in your Raspberry Pi.
  5. Power on your Pi board. On boot Pi will check whether this file exists and if it does, SSH will be enabled and the file is removed.

That’s it, now connect your raspberry pi to your network via LAN, find the IP address from your Router’s console and ssh onto it. If you want to SSH to your Pi from a different network – Click Here.

Configure Wifi in Debain Linux(Ubunut Server) with WPA Supplicant

wifi-wpa-supplicant

Configuring wifi is a pretty easy task when we have a Desktop Environment installed on our Linux system. But it becomes a troublesome process when we have to configure Wifi in headless mode. Here are some tools and commands to configure your wifi efficiently in any Debian Linux OS.

wifi-wpa-supplicant

We need to have a LAN connectivity via ethernet on the host computer where we need to configure Wifi.

  1. STEP 1: find the name of your Wireless Interface
    • iwconfig
    • sudo ifconfig wlan0 up   (bring wifi up)
    • sudo iwlist wlan0 scan | grep ESSID
  2. STEP 2: Connect to Wi-Fi Network With WPA_Supplicant
    • sudo apt install wpasupplicant
    • wpa_passphrase your-ESSID your-wifi-passphrase | sudo tee /etc/wpa_supplicant.conf
    • sudo wpa_supplicant -B -c /etc/wpa_supplicant.conf -i wlan0
    • sudo dhclient wlan0   (obtain IP address)
    • sudo dhclient wlan0 -r   (release IP address)

For more info like starting this service at boot time and obtain a status IP address head on to this Awesome Article

Make SSH connection on Public Internet (WAN) without Port Forwarding

remote-ssh-raspberry-pi

Making an SSH connection is something elementary while developing an IOT project, just open the port 22 install an SSH server and you are ready to go. But sometimes you need to access a device which is behind a router on Public internet. One scenario is you want to access a device that is present in a different city or a country.

remote-ssh-raspberry-pi

For this situation, we will use a VPN managed service from remote.it (this service creates a VPN between itself and your device, and let you access that device from its dynamic address)

STEP 1: Create a free developer account and install the remote.it service on your device by clicking add device option.

For raspberry pi just execute the below line

sudo apt update && sudo apt install -y connectd && sudo connectd_installer

STEP 2: Sign-in to the service using your credentials for remote.it on your terminal.

STEP 3: Select the Application (Port you want to open). Also, you can map any device in your LAN(ex. router homepage) to any application.

STEP 4: On the Web Console, click on DEVICE -> Device Name -> Service. Then from the window copy, the remote address and you are good to go.

Additional Official Documentation: HERE

What is NTP/SNTP Protocols, How NTP works

What is NTP/SNTP Protocols, How NTP works

The Network Time Protocol is a Networking Protocol which is used to synchronize time for nodes in a Network. NTP is one of the oldest protocol in the TCP/IP stack which is still in use. NTP is intended to synchronize all participating computers to within a few milliseconds of Coordinated Universal Time (UTC).
The protocol is usually described in terms of a client-server model, but can as easily be used in peer-to-peer relationships where both peers consider the other to be a potential time source.

How NTP works

NTP works on IP port – 123 and to run NTP we first install NTP daemon (ntpd) on both Time Provider(server) and Time Consumer(client)

The NTP client initiates a time-request exchange with the NTP server. As a result of this exchange, the client is able to calculate the link delay and its local offset, and adjust its local clock to match the clock at the server’s computer. As a rule, six exchanges over a period of about five to 10 minutes are required to initially set the clock.

Once synchronized, the client updates the clock about once every 10 minutes, which prevents the time drift from happening. In Other words ,large adjustment are made quickly and small adjustment are made over a period of time.


NTP Stratum

ntp-stratum

NTP uses the concept of stratum to define the hierarchy of NTP servers. At top of the hierarchy are Stratum 1 which gets their time from Atomic clocks which tells exact time at the moment.

Similarly Stratum 2 time providers get their time from Stratum 1 time provider and Stratum 3 from Stratum 2. And these stratum can go upto 256 layers.

There are many Stratum 2 servers available on the public internet which we can use for couple of PC. But if you have large number of PC’s in the network it is wise to create a local stratum (maybe 2-3) which get its time from Stratum 2 time provider and PC’s in your local network can get time from its local Stratum.


Some important terms related to NTP

Stepping: When time difference between consumer and provider is large then time adjust are made more quickly ~1min.

Slewing: When time difference is very less ~128ms the ntp will adjust time gradually around every 17 minutes

Insane Time: If time difference between consumer and provider is more than 17 min, then ntpd treats that time to be insane.

Drift: Your clock will drift due to fluctuations in the frequency oscillating the quartz crystal on your motherboard. A fluctuation of just 0.001% (0.00001, or 10 PPM) means losing or gaining about 1 second per day.

Latency: The time delay between when data is sent on a network to when it is received. Latency can make it difficult to synchronize processes over a network, especially when the latency is variable. The latency is added to the time it got from server to get the exact time.

Jitter: It is a measurement of the variance in latency on the network. If the latency on the network is constant, there is no jitter.


Configuring NTP

Here we will see how to configure a system to use NTP

#installing NTP on linux machine
yum install ntp
apt-get install ntp

Now we will edit the server for NTP. Open the file /etc/ntp.conf

And edit the server info as [server server_address]

you can get list of server from https://www.pool.ntp.org/en/. From the website choose the server closest to your geographical location.

Remembered, previously we talked about Insane Time (where system and server time difference is more than 17min), to prevent such situation we run below command to update the system time (close to actual time) to eliminate insane time issue

ntpdate server_address

#you can use google public ntp server 
#sometimes you need to run it more than once
ntpdate time.google.com

Note : Before running the above command make sure to turn of ntpd by [ntpd off] and start after executing above command [ntpd on].

Monitor time synchronization : ntpq -p , ntptrace
Log File of NTP :  /var/log/ntp.log


How is SNTP different from NTP ?

Simple Network Time Protocol (SNTP) is a simplified, client-only version of NTP. SNTP can receive only
the time from NTP servers; it cannot be used to provide time services to other systems.

SNTP typically provides time within 100 milliseconds of the accurate time, but it does not provide the complex
filtering and statistical mechanisms of NTP. In addition, SNTP does not authenticate traffic, although you
can configure extended access lists to provide some protection. An SNTP client is more vulnerable to servers
that have unexpected behavior than an NTP client, and should be used only in situations where strong
authentication is not required.

So that all to get the basics of NTP protocol if you are more interested to dig in, do visit Cisco NTP