Thursday, 9 April 2020

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

Monday, 30 March 2020

Bash script to remove duplicate files

Identify the duplicate files and remove.

vi remove_duplicate.sh
#!/bin/bash
ls -ls --time-style=long-iso | awk 'BEGIN {
 getline; getline;
 name1=$9; size=$5
}
{
 name2=$9;
 if (size==$5)
 {
  "md5sum "name1 | getline; csum1=$1;
  "md5sum "name2 | getline; csum2=$1;
  if ( csum1==csum2 )
        {
         print name1; print name2;
        }
 };

 size=$5; name1=name2;
}' | sort -u > duplicate_files

cat duplicate_files | xargs -I {} md5sum {} | sort | uniq -w 32 | awk '{ print "^"$2"$" }' | sort -u > duplicate_sample

if [ -s duplicate_sample ]
 then
  echo Removing...
  comm duplicate_files duplicate_sample -2 -3 | tee /dev/stderr | xargs rm -rf
  echo Removed duplicate files successfully.
 else
 echo No duplicate files found.
fi

Friday, 27 March 2020

20 Examples of Linux xargs Command


 xargs Command usage

man xargs
xargs --version

ls -l /usr/local/src/ | xargs                #single line output
cat example.txt | xargs -n 7
xargs -a example.txt
echo 'one two three' | xargs mkdir #creating three dirs
echo 'one two three' | xargs -p touch #creating three files

printf "one  three  two" | xargs -i touch {}.txt #add extention
echo "Text1XText2XText3XText4" | xargs -d X -n 2
printf %s\\n {1..30} | xargs -n 5 -P 8                     #executing commands in parallel
cat args.txt | xargs -n 2 '/usr/local/src/cecho.sh'

cat args.txt | xargs -I {} ./cecho.sh -p {} -l
echo "file1 file2 file3" | xargs -t -I % sh -c '{ touch %; ls -l %; }' #multiple commands
find ./*.txt -print0 | xargs -0 -n 1 -P 3 bzip2    #bzip2 processes in parallel
ls "b.txt" | xargs -n 1 sed -i "s/color/colour/g" #replace all occurrences

cut -d: -f1 < /etc/passwd | sort | xargs #compact list of all Linux users
ls *digital* | xargs wc                             #number of lines/words/characters in each file
echo dir1/ dir2/ dir3/ | xargs -n 1 cp -v dir/test.txt #copy a file to multiple dirs at once
find . -type f -name “*.java” | xargs tar cvf myfile.tar
find / -type f -name "*.sh" -print0 | xargs -0 wc -l #cout no of lines

find . -type f -name "*.txt" -print0 | xargs -0 rm -f                 #remove files
find . -type f -name "b.txt" -print0 | xargs -0 -p -n 1  rm -rf   #prompt before execution
find /tmp -mtime +7 | xargs rm

find . -type f -not -name '*.txt' -print0 | xargs -0 -I {} rm -v {} #remove except extension
find . -type d -name "abb" -print0 | xargs -0 rm -v -rf "{}"        #remove directory
echo 'one two three' | xargs -t rm -rf                               #remove three files/dirs

Ref:- linuxtechi.com shapeshed.com tecmint.com


Thursday, 26 March 2020

60 Examples of Linux find command

Using find command in Linux

Find becomes extremely useful when combined with other commands,

man find
info find
find . -type d -print  #list directories
find . -type f -print  #regular files
find . -type l -print  #symbolic files

find . -name demo.sh
find . -iname demo.sh #case-insensitive search
find /home -iname demo.sh      #both capital and small letters
find . -exec ls -ld {} \;         #List out the found files
find . \( -name "*.mp4" -o -name "*.txt" \) -print
find /home/test -path "*/public/*" -print
find . -iregex ".*\(\.py\|\.sh\)$"
find . -type f -not -name "*.sh" #invert match
find . ! -name "*.txt" -print

find /tmp -type f -empty         #find Empty files
find /tmp -type d -empty find   #Empty directories
find /tmp -type f -name ".*"  #find hidden files
find ./ -type f -name "ab.txt" -exec grep 'This' {} \; #print lines which have ‘This’
find ./test -name 'abc*' ! -name '*.php' #combine multiple search criteria
find -name '*.php' -o -name '*.txt'
find ./test ./dir2 -type f -name "abc*"       #search multiple directories
find . -maxdepth 1 -name "f*" -print
find . -mindepth 2 -name "f*" -print

find . -type f -atime -7 -print    #accessed in last 7 days
find . -type f -atime 7 -print     #accessed in last 7th day
find . -type f -atime +7 -print    #accessed older that 7 days
find / -mtime +7 –mtime -10     #modified 7 days back and less than 10 days
find / -cmin -60  #files which are changed in last 1 hour
find / -mmin -60 #the files which are modified in last 1 hour
find / -amin -60 #files which are accessed in last 1 hour
find . -type f -amin +7 -print                  #accessed in last 7 mins
find . -type f -newer cal_seq.sh -print    #newer than a file

find . -type f -size +2k                      #file size
find / -size +50M -size -100M             #greater than 50MB and less than 100MB
find . -type f -exec ls -s {} \; | sort -n -r | head -5  #find largest
find . -type f -exec ls -s {} \; | sort -n | head -5     #smallest file
find . -type f -name "a.txt" -exec cp {} test/ \;     #find and move
find / -type f -size +100M -exec rm -f {} \;               #find all 100MB files and delete
find / -type f -name *.mp3 -size +10M -exec rm {} \;#find all .mp3 & delete more than 10MB
find / -name name.txt -exec rm -i {} \; #delete a file with confirmation
find . -type f -name *.swp -delete                 #delete

find / -perm 1551 #sticky bit files
find / -perm /u=s #SUID
find / -perm /g=s #SGID
find / -perm /u=r #read only
find / -perm /a=x # executable

find /etc -maxdepth 1 -perm /u=r
find . -type f -perm 644 -print    #permission
find / -type f ! -perm 777 #with out permission
find . -type f -name "*.php" ! -perm 644 -print #don't have permission levels
find / -type f -perm 0777 -print -exec chmod 644 {} \;  #change permission
find . -type f -user root -print #find based on user
find /data -owner smith #find all files owned by smith
find /home -group developer #find based on group
find . -type f -user root -exec chown test {} \; #find and change ownership

find ./ -type f -exec sed -i 's/find/replace/g' {} \; #find and replace
find / -name \*.txt -exec sed -i "s/one/five/g" {} \;
find . -type f -name "*.c" -exec cat {} \;>all_c_files.txt #all .c to .txt
find . -type f -mtime +10 -name "*.txt" -exec cp {} OLD \; #cp 10 days old OLD dir
find . -type f -name "*.txt" -exec printf "Text file: %s\n" {} \;  #list .txt

find / -type f -name "*.sh" -print0 | xargs -0 wc -l           #cout no of lines
find . -type f -name "*.txt" -print0 | xargs -0 rm -f          # remove files
find . -type f -name “*.java” | xargs tar cvf myfile.tar    #find and tar files
find [paths] [expression] [actions] 2>/dev/null          #finding error messages
find ~/documents -type f -name '*.txt' \ -exec grep -s DOGS {} \; -print   #grep

#remove duplicate files
find . ! -empty -type f -exec md5sum {} + | sort | uniq -w32 -dD
find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate

Monday, 23 March 2020

tput in Linux

tput usage

$ man tput
$ info tput

$ tput longname #full name of the current terminal type
xterm terminal emulator (X Window System)

$ tput -T screen longname
VT 100/ANSI X3.64 virtual terminal

Foreground & background color commands

tput setab [1-7] # Set the background color using ANSI escape
tput setaf [1-7] # Set the foreground color using ANSI escape

Colors are as follows:

No   Color        #define                R G B

 0    black       COLOR_BLACK        0,0,0
 1    red           COLOR_RED          1,0,0
 2    green       COLOR_GREEN       0,1,0
 3    yellow      COLOR_YELLOW     1,1,0
 4    blue         COLOR_BLUE          0,0,1
 5    magenta   COLOR_MAGENTA   1,0,1
 6    cyan        COLOR_CYAN           0,1,1
 7    white       COLOR_WHITE         1,1,1

















Text mode commands

tput bold    # Select bold mode
tput dim     # Select dim (half-bright) mode
tput smul    # Enable underline mode
tput rmul    # Disable underline mode
tput rev     # Turn on reverse video mode
tput smso    # Enter standout (bold) mode
tput rmso    # Exit standout mode

Cursor movement commands

tput cup Y X # Move cursor to screen postion X,Y (top left is 0,0)
tput cuf N   # Move N characters forward (right)
tput cub N   # Move N characters back (left)
tput cuu N   # Move N lines up
tput ll      # Move to last line, first column (if no cup)
tput sc      # Save the cursor position
tput rc      # Restore the cursor position
tput lines   # Output the number of lines of the terminal
tput cols    # Output the number of columns of the terminal

Clear and insert commands

tput ech N   # Erase N characters
tput clear   # Clear screen and move the cursor to 0,0
tput el 1    # Clear to beginning of line
tput el      # Clear to end of line
tput ed      # Clear to end of screen
tput ich N   # Insert N characters (moves rest of line forward!)
tput il N    # Insert N lines

Reset and play bell commands

tput sgr0    # Reset text format to the terminal's default
tput bel     # Play a bell

Error Messages

tput displays various error messages if problems occur. In addition, it exits with one of the following status values:

0 Normal status; the given capability is present.
1 The given Boolean or string capability is not present.
2 Usage error; tput was given invalid arguments.
3 The terminal type given is unknown, or the termcap database can not be read.
4 The given capability is unknown.


Ref:- stackoverflow.com gnu.org linuxcommand.org

Linux date command usage

Date command usages in Linux

The date command displays or sets the system date and time. It is most commonly used to print the date and time in different formats and calculate future and past dates.

$ man date
$ info date

$ date
Mon Mar 23 08:08:21 EDT 2020

$ date +"Year: %Y, Month: %m, Day: %d"
Year: 2020, Month: 03, Day: 23

$ date "+DATE: %D%nTIME: %T"
DATE: 03/23/20
TIME: 08:08:41

$ date -d "2010-02-07 12:10:53" #date string option
Sun Feb  7 12:10:53 EST 2010

$ date -d '16 Dec 1974' +'%A, %d %B %Y'
Monday, 16 December 1974

$ date -d "last week"             #Display past date
Mon Mar 16 08:10:54 EDT 2020

$ date -d 'TZ="Australia/Sydney" 06:30 next Monday'
Sun Mar 29 15:30:00 EDT 2020

$ TZ=GMT date
Mon Mar 23 12:15:31 GMT 2020

$ TZ='Australia/Melbourne' date #current time of some other location
Mon Mar 23 23:11:29 AEDT 2020 (ls /usr/share/zoneinfo)

$ date -u               #Display universal time
Mon Mar 23 16:57:45 UTC 2020

$ date --iso-8601=seconds #ISO 8601 format
2020-03-23T13:06:30-0400

$ date --rfc-3339=seconds #RFC 3339 format
2020-03-23 13:08:54-04:00

$ date +%s                #Epoch converter (seconds since 00:00:00, Jan 1, 1970)
1584965527

$ date -d @1234567890         #Convert epoch to a date
Fri Feb 13 18:31:30 EST 2009

$ date -d "2020-01-01" +"%s" #calculate the seconds from epoch to provided date/time
1577854800

$ date_now=$(date "+%F-%H-%M-%S") #use with shell scripts
$ echo $date_now
2020-03-23-08-13-50

$ date -r /etc/hosts                 #Last Modification Time of a File
Wed Jan 23 17:30:26 EST 2019

$ date --set="20200801 12:30" #Set the System Time and Date

$ date +"Week number: %V Year: %y"
Week number: 13 Year: 20

$ touch ~/Desktop/`date +%F`.txt #create a file with the date and time

$ tar cfz /backup-`date +%F`.tar.gz /home/ #create archives with the date and time

$ mysqldump  db_name > db_name-$(date +%Y%m%d).sql #mysqldump file with date


Sunday, 22 March 2020

Color code in Linux shell

Color modes, colors for bash prompt, tput and ANSI color 

Setting colors in terminal session is only temporary and relatively unconditional.
Modern systems usually default to at least xterm-256color.

$ echo $TERM
xterm (old)

Check current settings

$ echo $LS_COLORS
rs=0:di=01;34:ln=01;36:mh=00:pi=40;
33:so=01;35:do=01;35:bd=40;33;01:cd=40;
[...]

$ dircolors --print-database
[...].jpg 01;35
.jpeg 01;35
.gif 01;35[...]

$ dircolors --bourne-shell
LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;
33:so=01;35:do=01;35:bd=40;33;01:cd=40;
[...]

The Color modes:

1. Color-mode

It modifies the style of color NOT text. For example make the color bright or darker.

0; reset
1; lighter than normal
2; darker than normal
This mode is not supported widely. It is fully support on Gnome-Terminal.

2. Text-mode

This mode is for modifying the style of text NOT color.

3; italic
4; underline
5; blinking (slow)
6; blinking (fast)
7; reverse
8; hide
9; cross-out
and are almost supported.
For example KDE-Konsole supports 5; but Gnome-Terminal does not and Gnome supports 8; but KDE does not.

3. Foreground mode
This mode is for colorizing the foreground.

4. Background mode
This mode is for colorizing the background.

Colors to current BASH prompt (PS1)

To add colors to the shell prompt use the following export command syntax:
'\e[x;ym $PS1 \e[m'
Where,

\e[ : Start color scheme.
x;y : Color pair to use (x;y)
$PS1 : Your shell prompt variable.
\e[m : Stop color scheme.

Examples using export and tput,
$ export PS1="\e[0;31m[\u@\h \W]\$ \e[m "
$ export PS1="\[$(tput setaf 2)\]\u@\h:\w $ \[$(tput sgr0)\]"

tput

Foreground & background colour commands
tput setab [1-7] # Set the background colour using ANSI escape
tput setaf [1-7] # Set the foreground colour using ANSI escape

Colours are as follows:

No  Colour      #define              R G B

0    black       COLOR_BLACK       0,0,0
1    red           COLOR_RED          1,0,0
2    green       COLOR_GREEN       0,1,0
3    yellow      COLOR_YELLOW    1,1,0
4    blue          COLOR_BLUE        0,0,1
5    magenta   COLOR_MAGENTA  1,0,1
6    cyan         COLOR_CYAN        0,1,1
7    white        COLOR_WHITE      1,1,1

Text mode commands
tput bold    # Select bold mode
tput dim     # Select dim (half-bright) mode
tput smul    # Enable underline mode
tput rmul    # Disable underline mode
tput rev     # Turn on reverse video mode
tput smso    # Enter standout (bold) mode
tput rmso    # Exit standout mode

echo "$(tput setaf 1)Red text $(tput setab 7)and white background$(tput sgr 0)"

Color summary


ANSI Color variables












Code  Effect

  0       reset
  1       lighter than normal
  2       darker than normal
  3       italic
  4       underline
  5       blinking (slow)
  6       blinking (fast)
  7       reverse
  8       hide
  9       cross-out


































# Reset
Color_Off='\033[0m'       # Text Reset

# Regular Colors
Black='\033[0;30m'        # Black
Red='\033[0;31m'          # Red
Green='\033[0;32m'        # Green
Yellow='\033[0;33m'       # Yellow
Blue='\033[0;34m'         # Blue
Purple='\033[0;35m'       # Purple
Cyan='\033[0;36m'         # Cyan
White='\033[0;37m'        # White

# Bold
BBlack='\033[1;30m'       # Black
BRed='\033[1;31m'         # Red
BGreen='\033[1;32m'       # Green
BYellow='\033[1;33m'      # Yellow
BBlue='\033[1;34m'        # Blue
BPurple='\033[1;35m'      # Purple
BCyan='\033[1;36m'        # Cyan
BWhite='\033[1;37m'       # White

# Underline
UBlack='\033[4;30m'       # Black
URed='\033[4;31m'         # Red
UGreen='\033[4;32m'       # Green
UYellow='\033[4;33m'      # Yellow
UBlue='\033[4;34m'        # Blue
UPurple='\033[4;35m'      # Purple
UCyan='\033[4;36m'        # Cyan
UWhite='\033[4;37m'       # White

# Background
On_Black='\033[40m'       # Black
On_Red='\033[41m'         # Red
On_Green='\033[42m'       # Green
On_Yellow='\033[43m'      # Yellow
On_Blue='\033[44m'        # Blue
On_Purple='\033[45m'      # Purple
On_Cyan='\033[46m'        # Cyan
On_White='\033[47m'       # White

# High Intensity
IBlack='\033[0;90m'       # Black
IRed='\033[0;91m'         # Red
IGreen='\033[0;92m'       # Green
IYellow='\033[0;93m'      # Yellow
IBlue='\033[0;94m'        # Blue
IPurple='\033[0;95m'      # Purple
ICyan='\033[0;96m'        # Cyan
IWhite='\033[0;97m'       # White

# Bold High Intensity
BIBlack='\033[1;90m'      # Black
BIRed='\033[1;91m'        # Red
BIGreen='\033[1;92m'      # Green
BIYellow='\033[1;93m'     # Yellow
BIBlue='\033[1;94m'       # Blue
BIPurple='\033[1;95m'     # Purple
BICyan='\033[1;96m'       # Cyan
BIWhite='\033[1;97m'      # White

# High Intensity backgrounds
On_IBlack='\033[0;100m'   # Black
On_IRed='\033[0;101m'     # Red
On_IGreen='\033[0;102m'   # Green
On_IYellow='\033[0;103m'  # Yellow
On_IBlue='\033[0;104m'    # Blue
On_IPurple='\033[0;105m'  # Purple
On_ICyan='\033[0;106m'    # Cyan
On_IWhite='\033[0;107m'   # White


Monday, 16 March 2020

Digital clock using watch, echo, banner and tput

Digital Clock

watch

#!/bin/bash
watch -tn 1 date +%T

echo

#!/bin/bash
clear
while [ 1 ] ; do echo -e "$(date +%T)" ; sleep 1; done

banner

#!/bin/bash
clear
while [ 1 ] ; do banner "$(date +%r)" ; sleep 1; clear ; done

while

#!/bin/bash
clear
while :
do
 echo -e "\033[92m $(date '+%r')"
 sleep 1
 clear
done

tput

#!/bin/bash
while sleep 1;do tput sc;tput cup $(($(tput lines)-1)) 1;printf `date +%r`;tput rc;done

#!/bin/bash
clear
while :
do
 ti= date '+%r'
 echo -e -n "\033[7s"
 tput cup 0 69
 echo -n $ti
 echo -e -n "\033[8u"
 sleep 1
 clear
done

Ref:- Google

Friday, 2 August 2019

Reset Kali Linux Root Password in Windows Subsystem for Linux


WSL manages the default login user from DefaultUID registry.

To edit this registry value, open Registry Editor or regedit.exe from start menu
Go to this registry path,
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss

Double click on the DefaultUID value and change it to ZERO. Zero is for root user and 1000 (Decimal) or 3e8 (in hexadecimal) for normal users.

Open wsl.exe in command prompt. The prompt changes from $ to # (means root user). Run passwd command in Kali, change root password as usual.


Now go back to previous registry key, change Zero to previous value (or 3e8 in hex).

Ref:- superuser.com

Tuesday, 23 July 2019

Install Linux on Windows 10

1. Install Windows Subsystem for Linux using PowerShell


To ensure that the "Windows Subsystem for Linux" optional feature is enabled, Open PowerShell as Administrator and run:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Type Y to complete the installation and restart your computer.





2. Install Linux from the Microsoft Store


Open the Microsoft Store and choose your favorite Linux distribution.

Ref:- Linux Installation Guide for Windows 10


Thursday, 11 July 2019

Linux Security Files for Security Administrator

File                        Description

/etc/nologin         It denies login to all users except root
/etc/passwd         This file holds user account information
/etc/shadow         secured password values

/etc/xinetd.d/*     Directory to store configuration files used by xinetd
/etc/xinetd.conf   Configuration file for xinetd
/etc/inetdconf      Configuration file for inetd

/etc/inittab           Contains initial startup table
/etc/init.d/*          Configuration file for run level
/etc/hosts.allow    list of allowed hosts
/etc/hosts.deny     list of denyed hosts

Common Computer Security Tools in Linux


Tool                       Description

chage                    Change user password expiry information
find                        search for files in a directory hierarchy
lsof                         List open files

netstat                 To see the status of the network
nmap                    Network exploration tool and security/port scanner
passwd                 To update user's authentication tokens

su                           To run a command with substitute user and group ID
sudo                      To execute a command as another user
ulimit                     Resource limits on shells can be set or viewed
usermod               modify a user account

The Process File System Information in Linux


The Process File System (/proc) give you access to information about the Linux kernel and all process currently running on your system.

Files and Directories in /proc

File Name                    Content

/proc/acpi                     Information about Advanced Configuration and 
                                     Power Interface(ACPI)
/proc/bus                      Bus-specific information for each bus type, such as PCI
/proc/cmdline              The command line used to start the Linux kernel
/proc/cpuinfo               Information about the CPU
/proc/devices               Available block and character devices

/proc/dma                    Information about DMA
/proc/driver/rtc            Information about Real Time Clock(RTC)
/proc/filesystems         List of supported file systems
/proc/ide                      Information about IDE devices
/proc/interrupts            Information about Interrupt Request(IRQ) numbers

/proc/ioports                Information about Input/Output(I/O) ports
/proc/kcore                  Image of the physical memory
/proc/kmsg                  Kernel messages
/proc/loadavg              Load average
/proc/locks                  Current kernel locks

/proc/meminfo            Information about physical memory and swap space usage
/proc/misc                   Miscellaneous information
/proc/modules             List of loaded driver modules
/proc/mounts               List of mounted file systems
/proc/net                      Directory of Information about networking

/proc/partitions            List of partitions known to Linux kernel
/proc/pci                      Information about PCI devices
/proc/scsi                     Information about SCSI devices
/proc/stat                     Overall statistics about the system

/proc/swaps                 Information about the swap space
/proc/sys                      Information about the system
/proc/uptime                Information about uptime
/proc/version               Kernel version number

The Most Popular Configuration Files in Linux System

List of the most popular configuration files and brief description

Configuration File                  Description

/boot


/boot/grub                             GRUB bootloader file
/boot/grub/menu.list             boot menu
/boot/System.map                 Linux kernel map
/boot/vmlinuz                        Linux kernel

/etc

/etc/apache2/httpd.conf        Apache web server configuration file
                                             (Debian)
/etc/apt/sources.list               lists the sources from which the APT obtains
                                              packages(Debian & Ubuntu)
/etc/at.allow                          users allowed to use the at command to 
                                              schedule jobs
/etc/at.deny                           users forbidden to use the at command
/etc/bashrc                            functions and aliases for bash shell
                                             (Fedora)

/etc/bash.bashrc                    functions and aliases for bash shell
                                             (Debian, SUSE & Ubuntu)
/etc/cups/cupsd.conf            The CPUS scheduler
/etc/fonts/local.conf             font configuration file
/etc/fstab                              Information about the various file system and mount
/etc/group                             Information about groups

/etc/grub.conf                       GRUB bootloader
                                             (Fedora& SUSE)
/etc/hosts                              IP address and their hostnames
/etc/hosts.allow                    Hosts allowed to access internet service
/etc/hosss.deny                     hosts forbidden to access internet services
/etc/httpd/conf/httpd.conf    Apache web server configuration file
                                             (Fedora)

/etc/init.d                              Directory with scripts to start and stop various servers
/etc/init.d/rcS                        Linux initialization script
                                              (Debian, SUSE & Ubuntu)
/etc/inittab                             The init process that starts all the other processes
/etc/issue                               Distribution name and the version number
/etc/lilo.conf                          Linux Loader(LILO) configuration file

/etc/login.defs                       Default information for creating user accounts
/etc/modules.conf                 Configuration file for loadable kernel module
                                             (Debian)
/etc/mtab                               Information about the currently mounted file systems
/etc/passwd                           Information about all user accounts
/etc/profile                            Systemwide environment and startup file for the bash sell

/etc/profile.d                         Directory containing file that the /etc/profile script executes
/etc/rc.d/rc.sysinit                 Linux initialization script
                                              (Fedora)
/etc/shadow                           Secure file with encrypted passwords for all user accounts
/etc/shells                              List of all the shells on the system that the user can use
/etc/skel                                 Directory that holds .bash_logout, .bash_profile, .bashrc files

/etc/sysconfig                        Linux configuration files
                                              (Fedora & SUSE)
/etc/sysctl.conf                      Configuration file with kernel parameters
/etc/termcap                          Database of terminal capabilities and options
                                             (Fedora & SUSE)
/etc/udev                               Directory containing configuration files for udev
/etc/X11                                Directory with configuration files for X Window 
                                              System

/etc/X11/xorg.conf               Configuration file for X.Org X11 the X Window System
                                             (Fedora, Ubuntu & SUSE)
/etc/xinetd.conf                    Configuration for the xinetd daemon that starts internet 
                                             service on demand
/etc/yum.conf                       Configuration for the Yum package updater and installer
                                             (Fedora)

/var

/var/log/apache2                  Web-server access and error logs
                                            (Debian)
/var/log/cron                        Log file with messages from the cron process that runs 
                                            scheduled jobs
/var/log/boot.msg                File with boot messages(SUSE)
/var/log/dmesg                    File with boot messages
                                            (Debian, Fedora & Ubuntu)
/var/log/httpd                      Web-server access and error logs(Fedora)
/var/log/message                 System log


APT    - Advanced Packaging Tool
CPUS - Common Unix Printing System



Basic TCP/IP Network Configuration Files


Summary of basic TCP/IP configuration files

File                                        Description
/etc/hosts                           IP address and hostnames for local network
/etc/networks                    Names and IP address of networks
/etc/host.conf                    Translate host name in to IP address
/etc/resolv.conf                 IP address of name servers
/etc/hosts.allow                 Allow which system can access internet
/etc/hosts.deny                  Deny internet access to systems
/etc/nsswitch.conf            Translate hostnames into ip address