Saturday 24 November 2018

Configuring chrony time server in Centos 7

In this quick article we'll demonstrate how to configure the chrony time service in Linux. Configuring chrony is an exam objective for the RedHat as well as Linux Foundation exams.
Chrony is touted as an alternative for the popular ntpd daemon that has been used in UNIX/Linux systems for years now. Chrony has become more popular in desktop environments since time synchronization using chrony is very quick making it ideal for systems that get restarted frequently.

To install chrony, type the following command:

[root@cent7~]# yum install chrony
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                                                                          | 7.7 kB  00:00:00
 * base: centos.mirror.myduniahost.com
 * epel: mirrors.aliyun.com
 * extras: mirror.0x.sg
 * updates: mirror.0x.sg
base                                                                                                                                          | 3.6 kB  00:00:00
epel                                                                                                                                          | 3.2 kB  00:00:00
extras                                                                                                                                        | 3.4 kB  00:00:00
updates                                                                                                                                       | 3.4 kB  00:00:00
(1/2): epel/x86_64/updateinfo                                                                                                                 | 932 kB  00:00:01
(2/2): epel/x86_64/primary                                                                                                                    | 3.6 MB  00:00:14
epel                                                                                                                                                     12716/12716
Package chrony-3.2-2.el7.x86_64 already installed and latest version
Nothing to do

Apparently it's already installed on my system.

Let's take a look at the available time servers in the chrony configuration file /etc/chrony.conf.

[root@cent7~]# grep -E '^server' /etc/chrony.conf
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
[root@cent7~]#

Now we'll enable and start the service.

[root@cent7 ~]# systemctl enable chronyd
Created symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.
[root@cent7 ~]# systemctl start chronyd

Let's check the status of the service now:

[root@cent7 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-11-24 11:53:55 IST; 5s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 1796 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 1792 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1794 (chronyd)
   CGroup: /system.slice/chronyd.service
           └─1794 /usr/sbin/chronyd

Nov 24 11:53:55 cent7 systemd[1]: Starting NTP client/server...
Nov 24 11:53:55 cent7 chronyd[1794]: chronyd version 3.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SECHASH +SIGND +ASYNCDNS +IPV6 +DEBUG)
Nov 24 11:53:55 cent7 systemd[1]: Started NTP client/server.

To obtain information about the time server we are currently syncing our time from, type the following command:

[root@cent7 ~]# chronyc tracking
Reference ID    : 768CB862 (sr-97-184-140-128-on-nets.com)
Stratum         : 3
Ref time (UTC)  : Sat Nov 24 06:24:05 2018
System time     : 0.000006578 seconds slow of NTP time
Last offset     : +0.007599837 seconds
RMS offset      : 0.007599837 seconds
Frequency       : 0.000 ppm slow
Residual freq   : +3831.410 ppm
Skew            : 1000000.000 ppm
Root delay      : 0.178650528 seconds
Root dispersion : 39.540195465 seconds
Update interval : 1.6 seconds
Leap status     : Normal

From the above output we can determine that we are currently syncing time from the server sr-97-184-140-128-on-nets.com.

To view the list of time servers available to us to sync from type the following command:

[root@cent7~]# chronyc sources
210 Number of sources = 4
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^- ntp.fra.de.as206479.net       2   6    37    53    +36ms[  +36ms] +/-  252ms
^* hkg1.m-d.net                  2   6    37    54  -1579us[  -17ms] +/-  117ms
^? 30-213-226-103-static.ch>     1   6     1    54  +1364us[  -14ms] +/-   93ms
^+ sr-97-184-140-128-on-net>     2   6    37    54  +1105us[+1105us] +/-  122ms

The * symbol indicates the time server we are currently syncing from.

I hope that you found this quick and easy setup of chrony to be useful.

Tuesday 13 November 2018

host command as an alternative to nslookup

The nslookup command is the go to command for performing hostname/IP address lookups. It's a standard command found in Linux, UNIX and Windows distributions alike. While writing scripts we may come across requirements wherein we need the hostname and IP address information to be extracted from the output of the command.

If we use nslookup for this purpose then we would need to employ some interesting awk or grep commands to get the desired result. Given below is an example:

[root@lpic ~]# nslookup google.com
Server:         192.168.178.1
Address:        192.168.178.1#53

Non-authoritative answer:
Name:   google.com
Address: 172.217.24.238

[root@lpic ~]# nslookup google.com | awk '/Address/ && !/#/ {print $2}'
172.217.160.206
[root@lpic ~]#

On the contrary now let's talk about the host command. host is a simple utility for performing DNS lookups. It is normally used to convert names to IP addresses and vice versa. But I found it's output easier to read and extract desired fields from it. Here's an example:

[root@lpic ~]# host google.com
google.com has address 172.217.24.238
google.com has IPv6 address 2404:6800:4002:803::200e
google.com mail is handled by 10 aspmx.l.google.com.
google.com mail is handled by 20 alt1.aspmx.l.google.com.
google.com mail is handled by 50 alt4.aspmx.l.google.com.
google.com mail is handled by 40 alt3.aspmx.l.google.com.
google.com mail is handled by 30 alt2.aspmx.l.google.com.
[root@lpic ~]#
[root@lpic ~]# host google.com | awk '/has address/ {print $4}'
172.217.24.238
[root@lpic ~]#

I hope this quick tip has been helpful for you.

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...