Thursday 30 April 2020

Install SQLite3 from Source on Linux

Install SQLite3 from Source

$ wget https://www.sqlite.org/src/tarball/sqlite.tar.gz

$ tar xzf sqlite.tar.gz     #  Unpack the source tree into "sqlite"
$ mkdir bld                   #  Build will occur in a sibling directory
$ cd bld                        #  Change to the build directory
$ ../sqlite/configure     #  Run the configure script
$ make                         #  Run the makefile.
$ make sqlite3.c           #  Build the "amalgamation" source file
$ make test                  #  Run some tests (requires Tcl)

$ whereis sqlite3
sqlite3: /usr/bin/sqlite3 /usr/include/sqlite3.h /usr/share/man/man1/sqlite3.1.gz

$ sqlite3 --version
3.7.17 2013-05-20 00:56:22 118a3b35693b134d56ebd780123b7fd6f1497668

$ sqlite3
   SQLite version 3.7.17 2013-05-20 00:56:22
   Enter ".help" for instructions
   Enter SQL statements terminated with a ";"
   sqlite> .help
   [...]
   sqlite> .show
   [...]
   sqlite> .database
   [...] 
   sqlite> .table
   [...]
   sqlite> .quit

Ref :- sqlite.org sqlite.org/cli.html

Monday 27 April 2020

Install Python 3.8 using tarball in Linux

Install Python 3.8.2

$ cat /etc/redhat-release
  CentOS Linux release 7.7.1908 (Core)

$ python --version
  Python 2.7.5
$ which python
  /usr/bin/python

$ yum update
$ yum install yum-utils
$ yum-builddep python3

$ wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz
$ tar -xvzf Python-3.8.2.tgz
$ cd Python-3.8.2

$ ./configure --enable-optimizations
$ make
$ make test
$ make install

$ python3.8 --version
  Python 3.8.2
$ which python3.8
  /usr/local/bin/python3.8

Since the python2.7 is a default system wide python interpreter and need explicitly set to new version. To change python version only for a single user edit ~/.bashrc.

$ vi ~/.bashrc
  alias python='/usr/local/bin/python3.8'
$ cd /root
$ . .bashrc #apply changes

$ python
  Python 3.8.2 (default, Apr 27 2020, 08:41:59)
  [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  >>>

Ref:- linuxconfig.org

Saturday 11 April 2020

Debug initramfs images in Linux

Debug initramfs images

$ mkdir /tmp/testdir

$ ls /boot/
   config-3.10.0-957.el7.x86_64
   efi
   grub
   grub2
   initramfs-0-rescue-e45307abc30a407dbbd42a0632810083.img
   initramfs-3.10.0-957.el7.x86_64.img
   initramfs-3.10.0-957.el7.x86_64kdump.img
   symvers-3.10.0-957.el7.x86_64.gz
   System.map-3.10.0-957.el7.x86_64
   vmlinuz-0-rescue-e45307abc30a407dbbd42a0632810083
   vmlinuz-3.10.0-957.el7.x86_64

$ cp -r /boot/initramfs-3.10.0-957.el7.x86_64.img /tmp/testdir/i

$ ls /tmp/testdir/i
   i

$ cd /tmp/testdir/

$ file i
   i: ASCII cpio archive (SVR4 with no CRC)

$ cpio -i --no-absolute-filenames <i
   86917 blocks

$ ls /tmp/testdir/
   bin  etc init   lib64  root   sbinb          sys          tmp   var
   dev  i    lib    proc   run    shutdown   systoot    usr

$ ls -l init  
   lrwxrwxrwx. 1 root root 11 Apr   20 11:12 init -> usr/lib/systemd/systemd

Make a custom GRUB entry in Linux

How to make a custom GRUB entry

Copy menuentry for CentOS Linux 3.10.0-957.el7.x86_64 to 40_custom edit as,
$ vi /etc/grub.d/40_custom
#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
menuentry 'Custom Linux Boot Entry' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-957.el7.x86_64-advanced-4f940ed7-acd6-4631-bf35-ee2f43b49c9f' {
        load_video
        set gfxpayload=keep
        insmod gzio
        insmod part_msdos
        insmod xfs
        set root='hd0,msdos1'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  5971f3cb-eb26-4c88-af0a-b6c96e12965c
        else
          search --no-floppy --fs-uuid --set=root 5971f3cb-eb26-4c88-af0a-b6c96e12965c
        fi
        linux16 /vmlinuz-3.10.0-957.el7.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet initcall_debug
        initrd16 /initramfs-3.10.0-957.el7.x86_64.img
}

                                                OR

menuentry 'Custom Linux Boot' {
        linux16 /vmlinuz-3.10.0-957.el7.x86_64 root=/dev/mapper/centos-root ro rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet initcall_debug
        initrd16 /initramfs-3.10.0-957.el7.x86_64.img
}

$ grub2-mkconfig -o /etc/grub2.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-957.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-957.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-e45307abc30a407dbbd42a0632810083
Found initrd image: /boot/initramfs-0-rescue-e45307abc30a407dbbd42a0632810083.img
done

$ dmesg | grep initcall #check the current kernel have any init call
If no output then reboot and check.

$ dmesg | grep initcall | head
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-957.el7.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet initcall_debug
[    0.000000] Kernel command line: BOOT_IMAGE=/vmlinuz-3.10.0-957.el7.x86_64 root=/dev/mapper/centos-root ro crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet initcall_debug
[    0.131139] initcall init_hw_perf_events+0x0/0x5f1 returned 0 after 0 usecs
[    0.131167] initcall set_real_mode_permissions+0x0/0x102 returned 0 after 0 usecs
[    0.131173] initcall trace_init_flags_sys_exit+0x0/0xf returned 0 after 0 usecs
[    0.131178] initcall trace_init_flags_sys_enter+0x0/0xf returned 0 after 0 usecs
[    0.131184] initcall register_trigger_all_cpu_backtrace+0x0/0x16 returned 0 after 0 usecs
[    0.131190] initcall kvm_spinlock_init_jump+0x0/0x28 returned 0 after 0 usecs
[    0.131197] initcall early_efi_map_fb+0x0/0x32 returned 0 after 0 usecs
[    0.131228] initcall spawn_ksoftirqd+0x0/0x26 returned 0 after 0 usecs

How to Interrupt GRUB and Override init process in Linux

Interrupt GRUB and Override init

This can be achieved by making temporary changes to a kernel menu entry
requirements: cent OS 7 installed in a virtual machine.

Booting with rdinit=/bin/sh will start with a shell in the initramfs.
while booting with init=/bin/bash will complete the initramfs and then start with a shell on the disk.

Scenario 1:  Change kernel parameters only during a single boot process. reboot vm on the GRUB 2 boot screen press 'e' key for edit regular kernel entry
at the end of last to previous line, add rdinit=/bin/sh



Press Ctrl+x, it will boot as shell prompt with logged in.
The init is restarted process as /bin/sh.
There will be a limited file system loaded in RAM











Scenario 2: reboot vm, on the GRUB 2 boot screen press 'e' key for edit regular kernel entry. At the end of last to previous line, add init=/bin/bash.



Press Ctrl+x to boot, It will boot as shell prompt with logged in.
The init is restarted process as /bin/bash, with no ordinary service like GUI or init processes.



Thursday 9 April 2020

Bash script to monitor cpu usage in Linux

Script to monitor cpu usage 

$ vi cpu_usage.sh

#!/bin/bash

CPU_USAGE=$(top -b -n2 -p 1 | fgrep "Cpu(s)" | tail -l | awk -F'id,' -v prefix="$prefix" '{ split($1, vs, ","); v=vs[length(vs)]; sub("%", "", v); printf "%s%.1f%%\n", prefix, 100 -v}' )

DATE=$(date "+%d %B %Y %H:%M")
CPU_USAGE="$DATE CPU: $CPU_USAGE"
echo $CPU_USAGE >> /var/log/scripts/cpu_usage.log

$ ./cpu_usage.sh

$ cat /var/log/scripts/cpu_usage.log
09 April 2020 08:36 CPU: 3.2% 0.2%
09 April 2020 15:00 CPU: 9.7% 0.0%
09 April 2020 15:34 CPU: 0.0% 0.2%

Bash script to guess a random one-digit number

Script to guess a random one-digit number

$ vi ch4_solution.sh game

#!/bin/bash
rand=$RANDOM
secret=${rand:0:1}

function game {
        read -p "Guess a random one-digit number! " guess
        while [[ $guess != $secret ]]; do
                read -p "Nope, try again! " guess
        done
        echo "Good job, $secret is it! You're great at guessing!"
}

function generate {
        echo "A random number is: $rand"
        echo -e "Hint: type \033[1m$0 game\033[0m for a fun diversion!"
}

if [[ $1 =~ game|Game|GAME ]]; then
        game
else
        generate
fi

$ ./ch4_solution.sh game
Guess a random one-digit number! 1
Nope, try again! 4
Nope, try again! 5
Good job, 5 is it! You're great at guessing!

Tuesday 7 April 2020

Sample bash script to generate system report

Generate system report

vim /usr/local/src/test_my_scripts/system_report.sh
#!/bin/bash
# System Report
greentext="\033[32m"
bold="\033[1m"
normal="\033[0m"
freespace=$(df -h / | grep -E "\/$" | awk '{print $4}')
logdate=$(date +"%Y%m%d")
path=/var/log/script
logfile="$path$logdate"_report.log

echo -e $bold"Quick system report for "$greentext"$HOSTNAME"$normal
printf "\tSystem type:\t%s\n" $MACHTYPE
printf "\tBash Version:\t%s\n" $BASH_VERSION
printf "\tFree Space:\t%s\n" $freespace
printf "\tFiles in dir:\t%s\n" $(ls | wc -l)
printf "\tGenerated on:\t%s\n" $(date +"%m/%d/%y")
echo -e $greentext"A summary of this info has been saved to $logfile"$normal

cat <<- EOF > $logfile
        This report was generated using Bash script.
EOF

printf "\tGenerated on:\t%s\n" $(date +"%m/%d/%y") >> $logfile
printf "\tFree Space:\t%s\n" $freespace >> $logfile
printf "\tFiles in dir:\t%s\n" $(ls | wc -l) >> $logfile

sh /usr/local/src/test_my_scripts/system_report.sh
Quick system report for localhost.localdomain
        System type:    x86_64-redhat-linux-gnu
        Bash Version:   4.2.46(2)-release
        Free Space:     15G
        Files in dir:   6
        Generated on:   04/07/20
A summary of this info has been saved to /var/log/script20200407_report.log

cat /var/log/script20200407_report.log
This report was generated using Bash script.
        Generated on:   04/07/20
        Free Space:     15G
        Files in dir:   6

Monday 6 April 2020

40 Examples of grep command in Linux

Searching and mining text inside a file with grep

man grep
grep "string" demo_file      #string search
grep "string" demo_file test_file.txt
grep "string" *.*

grep “^hello” file #match all lines start with ‘hello’
grep “done$” file #match all lines end with 'done'
grep “[^aeiou]” file #Match all lines not contain a vowel
grep -n "string" demo_file      # show line numbers with match

egrep is the same as grep -E
fgrep is the same as grep -F
rgrep is the same as grep -r
egrep IP /etc/hosts
egrep 'IP1|IP2' /etc/hosts

seq 10 | grep 5 -A 3        #print 5-8
seq 10 | grep 5 -B 3        #print 2-5
seq 10 | grep 5 -C 3        #print 2-8
echo -e "a\nb\nc\na\nb\nc" | grep a -A 1

grep word filename --color=auto #add color
grep --color "string" demo_file
grep --color -n "string" demo_file
yum search php | grep gd
grep -E "[a-z]+" demo_file #extented
grep "[a-z]+" demo_file

grep -v "string" demo_file     #inverted result
grep -c "string" demo_file #count the occurrence
echo -e "1 2 3 4\nhellow\n5 6" | grep -c "[0-9]"
grep -r  --exclude-dir=log "string" *
grep -c "[0-9]" demo_file

grep -o "[0-9]" demo_file | wc -l     #count no of times
echo -e "1 2 3 4\nhellow\n5 6" | grep -o "[0-9]" | wc -l
cat filename | grep -b -o "word"
grep -l "string" sample.txt sample1.txt     #with-match
grep -L "string" sample.txt sample1.txt     #without-match

grep "string" . -R -n       #recursive search
grep -i "string" demo_file #case insensitive
grep -i 'Model' /proc/cpuinfo
grep -e "count" -e "image" -o rename.sh     #multiple patterns
grep "main()" . -r --include *.{c,cpp}
grep "main()" . -r --exclude "README"
grep "string" file* -lZ | xargs -0 rm -rf

grep -o "is.*line" demo_file      #matched string
grep -v -E '^\#|^$' demo_file #skips line beginning "#" or empty
grep -v -f file1 file2 > file.out    #diff of 2nd with 1st
find . -type f -exec grep -il 'string' {} \; #find in directory tree

grep authentication failure /var/log/secure-20200329
export GREP_OPTIONS='--color=auto' #to enable all the time
grep -i break-in autth.log | awk {'print $12'}
ping -c 1 linuxmissive.com | grep 'bytes from'
ping -c 1 linuxmissive.com | grep 'bytes from' | cut -d = -f 4

# find all files named "*.java,v" containing both 'prevayl' and 'jtable'
grep -li "jtable" $(find . -name "*.java,v" -exec grep -li "prevayl" {} \;)
egrep 'sting1|sting2|sting3|sting4' file.txt # all lines matching multiple patterns
locate -i calendar | grep Users | egrep -vi 'twiki|gif|shtml|drupal-7|java|PNG'
   
ps auxwww | grep httpd               # all processes containing 'httpd'
ps auxwww | grep -i java             # all processes containing 'java', ignoring case
ls -al | grep '^d'                        # list all dirs in the current dir
#printing lines before and after of matching
grep --context=6 "string" demo_file      #6 lines before and after

Ref:- thegeekstuff.com

Saturday 4 April 2020

Install MySQL on CentOS 7

Install and Configure MySQL

Install MySQL

wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
md5sum mysql57-community-release-el7-9.noarch.rpm
rpm -ivh mysql57-community-release-el7-9.noarch.rpm

Start MySQL

systemctl start mysqld
systemctl status mysqld #Active: active (running)
grep 'temporary password' /var/log/mysqld.log #A temporary password is generated

Configure MySQL

mysql_secure_installation #set a new password
mysqladmin -u root -p version

Packages for MySQL:- dev.mysql.com

Ref:- digitalocean.com

Friday 3 April 2020

Bash script to List all the machines alive on a network

Ping all the machines alive

vi ping.sh
#!/bin/bash
#ping ping.sh

for ip in 192.168.0.{1..255} ;
do
 (
 ping $ip -c 2 &> /dev/null;

 if [ $? -eq 0 ];
 then
  echo $ip is alive
 fi
 )&
done
wait

./ping.sh
192.168.0.1 is alive
192.168.0.3 is alive
192.168.0.16 is alive
192.168.0.32 is alive
192.168.0.201 is alive


Linux script to monitor top ten CPU consuming process

CPU monitoring bash script for an hour

vi cpu_usage.sh
#!/bin/bash
#calculate cpu usage by processes for 1 hour

SECS=3600
UNIT_TIME=60

STEPS=$(( $SECS / $UNIT_TIME ))

echo Watching CPU usage... ;

for ((i=0;i<STEPS;i++))
do
 ps -eocomm,pcpu | tail -n +2 >> /tmp/cpu_usage.$$
 sleep $UNIT_TIME
done

echo
echo CPU eaters :

cat /tmp/cpu_usage.$$ | \
awk '
{ process[$1]+=$2; }
END{
 for(i in process)
 { printf("%-20s%d\n",i, process[i]); }
 }' | sort -nrk 2 | head

rm -rf /tmp/cpu_usage.$$

./cpu_usage.sh
Watching CPU usage...

CPU eaters :
xfs-reclaim/sda      0
xfs-reclaim/dm-     0
xfs_mru_cache       0
xfs-log/sda1           0
xfs-log/dm-0          0
xfs-eofblocks/s       0
xfs-eofblocks/d      0
xfs-data/sda1         0
xfs-data/dm-0        0
xfs-conv/sda1         0