Monday, 12 July 2021

Docker: Getting started

Introduction
In this article, we be covering a basic introduction to the Docker Tool along with its installation on a Centos 7 system. Docker has become synonymous with containerization of applications in Linux and thousands of applications have successfully been dockerized and deployed in Production since Docker's inception.

History of Docker:
Docker, Inc. is a technology company based in San Francisco. It was founded by French born american Solomon Hykes and start as a PaaS provider called dotCloud. dotCloud leveraged Linux containers and their internal tool used to manage containers was nick-named Docker. In 2013 dotCloud was re branded as Docker.

What is Docker?
To understand what is Docker we must first know what are containers. A container is a special type of process that is isolated from other processes. Containers are assigned resources that no other process can access, and they cannot access any resources not explicitly assigned to them. Docker provides a Runtime and Orchestration Engine to allow users to deploy and manage containers. There are tools available that also allow for the orchestration of containers.

Docker is available in the following main editions:

Enterprise Edition (EE): Paid and supported for 12 months. Provides additional features like certified images, same day support and vulnerability scanning of images.
Community Edition (CE) : Free and supported for 4 months.
Moby: Open-Source upstream project of Docker and breaks Docker down into more modular components

Why should you use Docker?

Dev and Production environment are the same
Bugs in Production can be replicated in Development
Lets you put your environment and configuration into code and deploy it
Allows the same Docker configuration to be used in a variety of environments
Build standards and repeatable processes
Developer Productivity
App Isolation
Server Consolidation

Installing Docker:

Before installing Docker we must install some of the pre-requisite packages as shown below:

yum install -y yum-utils device-mapper-persistent-data lvm2

Now we will add the stable repository for the Docker community edition of the system:

[root@dokcer-test ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@dokcer-test ~]#

Now that the Docker repository has been configured, we can install Docker CE using yum.

 yum -y install docker-ce

The above command installs the following packages:

===========================================================================================================================================================
 Package                                 Arch                         Version                                 Repository                              Size
===========================================================================================================================================================
Installing:
 docker-ce                               x86_64                       3:19.03.1-3.el7                         docker-ce-stable                        24 M
Installing for dependencies:
 container-selinux                       noarch                       2:2.99-1.el7_6                          extras                                  39 k
 containerd.io                           x86_64                       1.2.6-3.3.el7                           docker-ce-stable                        26 M
 docker-ce-cli                           x86_64                       1:19.03.1-3.el7                         docker-ce-stable                        39 M


After the packages are successfully installed, we'll start the docker service and also enable it so that it starts automatically on boot.

[root@dokcer-test ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-07-30 04:38:00 UTC; 6s ago
     Docs: https://docs.docker.com
 Main PID: 9008 (dockerd)
   CGroup: /system.slice/docker.service
           └─9008 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Jul 30 04:37:58 dokcer-test.example.com dockerd[9008]: time="2019-07-30T04:37:58.649760132Z" level=info msg="Creating filesystem xfs on de...emapper
Jul 30 04:37:59 dokcer-test.example.com dockerd[9008]: time="2019-07-30T04:37:59.064964291Z" level=info msg="Successfully created filesyst...emapper
Jul 30 04:37:59 dokcer-test.example.com dockerd[9008]: time="2019-07-30T04:37:59.211113995Z" level=warning msg="[graphdriver] WARNING: the...elease"
Jul 30 04:37:59 dokcer-test.example.com dockerd[9008]: time="2019-07-30T04:37:59.269462315Z" level=info msg="Loading containers: start."
Jul 30 04:37:59 dokcer-test.example.com dockerd[9008]: time="2019-07-30T04:37:59.567854253Z" level=info msg="Default bridge (docker0) is a...ddress"
Jul 30 04:37:59 dokcer-test.example.com dockerd[9008]: time="2019-07-30T04:37:59.654696034Z" level=info msg="Loading containers: done."
Jul 30 04:38:00 dokcer-test.example.com dockerd[9008]: time="2019-07-30T04:38:00.354472432Z" level=info msg="Docker daemon" commit=74b1e89...19.03.1
Jul 30 04:38:00 dokcer-test.example.com dockerd[9008]: time="2019-07-30T04:38:00.355436639Z" level=info msg="Daemon has completed initialization"
Jul 30 04:38:00 dokcer-test.example.com systemd[1]: Started Docker Application Container Engine.
Jul 30 04:38:00 dokcer-test.example.com dockerd[9008]: time="2019-07-30T04:38:00.527177004Z" level=info msg="API listen on /var/run/docker.sock"
Hint: Some lines were ellipsized, use -l to show in full.
[root@dokcer-test ~]#


It is considered best practice to not manage containers directly as the root user. Therefore, I'll add a user named sahil to the docker group and use this user account for running and managing containers on this server.

usermod -aG docker sahil

Let's verify that we can run Docker related commands as the user sahil:

[sahil@docker-test ~]$ docker version
Client: Docker Engine - Community
 Version:           19.03.1
 API version:       1.40
 Go version:        go1.12.5
 Git commit:        74b1e89
 Built:             Thu Jul 25 21:21:07 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.1
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.5
  Git commit:       74b1e89
  Built:            Thu Jul 25 21:19:36 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.6
  GitCommit:        894b81a4b802e4eb2a91d1ce216b8817763c29fb
 runc:
  Version:          1.0.0-rc8
  GitCommit:        425e105d5a03fabd737a126ad93d62a9eeede87f
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
[sahil@docker-test ~]$


Conclusion
This concludes our introduction to Docker where we talked about the history of Docker along with it's most popular use cases and also installed Docker on a Centos 7 system.

Friday, 14 May 2021

Create VM in Google Cloud using cloud shell

 Introduction

In this article, 'll explain how we can create a virtual machine in Google Cloud using the command line tool gcloud which is pre-installed in google cloud shell. Cloud Shell is a Debian-based virtual machine loaded with all the development tools you'll need (gcloud, git, and others) and offers a persistent 5-GB home directory. Let's get started.


Step 1: Open cloud Shell

To open cloud shell, click on the shell icon on the top let corner of the google cloud home page.


Step 2: Authenticate 

After clicking on the shell icon, google cloud shell is launched in a terminal window towards the bottom of the home page. Here type 'gcloud auth list' to authenticate your user account to use cloud shell to access and manipulate google cloud resources. You may also type 'gcloud config list project' to check the project you are working under. The term project is analogous to the term organization in AWS.




Step 3: Create virtual machine

Now, we will create our virtual machine using the following command:

gcloud compute instances create gcelab2 --machine-type n1-standard-2 --zone us-central1-f

  • "compute instances" is the google cloud component we'd like to work with.
  • "create" is the action that we'd like to perform.
  • gcelab2 is the name of the VM
  • "machine-type" defines the CPU/Memory configuration that we would like to use and this is divided into entities called series wherein each series comprises CPU/Memory settings for VMs. In this example we've selected n1-standard-2.
  • "zone" attribute specifies the location where the VM will reside.



Step 4: Verify login to the VM

We should now be able see our VM in the VM instances section under the Compute Engine.


You can either click on ssh under the connect section to login to the VM or use the following command in the cloud shell:

gcloud compute ssh gcelab2 --zone us-central1-f


Conclusion

We hope that you found this post to be useful and we look forward towards your feedback and suggestions

Saturday, 26 September 2020

Setup and use lxc on Centos 8

 Introduction

In this article, we will learn the basics of lxc containerization technology and launch our first lxc container on a Centos 8 machine.


What is lxc?

LXC (Linux Containers) is an OS-level virtualization technology that allows the creation and running of multiple isolated Linux virtual environments (VE) on a single control host. These isolation levels or containers can be used to either sandbox specific applications, or to emulate an entirely new host.


Now let's gor through the steps of installing lxc on our Centos 8 machine.


Step 1: Enable the EPEL repo

[root@sa-lxc ~]# yum install epel-release

Last metadata expiration check: 0:32:41 ago on Sat 26 Sep 2020 07:40:13 PM IST.

Dependencies resolved.

==========================================================================

 Package                         Arch                      Version                     Repository                 Size

==========================================================================

Installing:

 epel-release                    noarch                    8-8.el8                     extras                     23 k


Transaction Summary

==========================================================================

Install  1 Package


Total download size: 23 k

Installed size: 32 k

Is this ok [y/N]: y

Downloading Packages:

epel-release-8-8.el8.noarch.rpm                                                         19 kB/s |  23 kB     00:01

-----------------------------------------------------------------------------------------------------------------------

Total                                                                                  9.3 kB/s |  23 kB     00:02

warning: /var/cache/dnf/extras-2770d521ba03e231/packages/epel-release-8-8.el8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY

CentOS-8 - Extras                                                                      1.3 MB/s | 1.6 kB     00:00

Importing GPG key 0x8483C65D:

 Userid     : "CentOS (CentOS Official Signing Key) <security@centos.org>"

 Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D

 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

Is this ok [y/N]: y

Key imported successfully

Running transaction check

Transaction check succeeded.

Running transaction test

Transaction test succeeded.

Running transaction

  Preparing        :                                                                                               1/1

  Installing       : epel-release-8-8.el8.noarch                                                                   1/1

  Running scriptlet: epel-release-8-8.el8.noarch                                                                   1/1

  Verifying        : epel-release-8-8.el8.noarch                                                                   1/1


Installed:

  epel-release-8-8.el8.noarch

Complete!



Step 2: Enable and increase user namespaces

To do so, we will need to modify the grub bootloader options and this will also entail a system reboot.

[root@sa-lxc ~]# grubby --args="namespace.unpriv_enable=1" --update-kernel="$(grubby --default-kernel)"

[root@sa-lxc ~]# echo "user.max_user_namespaces=2277484249" >> /etc/sysctl.d/99-userns.conf


Now reboot the system.


Step 3: Install snappy

The latest version of lxc is made available via the Snappy package manager. Snappy is a modern, cross-distribution, transactional package manager designed for working with self-contained, immutable packages. In order to install lxc we need to have the snappy package manager installed on the system and the corresponding services to be running.

[root@sa-lxc ~]# yum install snapd -y
Extra Packages for Enterprise Linux Modular 8 - x86_64                                  34 kB/s | 132 kB     00:03
Extra Packages for Enterprise Linux 8 - x86_64                                         2.0 MB/s | 8.0 MB     00:03
Dependencies resolved.
==========================================================================
 Package                                  Arch               Version                          Repository          Size
=========================================================================
Installing:
 snapd                                    x86_64             2.45.3.1-1.el8                   epel                18 M
Upgrading:
 libselinux                               x86_64             2.9-3.el8                        BaseOS             166 k
 libselinux-utils                         x86_64             2.9-3.el8                        BaseOS             244 k
 libsemanage                              x86_64             2.9-2.el8                        BaseOS             165 k
 libsepol                                 x86_64             2.9-1.el8                        BaseOS             340 k
 policycoreutils                          x86_64             2.9-9.el8                        BaseOS             377 k
 policycoreutils-python-utils             noarch             2.9-9.el8                        BaseOS             251 k
 python3-libselinux                       x86_64             2.9-3.el8                        BaseOS             283 k
 python3-libsemanage                      x86_64             2.9-2.el8                        BaseOS             127 k
 python3-policycoreutils                  noarch             2.9-9.el8                        BaseOS             2.2 M
 selinux-policy                           noarch             3.14.3-41.el8_2.6                BaseOS             615 k
 selinux-policy-targeted                  noarch             3.14.3-41.el8_2.6                BaseOS              15 M
Installing dependencies:
 snap-confine                             x86_64             2.45.3.1-1.el8                   epel               3.3 M
 snapd-selinux                            noarch             2.45.3.1-1.el8                   epel               326 k

Transaction Summary
==========================================================================
Install   3 Packages
Upgrade  11 Packages

Total download size: 42 M
Downloading Packages:
(1/14): snapd-selinux-2.45.3.1-1.el8.noarch.rpm                                        231 kB/s | 326 kB     00:01
(2/14): snap-confine-2.45.3.1-1.el8.x86_64.rpm                                         2.1 MB/s | 3.3 MB     00:01
(3/14): snapd-2.45.3.1-1.el8.x86_64.rpm                                                9.6 MB/s |  18 MB     00:01
(4/14): libselinux-utils-2.9-3.el8.x86_64.rpm                                          549 kB/s | 244 kB     00:00
(5/14): libselinux-2.9-3.el8.x86_64.rpm                                                247 kB/s | 166 kB     00:00
(6/14): libsemanage-2.9-2.el8.x86_64.rpm                                               442 kB/s | 165 kB     00:00
(7/14): libsepol-2.9-1.el8.x86_64.rpm                                                  1.1 MB/s | 340 kB     00:00
(8/14): policycoreutils-2.9-9.el8.x86_64.rpm                                           1.4 MB/s | 377 kB     00:00
(9/14): policycoreutils-python-utils-2.9-9.el8.noarch.rpm                              1.9 MB/s | 251 kB     00:00
(10/14): python3-libsemanage-2.9-2.el8.x86_64.rpm                                      1.7 MB/s | 127 kB     00:00
(11/14): python3-libselinux-2.9-3.el8.x86_64.rpm                                       1.8 MB/s | 283 kB     00:00
(12/14): selinux-policy-3.14.3-41.el8_2.6.noarch.rpm                                   3.0 MB/s | 615 kB     00:00
(13/14): python3-policycoreutils-2.9-9.el8.noarch.rpm                                  8.1 MB/s | 2.2 MB     00:00
(14/14): selinux-policy-targeted-3.14.3-41.el8_2.6.noarch.rpm                           17 MB/s |  15 MB     00:00
-----------------------------------------------------------------------------------------------------------------------
Total                                                                                  7.2 MB/s |  42 MB     00:05
warning: /var/cache/dnf/epel-6519ee669354a484/packages/snap-confine-2.45.3.1-1.el8.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 2f86d6a1: NOKEY
Extra Packages for Enterprise Linux 8 - x86_64                                         1.6 MB/s | 1.6 kB     00:00
Importing GPG key 0x2F86D6A1:
 Userid     : "Fedora EPEL (8) <epel@fedoraproject.org>"
 Fingerprint: 94E2 79EB 8D8F 25B2 1810 ADF1 21EA 45AB 2F86 D6A1
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                               1/1
  Running scriptlet: libsepol-2.9-1.el8.x86_64                                                                     1/1
  Upgrading        : libsepol-2.9-1.el8.x86_64                                                                    1/25
  Running scriptlet: libsepol-2.9-1.el8.x86_64                                                                    1/25
  Upgrading        : libselinux-2.9-3.el8.x86_64                                                                  2/25
  Running scriptlet: libselinux-2.9-3.el8.x86_64                                                                  2/25
  Upgrading        : libselinux-utils-2.9-3.el8.x86_64                                                            3/25
  Upgrading        : libsemanage-2.9-2.el8.x86_64                                                                 4/25
  Upgrading        : policycoreutils-2.9-9.el8.x86_64                                                             5/25
  Running scriptlet: policycoreutils-2.9-9.el8.x86_64                                                             5/25
  Upgrading        : python3-libselinux-2.9-3.el8.x86_64                                                          6/25
  Upgrading        : python3-libsemanage-2.9-2.el8.x86_64                                                         7/25
  Upgrading        : python3-policycoreutils-2.9-9.el8.noarch                                                     8/25
  Upgrading        : policycoreutils-python-utils-2.9-9.el8.noarch                                                9/25
  Upgrading        : selinux-policy-3.14.3-41.el8_2.6.noarch                                                     10/25
  Running scriptlet: selinux-policy-3.14.3-41.el8_2.6.noarch                                                     10/25
  Running scriptlet: selinux-policy-targeted-3.14.3-41.el8_2.6.noarch                                            11/25
  Upgrading        : selinux-policy-targeted-3.14.3-41.el8_2.6.noarch                                            11/25
  Running scriptlet: selinux-policy-targeted-3.14.3-41.el8_2.6.noarch                                            11/25

  Running scriptlet: snapd-selinux-2.45.3.1-1.el8.noarch                                                         12/25
  Installing       : snapd-selinux-2.45.3.1-1.el8.noarch                                                         12/25
  Running scriptlet: snapd-selinux-2.45.3.1-1.el8.noarch                                                         12/25
  Installing       : snap-confine-2.45.3.1-1.el8.x86_64                                                          13/25
  Installing       : snapd-2.45.3.1-1.el8.x86_64                                                                 14/25
  Running scriptlet: snapd-2.45.3.1-1.el8.x86_64                                                                 14/25
  Cleanup          : selinux-policy-targeted-3.14.1-61.el8.noarch                                                15/25
  Running scriptlet: selinux-policy-targeted-3.14.1-61.el8.noarch                                                15/25
  Cleanup          : selinux-policy-3.14.1-61.el8.noarch                                                         16/25
  Running scriptlet: selinux-policy-3.14.1-61.el8.noarch                                                         16/25
  Cleanup          : policycoreutils-python-utils-2.8-16.1.el8.noarch                                            17/25
  Cleanup          : python3-policycoreutils-2.8-16.1.el8.noarch                                                 18/25
  Running scriptlet: policycoreutils-2.8-16.1.el8.x86_64                                                         19/25
  Cleanup          : policycoreutils-2.8-16.1.el8.x86_64                                                         19/25
  Cleanup          : python3-libsemanage-2.8-5.el8.x86_64                                                        20/25
  Cleanup          : libsemanage-2.8-5.el8.x86_64                                                                21/25
  Cleanup          : python3-libselinux-2.8-6.el8.x86_64                                                         22/25
  Cleanup          : libselinux-utils-2.8-6.el8.x86_64                                                           23/25
  Cleanup          : libselinux-2.8-6.el8.x86_64                                                                 24/25
  Cleanup          : libsepol-2.8-2.el8.x86_64                                                                   25/25
  Running scriptlet: libsepol-2.8-2.el8.x86_64                                                                   25/25
  Running scriptlet: snapd-selinux-2.45.3.1-1.el8.noarch                                                         25/25
  Running scriptlet: libsepol-2.8-2.el8.x86_64                                                                   25/25
  Verifying        : snap-confine-2.45.3.1-1.el8.x86_64                                                           1/25
  Verifying        : snapd-2.45.3.1-1.el8.x86_64                                                                  2/25
  Verifying        : snapd-selinux-2.45.3.1-1.el8.noarch                                                          3/25
  Verifying        : libselinux-2.9-3.el8.x86_64                                                                  4/25
  Verifying        : libselinux-2.8-6.el8.x86_64                                                                  5/25
  Verifying        : libselinux-utils-2.9-3.el8.x86_64                                                            6/25
  Verifying        : libselinux-utils-2.8-6.el8.x86_64                                                            7/25
  Verifying        : libsemanage-2.9-2.el8.x86_64                                                                 8/25
  Verifying        : libsemanage-2.8-5.el8.x86_64                                                                 9/25
  Verifying        : libsepol-2.9-1.el8.x86_64                                                                   10/25
  Verifying        : libsepol-2.8-2.el8.x86_64                                                                   11/25
  Verifying        : policycoreutils-2.9-9.el8.x86_64                                                            12/25
  Verifying        : policycoreutils-2.8-16.1.el8.x86_64                                                         13/25
  Verifying        : policycoreutils-python-utils-2.9-9.el8.noarch                                               14/25
  Verifying        : policycoreutils-python-utils-2.8-16.1.el8.noarch                                            15/25
  Verifying        : python3-libselinux-2.9-3.el8.x86_64                                                         16/25
  Verifying        : python3-libselinux-2.8-6.el8.x86_64                                                         17/25
  Verifying        : python3-libsemanage-2.9-2.el8.x86_64                                                        18/25
  Verifying        : python3-libsemanage-2.8-5.el8.x86_64                                                        19/25
  Verifying        : python3-policycoreutils-2.9-9.el8.noarch                                                    20/25
  Verifying        : python3-policycoreutils-2.8-16.1.el8.noarch                                                 21/25
  Verifying        : selinux-policy-3.14.3-41.el8_2.6.noarch                                                     22/25
  Verifying        : selinux-policy-3.14.1-61.el8.noarch                                                         23/25
  Verifying        : selinux-policy-targeted-3.14.3-41.el8_2.6.noarch                                            24/25
  Verifying        : selinux-policy-targeted-3.14.1-61.el8.noarch                                                25/25

Upgraded:
  libselinux-2.9-3.el8.x86_64                                 libselinux-utils-2.9-3.el8.x86_64
  libsemanage-2.9-2.el8.x86_64                                libsepol-2.9-1.el8.x86_64
  policycoreutils-2.9-9.el8.x86_64                            policycoreutils-python-utils-2.9-9.el8.noarch
  python3-libselinux-2.9-3.el8.x86_64                         python3-libsemanage-2.9-2.el8.x86_64
  python3-policycoreutils-2.9-9.el8.noarch                    selinux-policy-3.14.3-41.el8_2.6.noarch
  selinux-policy-targeted-3.14.3-41.el8_2.6.noarch

Installed:
  snapd-2.45.3.1-1.el8.x86_64       snap-confine-2.45.3.1-1.el8.x86_64       snapd-selinux-2.45.3.1-1.el8.noarch

Complete!


Step 4: Enable and start snappy


[root@sa-lxc ~]# ln -s /var/lib/snapd/snap /snap
[root@sa-lxc ~]# systemctl enable --now snapd.socket
Created symlink /etc/systemd/system/sockets.target.wants/snapd.socket → /usr/lib/systemd/system/snapd.socket.
[root@sa-lxc ~]# systemctl enable snapd.service
Created symlink /etc/systemd/system/multi-user.target.wants/snapd.service → /usr/lib/systemd/system/snapd.service.
[root@sa-lxc ~]# systemctl start snapd.service


Step 5: Install lxc


[root@sa-lxc ~]# snap install lxd
lxd 4.6 from Canonical✓ installed


Step 6: Configure lxc

To configure lxc we need to type the lxd init command and answer a couple of prompts as shown below.

[root@sa-lxc ~]# lxd init
Would you like to use LXD clustering? (yes/no) [default=no]:
Do you want to configure a new storage pool? (yes/no) [default=yes]:
Name of the new storage pool [default=default]: mylxc
Name of the storage backend to use (lvm, ceph, btrfs, dir) [default=btrfs]: lvm
Create a new LVM pool? (yes/no) [default=yes]: yes
Would you like to use an existing empty disk or partition? (yes/no) [default=no]:
Size in GB of the new loop device (1GB minimum) [default=5GB]:
Would you like to connect to a MAAS server? (yes/no) [default=no]:
Would you like to create a new local network bridge? (yes/no) [default=yes]:
What should the new bridge be called? [default=lxdbr0]:
What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: none
Would you like LXD to be available over the network? (yes/no) [default=no]: yes
Address to bind LXD to (not including port) [default=all]:
Port to bind LXD to [default=8443]:
Trust password for new clients:
Again:
No password set, client certificates will have to be manually trusted. Would you like stale cached images to be updated automatically? (yes/no) [default=yes]
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]:


Step 7: Launch a container

Once lxd has been initialized we can go ahead and launch our first container using the lxc launch command.

[root@sa-lxc ~]# lxc launch ubuntu:18.04
Creating the instance
Instance name is: caring-dane
Starting caring-dane

To view currently running containers use the lxc list command.

[root@sa-lxc ~]# lxc list
+-------------+---------+-----------------------+------+-----------+-----------+
|    NAME     |  STATE  |         IPV4          | IPV6 |   TYPE    | SNAPSHOTS |
+-------------+---------+-----------------------+------+-----------+-----------+
| caring-dane | RUNNING | 10.14.41.189 (eth0) |      | CONTAINER | 0         |
+-------------+---------+-----------------------+------+-----------+-----------+
[root@sa-lxc ~]#

Note: Since this was just a lab setup, I had stopped the firewalld service on my Centos system. In a production setup, be sure to add the appropriate firewall rules to allow the setup to work.

Conclusion

We hope that this article has been helpful to you and we look forward to your suggestions and feedback.


Friday, 14 August 2020

Generate .pem file from .ppk file

 Introduction

I recently came across a requirement from our DBA Team wherein they needed to access a server on Oracle Cloud and all they had was a .ppk file received from the App Team that managed the OCI server. Since the source server from which we had to connect did not have a GUI, we needed to generate a corresponding .pem file to use on the command line.

For this, I installed putty on a lab machine.


yum install putty


 I then used the .ppk file to generate a .pem file using the following command:


puttygen oci.ppk -O private-openssh -o oci.pem


This step did prompt me for the passphrase for the .ppk file which I was are of.

To login to the OCI server, I used .pem file by specifying the -i option with the ssh command as follows.


-bash-4.1$ ssh -i oci.pem opc@11.59.17.209

Enter passphrase for key 'oci.pem':

Last login: Thu Aug 13 07:17:30 UTC 2020 from 10.22.88.26 on pts/0

Last login: Thu Aug 13 07:18:17 2020 from 10.22.88.26


The passphrase was the same one I was prompted for while generating the .pem file.


Conclusion

This concludes this post on how to generate a .pem file from a .ppk file. I hope you found the post to be useful.

Thursday, 21 May 2020

Add color to Message of the Day (MOTD in Centos/RHEL

Introduction

In this quick article, I'll demonstrate how to add some color to your message of the day (MOTD) which is some text that is displayed on the screen when you first log in.

We'll not be using the /etc/motd file for this so this file should be empty.

We'll create a script in /etc/profile.d/ that contains the text we'd like to print. Here I've named the script login-info.sh.

[ssuri@lab-node:~] $ cat /etc/profile.d/login-info.sh
#! /usr/bin/env bash

GREEN="\033[01;32m"
YELLOW="\033[01;33m"
MAGENTA="\033[01;35m"
CYAN="\033[01;36m"
NC="\033[00m"

echo -e "${GREEN}================================${NC}"
echo -e "${YELLOW}Welcome to the Ansible LAB Box${NC}"
echo -e "${GREEN}================================${NC}"
[ssuri@lab-node:~] $

I've used escape sequences with the echo command to display text in green and yellow colors when printed.

Next, we need to add the following line in /etc/ssh/sshd_config file and restart the sshd service.

PrintMotd no

That's it. The next time we login we'll be greeted with a colorful login message.

Conclusion

I've used green and yellow colors in this example but you can use a different color sequence. You may also use the output of commands within the script to make the login message more informative.

Monday, 12 August 2019

Bash: Use for loop to iterate over file without using cat

Introduction:

In this very brief post I just wanted to demonstrate a way to use the for loop to iterate over a file without using the cat command.

Here is a file containing 10 numbers, 1 number on each line.

cat seq.txt
1
2
3
4
5
6
7
8
9
10

If I wanted to loop over it using for loop in bash I could type:

for i in `cat seq.txt`; do echo $i ;done

But what if I don't want to read in the file using cat command?
I could use input redirection to read in the file contents as shown in the below example:

for i in $(<seq.txt); do echo $i ; done
1
2
3
4
5
6
7
8
9
10
                            
One more way of avoiding the cat command would be to read the file into an array and then loop over the array as shown below.

readarray -t ARR < seq.txt

for i in "${ARR[@]}"; do echo $i; done
1
2
3
4
5
6
7
8
9
10


Conclusion

There is nothing wrong with using the cat command to read in file contents while iterating over it in a for loop. I just found redirecting directly from STDIN to be more elegant and I would try to use it more often.

Friday, 2 August 2019

Git cheat sheet

Introduction

This post contains a git cheat sheet that I've created for my use. The content is very succinct. I'll be writing more elaborate posts on installing and using git in the future. Also, this will be an ongoing post and will be periodically updated.


Here's the cheat sheet:


Initialize repository:

[sahil@lab-node:~/rep] $ git init .

Check for anything for ready to be staged or committed:
[sahil@lab-node:~/rep] $ git status



Staging changes:


[sahil@lab-node:~/rep] $ git add 1.bash
[sahil@lab-node:~/rep] $ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   1.bash
#


Commit the changes to the repo:


[sahil@lab-node:~/rep] $ git commit -m "version 1"
[master (root-commit) 252f733] version 1
 1 file changed, 10 insertions(+)
 create mode 100644 1.bash


View log of commits:


[sahil@lab-node:~/rep] $ git log
commit 252f733ded22f7634725961bfd95ecdc0826a69c
Author: Sahil Suri <sahil.suri@emerson.com>
Date:   Fri Aug 2 05:30:02 2019 +0000

    version 1


View one line logs:

[sahil@lab-node:~/rep] $ git log --oneline
3ee84d7 version 2
252f733 version 1


View the difference between commits:

[sahil@lab-node:~/rep] $ git log --graph -p
* commit 3ee84d747f009d7cbbe6f12b3eb71bd39da14220
| Author: Sahil Suri <sahil.suri@emerson.com>
| Date:   Fri Aug 2 05:32:29 2019 +0000
|
|     version 2
|
| diff --git a/1.bash b/1.bash
| index e193461..2ab8d4e 100644
| --- a/1.bash
| +++ b/1.bash
| @@ -7,4 +7,4 @@
|  #version:
|  ##############################################################
|
| -echo "This is testing version 1"
| +echo "This is testing version 2"
|
* commit 252f733ded22f7634725961bfd95ecdc0826a69c
  Author: Sahil Suri <sahil.suri@emerson.com>
  Date:   Fri Aug 2 05:30:02 2019 +0000

      version 1

  diff --git a/1.bash b/1.bash
  new file mode 100644
  index 0000000..e193461
  --- /dev/null
  +++ b/1.bash
  @@ -0,0 +1,10 @@
  +#!/bin/bash
  +
  +##############################################################
  +#Author: Sahil Suri
  +#Date:
  +#Purpose:
  +#version:
  +##############################################################
  +
  +echo "This is testing version 1"


View differences b/w current version and a particular version:


#git diff <commit hash>

[sahil@lab-node:~/rep] $ git log --oneline
c9ef444 this is version 3'
3ee84d7 version 2
252f733 version 1

[sahil@lab-node:~/rep] $ git diff 3ee84d7
diff --git a/1.bash b/1.bash
index 2ab8d4e..c2b325e 100644
--- a/1.bash
+++ b/1.bash
@@ -7,4 +7,4 @@
 #version:
 ##############################################################

-echo "This is testing version 2"
+echo "This is testing version 3"

[sahil@lab-node:~/rep] $ git diff 252f733
diff --git a/1.bash b/1.bash
index e193461..c2b325e 100644
--- a/1.bash
+++ b/1.bash
@@ -7,4 +7,4 @@
 #version:
 ##############################################################

-echo "This is testing version 1"
+echo "This is testing version 3"
[sahil@lab-node:~/rep] $


Retrieving previous versions of files:

This has a couple of steps involved:

Retrieve version of 1.bash two commits before:

[sahil@lab-node:~/rep] $ git checkout HEAD~2 1.bash

 To save this as the current version of a file, use git commit.

To go back to the updated version follow the below steps:

[sahil@lab-node:~/rep] $ git reset HEAD 1.bash
[sahil@lab-node:~/rep] $ git checkout  1.bash


Add a remote to git repository:

git remote add origin https://github.com/sahilsuri008/creating_shell_scripts_linux


To push updates to GitHub:

git push origin master


Conclusion

That's it for this post for now. Thank you for taking the time to read it.

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...