Friday, September 27, 2019

E-Books and free resources

Python E-Books

Monday, July 15, 2019

Setting up private registry for docker images

The registry is a stateless and highly scalable server-side open source application permissive under Apache license, which let you store your images privately and makes internal distribution and management easy. In case you don't want private registry docker hub is the best place to store your images.

To configure own private registry follow below steps:


1. To start the registry container, which will be used as local registry on the docker host:

[root@localhost ~]# docker run -d -p 5000:5000 --restart=always --name registry -v /local_registry/images:/var/lib/registry registry:2
Unable to find image 'registry:2' locally
2: Pulling from library/registry
c87736221ed0: Pull complete
1cc8e0bb44df: Pull complete
54d33bcb37f5: Pull complete
e8afc091c171: Pull complete
b4541f6d3db6: Pull complete
Digest: sha256:77a8fb00c00b99568772a70f0863f6192ff2635e4af4e22e4d9c622edeb5f2de
Status: Downloaded newer image for registry:2
97f0845f8c3a0064ef0144aba57610e4c4f3d79947eed734d045f4d65b33a754


NOTE: In '-v /local_registry/images:/var/lib/registry' switch "/local_registry/images" is a folder on the host which will be used to store images and will be bind to "/var/lib/registry" container folder.


[root@localhost ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
97f0845f8c3a        registry:2          "/entrypoint.sh /etc…"   15 seconds ago      Up 13 seconds       0.0.0.0:5000->5000/tcp   registry


2. Pull any image from Docker public registry [Optional, as i don't have any other image for this demo]:

[root@localhost ~]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
e7c96db7181b: Pull complete
Digest: sha256:769fddc7cc2f0a1c35abb2f91432e8beecf83916c421420e6a6da9f8975464b6
Status: Downloaded newer image for alpine:latest


3. Tag image within local registry with new name:


[root@localhost ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              055936d39205        13 days ago         5.53MB
nginx               latest              53f3fd8007f7        2 weeks ago         109MB
registry            2                   f32a97de94e1        2 months ago        25.8MB


[root@localhost ~]# docker tag alpine:latest localhost:5000/my-alpine-latest


[root@localhost ~]# docker image ls
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
alpine                            latest              055936d39205        13 days ago         5.53MB
localhost:5000/my-alpine-latest   latest              055936d39205        13 days ago         5.53MB
nginx                             latest              53f3fd8007f7        2 weeks ago         109MB
registry                          2                   f32a97de94e1        2 months ago        25.8MB


4. Push newly tagged image to local registry:

[root@localhost ~]# docker push localhost:5000/my-alpine-latest
The push refers to repository [localhost:5000/my-alpine-latest]
f1b5933fe4b5: Pushed
latest: digest: sha256:bf1684a6e3676389ec861c602e97f27b03f14178e5bc3f70dce198f9f160cce9 size: 528


[root@localhost ~]# docker image ls
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
alpine                            latest              055936d39205        13 days ago         5.53MB
localhost:5000/my-alpine-latest   latest              055936d39205        13 days ago         5.53MB
nginx                             latest              53f3fd8007f7        2 weeks ago         109MB
registry                          2                   f32a97de94e1        2 months ago        25.8MB


5. [OPTIONAL] you can delete images downloaded from Docker public registry and the tagged one.

[root@localhost ~]# docker image rm alpine:latest
Untagged: alpine:latest
Untagged: alpine@sha256:769fddc7cc2f0a1c35abb2f91432e8beecf83916c421420e6a6da9f8975464b6


[root@localhost ~]# docker image rm localhost:5000/my-alpine-latest
Untagged: localhost:5000/my-alpine-latest:latest
Untagged: localhost:5000/my-alpine-latest@sha256:bf1684a6e3676389ec861c602e97f27b03f14178e5bc3f70dce198f9f160cce9
Deleted: sha256:055936d3920576da37aa9bc460d70c5f212028bda1c08c0879aedf03d7a66ea1
Deleted: sha256:f1b5933fe4b5f49bbe8258745cf396afe07e625bdab3168e364daf7c956b6b81


6. To pull locally pushed image:

[root@localhost ~]# docker pull localhost:5000/my-alpine-latest
Using default tag: latest
latest: Pulling from my-alpine-latest
e7c96db7181b: Pull complete
Digest: sha256:bf1684a6e3676389ec861c602e97f27b03f14178e5bc3f70dce198f9f160cce9
Status: Downloaded newer image for localhost:5000/my-alpine-latest:latest


If you notice, this pushed image will get store to below path:

/local_registry/images/docker/registry/v2/repositories/my-alpine-latest

Unable to pull images from docker hub in your environment ?

A couple of days back I faced an issue where I was able to pull images from docker hub on my laptop when connecting from my home and same was failing when connecting in office. I tried setting a proxy on my RHEL OS with "export" env setting but still, it didn't work. Then after googling figured out the exact root cause.

Setting up proxy on OS environment (using export with RHEL) is not enough in case of docker, here you need to set proxy on one more level i.e. on docker settings. To do this follow the below steps:

Create a new file "http-proxy.conf" if not exists in the below folder with shown contents:

[root@localhost ~]# vi /etc/systemd/system/docker.service.d/http-proxy.conf

[Service]
Environment="HTTP_PROXY=http://proxy1.localnet.net:8080/"
Environment="HTTPS_PROXY=http://proxy1.localnet.net:8080/"

save and quit.


Restart docker service:
[root@localhost ~]# systemctl daemon-reload

[root@localhost ~]# systemctl restart docker


Verify if proxy is configured in docker settings:

[root@localhost ~]# docker info
.
.
.
HTTP Proxy: http://proxy1.localnet.net:8080/
HTTPS Proxy: http://proxy1.localnet.net:8080/
.
.
.

[root@localhost yum.repos.d]# systemctl show --property Environment docker
Environment=HTTP_PROXY=http://proxy1.localnet.net:8080/ HTTPS_PROXY=http://proxy1.localnet.net:8080/

Try pulling image from docker hub, not it should work fine.

Preparing RHEL 7.5 base image for docker container from scratch

Whenever we start working with a container, to start you need a base image for any specific Operating system as per your requirement. You can get base images for many OS distributions except RHEL OS as it needs licensing. So in case, you want to prepare a base image of RHEL operating system you can follow below steps which are pretty straight forward: 

Prerequisites:

Mount RHEL 7.5 ISO on /mnt and create a .repo file:

[root@localhost yum.repos.d]# cat /etc/yum.repos.d/local.repo
[localrepo]
name=Local Repository
baseurl=file:///mnt
gpgcheck=0
enabled=1


OPTIONAL: HTTP/HTTPS repository is needed when you need to install any packages inside a container, or while preparing containers.

[root@localhost yum.repos.d]# cat httplocal.repo
[rhel-core]
name=RHEL 7.5 Core
baseurl=http://web1.repo.local/linux/RedHat/7.5Server/en/os/x86_64/
gpgcheck=0
enabled=1

OPTIONAL: Set proxy if needed in your network.


Steps to create a base image:

[root@localhost ~]# export img_root=/rhel7-root

[root@localhost ~]# rpm --root ${img_root} --initdb

[root@localhost ~]# rpm --root ${img_root} -ivh /mnt/Packages/redhat-release-server-7.5-8.el7.x86_64.rpm
warning: /mnt/Packages/redhat-release-server-7.5-8.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:redhat-release-server-7.5-8.el7  ################################# [100%]

[root@localhost ~]# rm -f ${img_root}/etc/yum.repos.d/*.repo

[root@localhost ~]# mkdir /rhel7-root/etc/yum.repos.d/

[root@localhost ~]# cp /etc/yum.repos.d/httplocal.repo ${img_root}/etc/yum.repos.d/httplocal.repo

[root@localhost ~]# rpm --root ${img_root} --import  /mnt/RPM-GPG-KEY-redhat-*

[root@localhost ~]# yum -y --installroot=${img_root} install yum

[root@localhost ~]# yum -y --installroot=${img_root} install net-tools iputils

Note: "net-tools and iputils" packages are optional, but in case if you won't install these packages, you will not be able to communicate to container as networking will be disabled. 

Once packages are installed check if chroot to the image location is working fine.

[root@localhost ~]# chroot ${img_root} /bin/bash
bash-4.2# ls

bash-4.2# exit
exit


[root@localhost ~]# rm -rf “${img_root}”/var/lib/rpm/__db*

[root@localhost ~]# rm -rf “${img_root}”/var/cache/yum

[root@localhost ~]# yum --installroot=${img_root} history sync

[root@localhost ~]# yum --installroot=${img_root} clean all


Create a tar file for a image filesystem and import it as an working base docker image:

[root@localhost ~]# tar -C ${img_root}/ -c . | docker import - rhel75base
sha256:4fe7ebbf140cabf573f7ce2c69a71f165c1a8d91d257d95e3ef073b9ca7f4213

Start a container and check if image is working fine and you are able to see virtual ethernet interface:

[root@localhost ~]# docker run -it rhel75base bash
bash-4.2# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 6  bytes 508 (508.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


Note: Once in the container, you might need to set up proxy to reach httplocal.repo in case of further package installation.

Saturday, May 18, 2019

How to disable system reboot using “Alt+Ctrl+Del” combination in CentOS/RHEL 7

In CentOS/RHEL operating systems, pressing "Ctrl + Alt + Del" key combination by default initiates a system reboot without any user confirmation. In case if you want to disable this behavior you can run below command:

[root@localhost ~]# systemctl mask ctrl-alt-del.target

OR

[root@localhost ~]# ln -sf /dev/null /etc/systemd/system/ctrl-alt-del.target

Now you can try pressing "Ctrl + Alt + Del" key combination and you will see instead of rebooting system it will log below messages to log file "/var/log/messages":

May 18 13:16:07 localhost systemd: Received SIGINT.
May 18 13:16:07 localhost systemd: Failed to enqueue ctrl-alt-del.target job: Unit is masked.


To enable "Ctrl + Alt + Del" key combination for system reboot:

[root@localhost ~]# systemctl unmask ctrl-alt-del.target

Thursday, April 18, 2019

How to monitor disk performance on RHEL or CentOS 7 ?

How to monitor disk performance on RHEL or CentOS 7 ?


iotop : Watches I/O usage information output by the Linux kernel (requires 2.6.20 or later) and displays a table of current I/O usage by processes or threads on the system. At least the CONFIG_TASK_DELAY_ACCT, CONFIG_TASK_IO_ACCOUNTING, CONFIG_TASKSTATS and CONFIG_VM_EVENT_COUNTERS options need to be enabled in your Linux kernel build configuration. iotop displays columns for the I/O bandwidth read and written by each process/thread during the sampling period. It also displays the percentage of time the thread/process spent while swapping in and while waiting on I/O. For each process, its I/O priority (class/level) is shown.

[root@localhost ~]# iotop

Total DISK READ :       0.00 B/s | Total DISK WRITE :       0.00 B/s
Actual DISK READ:       0.00 B/s | Actual DISK WRITE:       0.00 B/s
   TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND
 14856 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.08 % [kworker/3:0]
     1 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % systemd --switched-root --system --deserialize 22
     2 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kthreadd]
     3 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksoftirqd/0]
     5 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/0:0H]
     6 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/u256:0]
     7 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [migration/0]
     8 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_bh]
     9 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [rcu_sched]
    10 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [lru-add-drain]
    11 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [watchdog/0]
    12 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [watchdog/1]
    13 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [migration/1]
    14 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksoftirqd/1]
 10925 be/4 hpe         0.00 B/s    0.00 B/s  0.00 %  0.00 % gsd-xsettings [dconf worker]
    16 be/0 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kworker/1:0H]
    17 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [watchdog/2]
    18 rt/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [migration/2]
    19 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksoftirqd/2]


iostat : It report CPU statistics and IO statistics for devices and partitions. This command is used for monitoring system input/output device loading by observing the time devices are active in relation to their average transfer rates. The iostat command generates reports that can be used to change system configuration to better balance the input/output load between physical disks.

[root@localhost ~]# iostat
Linux 3.10.0-957.el7.x86_64 (localhost.localdomain)     Friday 29 March 2019    _x86_64_        (4 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.84    0.91    1.70    0.94    0.00   95.61

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda              16.88       426.35       198.58    1477954     688381
scd0              0.01         0.30         0.00       1050          0
dm-0             16.66       415.36       197.98    1439850     686291
dm-1              0.03         0.71         0.00       2460          0


To check iostats on /dev/sda:

[root@localhost ~]# iostat -p sda
Linux 3.10.0-957.el7.x86_64 (localhost.localdomain)     Friday 29 March 2019    _x86_64_        (4 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.79    0.85    2.01    1.01    0.00   95.34

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda              19.93       404.65       185.43    1584346     726011
sda1              0.52         8.15         0.53      31892       2090
sda2             19.41       396.08       184.89    1550782     723921


To check detailed stats:

[root@localhost ~]# iostat -x
Linux 3.10.0-957.el7.x86_64 (localhost.localdomain)     Friday 29 March 2019    _x86_64_        (4 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.72    0.57    1.58    0.74    0.00   96.39

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
sda               0.03     0.37   12.37    1.45   274.77   137.89    59.72     0.18   13.40    6.47   72.30   3.92   5.41
scd0              0.00     0.00    0.00    0.00     0.18     0.00    72.41     0.00    1.72    1.72    0.00   1.31   0.00
dm-0              0.00     0.00   12.02    1.82   268.25   137.54    58.63     0.24   17.31    6.65   87.75   3.90   5.39
dm-1              0.00     0.00    0.02    0.00     0.42     0.00    54.67     0.00    0.20    0.20    0.00   0.11   0.00


atop : atop is a Advanced System & Process Monitoring tool, which displays live status of the critical hardware resources (from a performance point of view) on system level, i.e. cpu, memory, disk and network in a arranged manner.

NOTE: To install this tool you will have to install EPEL repository and download it separately as it doesn't comes with RHEL or CentOS OS ISO's.

To install EPEL on CentOS and atop:

[root@localhost ~]# yum --enablerepo=extras install epel-release

[root@localhost ~]# yum install atop

To run atop :

[root@localhost ~]# atop




ioping : This tool generates various I/O patterns and lets you monitor I/O speed and latency in real time.

NOTE: To install this tool you will have to install EPEL repository and download it separately as it doesn't comes with RHEL or CentOS OS ISO's.

To install EPEL on CentOS and ioping:

[root@localhost ~]# yum --enablerepo=extras install epel-release

[root@localhost ~]# yum install ioping

To run ioping :

[root@localhost ~]# ioping -c 10 .
4 KiB <<< . (xfs /dev/dm-0): request=1 time=236.0 us (warmup)
4 KiB <<< . (xfs /dev/dm-0): request=2 time=496.4 us
4 KiB <<< . (xfs /dev/dm-0): request=3 time=592.1 us
4 KiB <<< . (xfs /dev/dm-0): request=4 time=630.0 us
4 KiB <<< . (xfs /dev/dm-0): request=5 time=384.8 us
4 KiB <<< . (xfs /dev/dm-0): request=6 time=696.7 us
4 KiB <<< . (xfs /dev/dm-0): request=7 time=1.22 ms (slow)
4 KiB <<< . (xfs /dev/dm-0): request=8 time=580.1 us
4 KiB <<< . (xfs /dev/dm-0): request=9 time=778.8 us
4 KiB <<< . (xfs /dev/dm-0): request=10 time=726.6 us

--- . (xfs /dev/dm-0) ioping statistics ---
9 requests completed in 6.11 ms, 36 KiB read, 1.47 k iops, 5.76 MiB/s
generated 10 requests in 9.01 s, 40 KiB, 1 iops, 4.44 KiB/s
min/avg/max/mdev = 384.8 us / 678.6 us / 1.22 ms / 222.8 us


[root@localhost ~]# ioping -c 10 -i 5 -s 16k .
16 KiB <<< . (xfs /dev/dm-0): request=1 time=374.2 us (warmup)
16 KiB <<< . (xfs /dev/dm-0): request=2 time=732.5 us
16 KiB <<< . (xfs /dev/dm-0): request=3 time=932.0 us
16 KiB <<< . (xfs /dev/dm-0): request=4 time=574.3 us
16 KiB <<< . (xfs /dev/dm-0): request=5 time=667.4 us
16 KiB <<< . (xfs /dev/dm-0): request=6 time=588.0 us
16 KiB <<< . (xfs /dev/dm-0): request=7 time=903.8 us
16 KiB <<< . (xfs /dev/dm-0): request=8 time=666.1 us
16 KiB <<< . (xfs /dev/dm-0): request=9 time=886.9 us
16 KiB <<< . (xfs /dev/dm-0): request=10 time=594.5 us

--- . (xfs /dev/dm-0) ioping statistics ---
9 requests completed in 6.55 ms, 144 KiB read, 1.37 k iops, 21.5 MiB/s
generated 10 requests in 45.0 s, 160 KiB, 0 iops, 3.55 KiB/s
min/avg/max/mdev = 574.3 us / 727.3 us / 932.0 us / 135.9 us

NOTE: 

-c Stop after count requests
-i Set time between requests to interval (Default 1s)
-s Request size (Default 4k)

How to add more swap space by adding new swap partition in RHEL or CentOS 7 ?

How to add more swap space by adding new swap partition in RHEL or CentOS 7


What is swap?


Swap space on a disk is used when the amount of physical RAM is full in the Linux system. When the system runs out of RAM, inactive pages are moved from the RAM to the swap space. Swap space can be either a dedicated swap partition or a swap file. So here we will see how you can add more swap space as a swap file.


In this article we will see how we can add more swap space using a new swap partition.


Steps :

Let's check how much swap is available on the system currently:

[root@localhost ~]# swapon --show

[root@localhost ~]# free -h

I have a new 10GB disk named "/dev/sdb" and i want to use it as a swap space.


[root@localhost ~]# fdisk -l
Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Step 1 : Create a disk partition on /dev/sdb and set partition type as swap:


[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xceaf232e.

Command (m for help): n  ==> CREATE NEW PARTITION
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p  ==> PRIMARY PARTITION
Partition number (1-4, default 1): ==> PRESS ENTER
First sector (2048-20971519, default 2048): ==> PRESS ENTER
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): ==> PRESS ENTER
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): p  ==> PRINT PARTITION TABLE

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xceaf232e

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20971519    10484736   83  Linux

Command (m for help): l  ==> LIST TYPE OF SUPPORTED FILESYSTEM

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT
1e  Hidden W95 FAT1 80  Old Minix

Command (m for help): t  ==> CHANGE PARTITION TYPE
Selected partition 1
Hex code (type L to list all codes): 82 ==> ENTER "82" FOR SWAP
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): p  ==> PRINT PARTITION TABLE

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xceaf232e

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20971519    10484736   82  Linux swap / Solaris

Command (m for help): w  ==> WRITE PARTITION TABLE CHANGES
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.


Step 2 : Run "partprobe" command to re-read the partition table after changes:

[root@localhost ~]# partprobe /dev/sdb


Step 3 : Format the new /dev/sdb1 swap disk:

[root@localhost ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 10484732 KiB
no label, UUID=8b375896-99fe-4b54-8c78-9ef41accef9b


Step 4 : Activate swap space on "/dev/sdb1" partition:

[root@localhost ~]# swapon /dev/sdb1


Step 5 : To make these new changes persistent across reboot add an fstab entry:

[root@localhost ~]# vi /etc/fstab

/dev/sdb1 swap swap defaults 0 0

save and exit.


Step 6 : Verify if new disk is added as a swap space:

[root@localhost ~]# swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition 3.9G   0B   -2
/dev/sdb1 partition  10G   0B   -3


[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           3.7G        421M        3.0G         14M        281M        3.0G
Swap:           13G          0B         13G


Removing swap partition gracefully:


Step 1 : Deactivate swap on /swapfile:

[root@localhost ~]# swapoff -v /dev/sdb1
swapoff /dev/sdb1


Step 2 : Remove swap entry from /etc/fstab file:


Step 3 : Delete "/dev/sdb1" partition or remove disk [OPTIONAL] :


Verify if swap space from "/dev/sdb1" partition is deactivated and removed:


[root@localhost ~]# swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition 3.9G   0B   -2


[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           3.7G        413M        3.0G         14M        281M        3.0G
Swap:          3.9G          0B        3.9G

How to add more swap space as a swapfile on RHEL or CentOS 7 ?

How to add more swap space as a swapfile on RHEL or CentOS 7

What is swap?

Swap space on a disk is used when the amount of physical RAM is full in the Linux system. When the system runs out of RAM, inactive pages are moved from the RAM to the swap space. Swap space can be either a dedicated swap partition or a swap file. So here we will see how you can add more swap space as a swap file.

In this article we will see how we can add more swap space using a swapfile.

Steps :

Let's check how much swap is available on the system currently:

[root@localhost ~]# swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition 3.9G   0B   -2


[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           3.7G        414M        1.1G         14M        2.2G        3.0G
Swap:          3.9G          0B        3.9G


Step 1 : Create a swapfile, which can be used as swap memory:

[root@localhost ~]# dd if=/dev/zero of=/swapfile bs=1G count=2
2+0 records in
2+0 records out
2147483648 bytes (2.1 GB) copied, 56.732 s, 37.9 MB/s


OR you can use "fallocate" command to create a new swapfile:


[root@localhost ~]# fallocate -l 2G /swapfile


[root@localhost ~]# ls -lkh /swapfile
-rw-r--r--. 1 root root 2.0G Apr 18 17:22 /swapfile



Step 2 : Ensure that only the root user have read and write permissions on the /swapfile:

[root@localhost ~]# chmod 600 /swapfile


Step 3 : Create swap on /swapfile :

[root@localhost ~]# mkswap /swapfile
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=97900239-4711-4cfd-90a2-e46be60a34e4


Step 4 : Activate swap on the file :

[root@localhost ~]# swapon /swapfile


Step 5 : To make this swap file persistent across reboot, add an fstab entry:

# vi /etc/fstab

/swapfile swap swap defaults 0 0

save and exit.


Verify if this new swap file is active:

[root@localhost ~]# swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition 3.9G   0B   -2
/swapfile file        2G   0B   -3


[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           3.7G        418M        1.1G         14M        2.2G        3.0G
Swap:          5.9G          0B        5.9G

The new swapfile is now added as a swap memory.

Removing swapfile gracefully:


Step 1 : Deactivate swap on /swapfile:

[root@localhost ~]# swapoff -v /swapfile
swapoff /swapfile

Step 2 : Remove swap entry from /etc/fstab file:


Step 3 : Delete /swapfile :

[root@localhost ~]# rm -rf /swapfile


Verify if the /swapfile is deactivated and removed:

[root@localhost ~]# swapon --show
NAME      TYPE      SIZE USED PRIO

/dev/dm-1 partition 3.9G   0B   -2


[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           3.7G        415M        1.1G         14M        2.2G        3.0G
Swap:          3.9G          0B        3.9G


Monitoring Memory, CPU and swap on RHEL or CentOS 7

top : The top provides a dynamic real-time view of running processes, CPU, Memory and SWAP utilization on a system. It can display system summary information as well as a list of processes or threads currently being managed by the Linux kernel.

[root@localhost ~]# top


In case as a root you want to check it for some particular user then :

[root@localhost ~]# top -u hpe



pmap : This command report memory map of a process or processes.

[root@localhost ~]# pmap -d 22745



vmstat : This command reports information about processes, memory, paging, block IO, traps, disks and cpu activity.

[root@localhost ~]# vmstat -w

[root@localhost ~]# vmstat -as

[root@localhost ~]# vmstat -dw


[root@localhost ~]# vmstat -D

            5 disks
            2 partitions
        48470 total reads
          104 merged reads
      9879363 read sectors
      6320494 milli reading
        57453 writes
        39762 merged writes
     46514707 written sectors
     61995147 milli writing
            0 inprogress IO
         1162 milli spent IO


mpstat : The  mpstat  command  writes  to  standard output activities for each available processor, processor 0 being the first one.  Global average activities among all processors are also reported. The mpstat command can be used both on SMP and UP machines, but in the latter, only global average activities will be printed. If no activity has been selected, then the default report is the CPU utilization report.

[root@localhost ~]# mpstat -A



free : free displays the total amount of free and used physical and swap memory in the system, as well as the buffers and caches used by the kernel. The information is gathered by parsing /proc/meminfo.

[root@localhost ~]#  free -mlt




swapon : It displays name of the swap device along with total/used size and it's priority:


[root@localhost ~]# swapon --show
NAME      TYPE      SIZE  USED PRIO
/dev/dm-1 partition 3.9G 70.5M   -2


ps : This command gives you the list of running process on the system.

[root@localhost ~]# ps -eaf
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 13:14 ?        00:00:08 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root          2      0  0 13:14 ?        00:00:00 [kthreadd]
root          3      2  0 13:14 ?        00:00:11 [ksoftirqd/0]
root          5      2  0 13:14 ?        00:00:00 [kworker/0:0H]
root          7      2  0 13:14 ?        00:00:07 [migration/0]
root          8      2  0 13:14 ?        00:00:00 [rcu_bh]
root          9      2  0 13:14 ?        00:00:08 [rcu_sched]
root         10      2  0 13:14 ?        00:00:00 [lru-add-drain]
root         11      2  0 13:14 ?        00:00:03 [watchdog/0]
root         12      2  0 13:14 ?        00:00:03 [watchdog/1]
root         13      2  0 13:14 ?        00:00:14 [migration/1]
root         14      2  0 13:14 ?        00:00:09 [ksoftirqd/1]
root         16      2  0 13:14 ?        00:00:00 [kworker/1:0H]
root         17      2  0 13:14 ?        00:00:04 [watchdog/2]
root         18      2  0 13:14 ?        00:00:19 [migration/2]
root         19      2  0 13:14 ?        00:00:03 [ksoftirqd/2]
root         20      2  0 13:14 ?        00:00:08 [kworker/2:0]
root         21      2  0 13:14 ?        00:00:00 [kworker/2:0H]
root         22      2  0 13:14 ?        00:00:06 [watchdog/3]
root         23      2  0 13:14 ?        00:00:24 [migration/3]
root         24      2  0 13:14 ?        00:00:27 [ksoftirqd/3]
root         26      2  0 13:14 ?        00:00:00 [kworker/3:0H]


Use of "--forest" switch in the ps command displays parent and chile process tree.

[root@localhost ~]#  ps -e -o pid,args --forest





pstree : It shows running processes as a tree. The tree is rooted at either pid or init if pid is omitted. If a user name is specified, all process trees rooted at processes owned by that user are shown.


[root@localhost ~]# pstree



[root@localhost ~]# pstree -u hpe
bash───vim

bash───vim



lsof : This displays the lists of files currently opened on your system.

[root@localhost ~]# lsof | more



If you want to check list of opened files for some user:

[root@localhost ~]# lsof -u hpe



strace : Strace is used for debugging and troubleshooting the execution of an executable. It displays the system calls used by the process, and the signals received by the process. It also provides execution of a binary from start to end.

[root@localhost ~]# strace ls
execve("/usr/bin/ls", ["ls"], [/* 31 vars */]) = 0
brk(NULL)                               = 0x1b95000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fe9a053f000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=122100, ...}) = 0
mmap(NULL, 122100, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fe9a0521000
close(3)                                = 0
open("/lib64/libselinux.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320i\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=155784, ...}) = 0
mmap(NULL, 2255184, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fe9a00f8000
mprotect(0x7fe9a011c000, 2093056, PROT_NONE) = 0
mmap(0x7fe9a031b000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x23000) = 0x7fe9a031b000
mmap(0x7fe9a031d000, 6480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fe9a031d000
close(3)                                = 0
open("/lib64/libcap.so.2", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0 \26\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=20032, ...}) = 0
.
.
.