Saturday, June 9, 2018

Linux Shell


Kinds of Shells
---------------

bash
  - Bourne Again SHell
  - based on earlier version of sh
  - commonly default shell in Linux

sh
  - Bourne shell
  - this is were BASH was based
  - not often used in Linux but usually a pointer to /bin/bash

tcsh
  - based on earlier C shell (csh)
  - not popular in Linux

csh
  - the original C Shell
  - not commonly used in Linux
  - very similar to tcsh

ksh
  - combination of features of csh and sh

zsh
  - Z Shell
  - evolution of ksh

How to start a shell?
--------------------

There are several ways and here are the most common ways.

1. Once you logged in to a linux server (either by SSH or via console), you
   automatically starts your own shell
2. In xterm or GUI, you can right click on the desktop and open a terminal. In
   that way, you are also starting a shell

Parts of a shell
----------------

Understanding every part of the shell is very important specially for linux
administrators because it is the one you interact with everytime you deal with
Linux.

As an example, when you logged as a regular, the shell will provide you a prompt
where you can type your commands.

$ uname
Linux
$

The "$" represents something. In Linux by default, it means that you are logged
in as regular user. You may customized it by changing the PS1 environment
variable.

A root's prompt starts with "#" so its important to clearly distinguished a
regular user's prompt from a root's prompt. That can avoid accidentally running
destructive command like "rm -fr *" on /. The command used in the example above
is "uname" and its output is "Linux". Depending on the command you type, it may
return an output or nor. Or it may even return an error.

Types of commands you can use
-----------------------------

2 types of commands are "Internal" and "External" commands. Internal are the
ones built-in in linux while External are the  ones came from outside sources
(e.g came from installing a package). To see what type
of command, execute the following:

$ type cd
cd is a shell builtin
$ type date
date is /bin/date
$
$ type -a pwd
pwd is a shell builtin
pwd is /bin/pwd
$

The first example tells us that "cd" is an Internal command while the second one
indicates that "date" is an external command. There are some commands you may
encounter that has both Internal and External versions.

So to see it, add "-a" on type command that shown in the 3rd example above.

Keeping Track of Command History
--------------------------------

- bash history displays previous commands (usually last 500)
- this doesn't record the commands you enter inside of a prompt as:
   [root@localhost ~] passwd  # this will appear in bash history
   Changing password for user root.
   Changing password for root.
   Current password:          # anything you type here will not

history  # displays typically last 500 lines
history -c  # clears current history
~/.bash_history  # this is your history file (be aware that this might be
readable by other users)

Ways of executing a program
---------------------------

1. Using "exec"
  - run by: exec /path/to/program
  - exec doesn't create a new process
  - exec replaces the current shell
  - when the process terminate, your shell also terminates

2. The most common way
  - to run a script this way: ./myscript.sh
  - or this way: /path/to/myscript.sh
  - the script must have executes bit enabled (chmod +x myscript.sh)

Shell Tricks
------------

Typing in linux terminal is a major part in using a Linux based system so
here are some shell tricks and shortcuts that makes typing easier and more
efficient.

tab completion
 - you may hit TAB at the end of each command to auto complete
 - if the amount of string entered doesn't allow bash to determine uniqueness,
   you may
   hit another TAB to display possible results (so it would be TAB..TAB)
 - further improved by installing bash-completion

examining previous commands entered
  - typing "history" will show past entries accompanied with a number
  - given the history number, you may type ! to re-execute a specific
    entry which
    is helpful in re-typing long commands such as "for i in blah blah..; do
    blah ... blah
  - another efficient way of searching is by using "reverse search"; that is by
    pressing CTRL + R then you may start searching any keyword at the start,
    middle, or end of the command

fast moving
  - pressing ALT + F (or CTRL + right arrow in some) will move the cursor
    position 1 word forward while ALT + B to move 1 word backward (CTRL + left
    for some)
  - to quickly move at the end of the line, you may press CTRL + E (or just hit
    END button) on your keyboard while moving at the start is CTRL + A (or just
    HOME button)

delete multiple words at a time
  - by default pressing backspace (or CTRL + H) will delete 1 character at a
    time
  - to delete 1 word at a time, press CTRL + W
  - pressing CTRL + U will delete all words before the current cursor position
  - CTRL + K will delete all words after the current cursor positi

getting the last word of previous command (one of my favorite)
  - often we need to use the last word of the previous command we entered
  - as an example, see the series of commands below:
      touch /tmp/test
      chmod 600 /tmp/test
      vi /tmp/test
  - to quickly reuse /tmp/test instead of re-typing it or copying it, you may
    press ESC followed by . (period)

Terminating the shell
---------------------

Two commands can be used to terminate a shell session:

exit - terminates any shell
logout - same as above but terminates only login shells (e.g logins from ssh)

Getting help
------------

Since we are dealing with command line, we often need more information on a
particular command we are using. There are several ways on how to display
command information.

1. Using "man" - perhaps the most important help utility in Linux
  man   # displays man pages of a command
  man -k   # searches the man database for commands (or description)
                     matching the specified string
  man -K   # similar as above but do a full search (takes significantly
                     longer to complete)
  man
  # prints information on a specific
                                                manual section (see discussion
                                                below)

Man contains sections which groups the type of information you can see on a
particular command. Not all command have the same manual sections. Here are the
example of manual sections:

Section Number
  1 -- user commands (contains traditional command usage and options)
  2 -- system calls provided by the kernel (not commonly used by sys ads)
  3 -- library calls
  4 -- device files
  5 -- file formats (these are config files like "man 5 fstab")
  6 -- games
  7 -- miscellaneous
  8 -- system administration commands (usually ones that must be run as root)
  9 -- kernel routines

On the 9 manual sections, only 1, 5, and 8 are the most commonly used by system
administrators.

2. Using pinfo/info - not all commands support this
  info coreutils 'ls invocation'  # as an example, you can see this command
  under "SEE ALSO" of ls (1)

3. Exploring /usr/share/doc
  - some packages includes README files under this directory
  - you can see examples here
  - sometimes there are also sample configurations

Environment Variables
---------------------

These are global values read by the shell and inherited by the user (which can
also be overriden) during startup. To see all environment variables, type "env".
Here are the common environment variables in Linux.

PS1 - this is your prompt (e.g [user@host] $)
HOME - contains path to your home directory
HOSTNAME - the hostname of current machine where you are logged in
LOGNAME - the username used to login to the machine

To display the values, use the following command:

echo $HOME

sources:

PS1 code generator | http://bashrcgenerator.com/

Wednesday, June 6, 2018

Linux Package Managers (RPM and Debian)


Introduction
============

In this post, we will discuss the 2 common package managers in Linux.
They are the following:

1. RPM-based - rpm,yum
2. Debian-based - dpkg,apt-get,dselect,aptitude,apt-cache

RPM
===

Examples of systems that uses RPM-based package managers are Red Hat, Fedora,
and Centos. The "rpm" command is to install packages directly. It doesn't handle
dependency requirements so you need to install the needed packages (if there is
any) before installing the package you want. For example, if you want to install
packageA but it needs packageB, you need to install packageB first before
proceeding with packageA. Here are the common uses of "rpm" command.

Installing/
Upgrading
# installs a package (install only if it doesn't exist)
rpm -i

# same as the above, but increases verbosity (-v) and hashes as progress
# indicator (-h)
rpm -ivh

# installs a package even if it exists (package reinstall)
rpm -ivh --force

# freshens a package (upgrade a package if ONLY an older version exists)
rpm -Fvh

# installs a package if that doesn't exist or upgrade a package if it
# exists
rpm -Uvh

# installs a package ignoring dependency checking (use this with caution
# because the program you will install might not work)
rpm -ivh --nodeps

# do a dry-run instead of installing actual package
rpm -i --test
Removing
# removes a package
rpm -e
Querying
# queries a package (-q) by printing information (-i); takes only
# package name and not filename (file.rpm)
rpm -qi

# lists files associated in the package (-l)
rpm -ql

# shows you on what package the file/directory/script came from; example
# is rpm -qf $(which cp)
rpm -qf

# prints change log of the given package
rpm -qi --change-log

# prints scripts that ran when the package was installed
rpm -qi --scripts

# queries a package filename instead (-p) of package name; example is
# rpm -qip my-program.rpm
rpm -q[other query options]p file_name.rpm 

YUM
===

There is also an RPM-based tool that handles dependency conflicts automatically
and that is YUM (Yellowdog Updater Modified). "yum" command is the one we used
which can also do what "rpm" command can. This is the best package management
tool to use if you are using the Linux distributions mentioned above.

The main disadvantage of this is you need a repository before you can do any
transactions whereas RPM don't. Repositories are the ones inside
/etc/yum.repos.d directory and will be discussed on a separate post in this
blog. YUM commands that are commonly used are:

Installing/
Upgrading
# installs a package or multiple packages
yum install

# same as above but answers "y" to all installation questions
yum install -y

# updates all installed packages on the system
yum update

# updates a single or multiple packages
yum update

# installs a local rpm file using your yum repository to resolve
# dependencies
yum localinstall
Removing
# removes packages and all packages where the package depends on
yum remove

# same as above
yum erase
Querying
# lists all installed packages
yum list

# searches all package names that contains the string; prints both
# installed and available packages
yum search *pattern*

# deeper search compare to the above command (searches also contents not
# only package names)
yum search all *pattern*

# shows what package owns the file/directory
yum provides

# use this when you don't get a result from the above command
yum whatprovides */

# shows info on a package (similar to rpm -qi)
yum info

# checks whether there are updates available
yum check-update
  
DPKG
====

Aside from RPM-based systems, we also have Debian-based systems. Examples of
these are Ubuntu and of course Debian. The package management tools they use are
apt-get, aptitude, dselect, and dpkg. "apt-get" is similar to yum where you need
a repository which is defined under /etc/apt/sources.lst, "dpkg" is similar to
rpm, "dselect" is a menu based tool, and aptitude is a combination of dselect's
menu based and apt-get's cli features. Debian packages by convention ends with
.deb. Let's start with dpkg. Here are the common commands for dpkg.

Installing/
Upgrading/
Configuring
# installs a package
dpkg -i

# ignore dependency conflict (similar to --nodeps in rpm)
dpgk -i --ignore-depends=

# tests for dependency only (similar to --test in rpm)
dpkg -i --no-act

# doesn't install a package if a newer version of same package is
# already installed
dpkg -iG

# doesn't install a package if same version of package is already
# installed
dpkg -iE
Removing
# removes a package while retaining its configuration files
dpkg -r

# removes a package and configuration files
dpkg -P
Querying
# displays information about an installed package
dpkg -P

# displays information about an uninstalled package
dpkg -I

# displays current installed packages
dpkg --get-selections

# list installed packages matching the pattern string
dpkg -l

# lists files associated with the package (similar to rpm -ql)
dpkg -L
   
One command that is very useful if you want to return the package to its
original state (fresh install with default settings) is "dpkg-reconfigure".

For example, the command below will reconfigure the samba package, asking the
packages initial installation questions and restarting its daemons.

dpkg-reconfigure samba

APT-GET
=======

Similar to yum , Debian-based systems have the tool called "apt-get" which
automatically handle dependency conflicts. Before using it, make sure you have
the appropriate sources inside /etc/apt/sources.lst. Here are the common usage
of that command:

Installing/
Upgrading
# installs a package
apt-get install

# installs a package assuming "y" to all questions
apt-get install -y

# don't upgrade a package if an older version exists
apt-get install --no-upgrade

# downloads a package but doesn't install it
apt-get install -d

# performs a simulation/dry-run without installing any package or
# configuring any file
apt-get install -s

# obtains information on available packages inside /etc/apt/sources.lst
apt-get update

# upgrades all installed packages to newer versions
apt-get upgrade

# similar to above command but performs "smart" conflict resolution
apt-get dist-upgrade
Removing
# removes a package
apt-get remove
Configuring
# performs any changes in package status left undone after running
# deselect
apt-get dselect-upgrade

# performs housekeeping (like yum clean)
apt-get clean

# similar to the above command but removes about packages that can no
# longer be downloaded
apt-get autoclean
Querying
# checks package database for consistency and broken package
# installations
apt-get check
  
APT-CACHE
=========

We also have apt-cache. Its only purpose is to provide information about Debian
package database. Here are sample commands:

Listing
# displays information about a package
apt-cache showpkg

# displays package statistics (how many installed, dependencies recorded
# etc..)
apt-cache stats

# displays information about unmet dependencies
apt-cache unmet

#  shows on what packages the one you specified depend on
apt-cache depends

# displays all installed packages
apt-cache pkgnames

# displays list of installed packages matching the string specified
apt-cache pkgnames

DSELECT
=======

A menu-based package manager also exists for Debian-based systems. That is
"dselect". When invoked, it will display you the following menu.

0. [A]ccess Choose the access method to use.
1. [U]pdate Update list of available packages, if possible.
2. [S]elect Request which packages you want on your system.
3. [I]nstall Install and upgrade wanted packages.
4. [C]onfig Configure any packages that are unconfigured.
5. [R]emove Remove unwanted software.
6. [Q]uit Quit dselect.

APTITUDE
========

A tool that combines dselect's menu based gui and apt-get's cli is "aptitude".
Here are the common usage:

Listing/
Querying
# searches packages related to the one you
# NOTE: (apt-get seems not to have this feature
aptitude search specified

# update package list from APT repository
aptitude update

# shows complete options
aptitude help
Installing/
Upgrading
# installs a package
aptitude install

# upgrades all installed packages
aptitude full-upgrade

# conservative version of the above command
aptitude safe-upgrade

# removes already-downloaded packages that are no longer available
aptitude autoclean
Removing
# removes a package (w/ leading dash)
aptitude install -

# same as above
aptitude remove

Tuesday, June 5, 2018

NTP (Network Time Protocol)

Basics
------

- user space daemon is "ntpd" running on udp port 123
- ntpd updates the system clock
- system clock is a software clock running in the kernel w/c is more accurate
  compared to RTC
- RTC is the hardware clock whose value varies depending on the temperature
  (real time clock)

What is Drift File?
-------------------

- stores the frequency offset between the system clock running at its nominal
  frequency and the frequency required to remain in synchronization with UTC
- used to adjust system clock every time ntpd start
- located in: /var/lib/ntp/drift (directory must be writable by NTPD)
- entry on config file: driftfile /var/lib/ntp/drift

Time on Virtual Machines
------------------------

- they have no hardware clock w/c is a more accurate time source
- load of hosts (ESX) where they resides affects the timekeeping (e.g higher
  load slows down clock ticks)

NTP Strata
----------

stratum 0
atomic clocks, GPS, mobile phone systems
stratum 1
computer with radio clock, GPSclock, or atomic clock attached
stratum 2
Reads from stratum 1; Serves to lower strata
stratum 3
Reads from stratum 2; Serves to lower strata
stratum n+1
Reads from stratum n; Serves to lower strata
stratum 15
Reads from stratum 14; This is the lowest stratum
stratum 16
unsynchronized state

How ntpd chooses a time source?
-------------------------------

1. it will check which is the closest (e.g least delay, offset, jitter)
2. it will check the stratum
3. it will check "prefer" directive in /etc/ntp.conf

NTP configuration file: /etc/ntp.conf
-------------------------------------

assigning a time source
## basic
server [options]

## assigning from an ntp pool
server 0.rhel.pool.ntp.org iburst
restricting access
## don't allow IPs on these list to modify your config file and query
## your NTP server
restrict 192.168.1.0 mask 255.255.255.255 nomodify noquery

## makes sure localhost has full permissions w/o any restricting keyword
restrict 127.0.0.1
misc
## fails back to local clock if no time source is available
server 127.127.1.0
fudge 127.127.1.0 stratum 10

Some facts about leap seconds
-----------------------------

- affected systems are only those who use NTP
- by default, systems not running NTP are not affected
- leap second might make the system (or the application running inside) crash
- when a leap second is inserted, kernel prints it to /var/log/messages
- Cron jobs scheduled on 00:00 UTC won't be launched twice during leap second

Action Plans to do for Leap Second (from Redhat support)
--------------------------------------------------------

1) You can stop ntpd before June 30th, and then start it again when it is more
   convenient. Once it is started, it will sync with the NTP servers.
2) Use ntpd in slew mode (-x option). With this configuration the time won't be
   stepped, instead it will be gradually adjusted.

Commands
--------

Displaying
## checks time servers (detailed status)
ntpq -p

COLUMNS:
remote and refid = remote NT P server, and its NT P server
st = stratum of server
t = type of server (local, unicast, multicast, or broadcast)
poll = how frequently to query server (in seconds)
when = how long since last poll (in seconds)
reach = octal bitmask of success or failure of last 8 queries
        (left-shifted); 377 = 11111111 = all recent queries were
        successful; 257 = 10101111 = 4 most recent were successful,
             5 and 7 failed
delay = network round trip time (in milliseconds)
offset = difference between local clock and remote clock
        (in milliseconds)
jitter = difference of successive time values from server (high
              jitter could be due to an unstable clock

NOTE:
- unreliable time source are those with high delays, offset, and jitters
- its current time source is the one with *

## checks for brief ntp status
ntpstat

## syncs with ntp server manually
ntpdate –u

## queries only; doesn't set the clock
ntpdate -q

Monday, June 4, 2018

NIC Bonding in RHEL/CentOS 6


1. create bond0 config file
# vi /etc/sysconfig/network-scripts/ifcfg-bond0
--- START EDIT ---
DEVICE=bond0
IPADDR=10.10.10.11
NETWORK=10.10.10.0
NETMASK=255.255.255.0
USRCTL=no
BOOTPROTO=none
ONBOOT=yes
--- END EDIT ---

2. edit the following files:
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
--- START EDIT ---
DEVICE=eth0
MASTER=bond0
SLAVE=yes
HWADDR=00:0C:29:8D:FB:EF
TYPE=Ethernet
UUID=5606b424-2186-410d-9dbc-dcb65b330bd9
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
--- END EDIT ---

# vi /etc/sysconfig/network-scripts/ifcfg-eth1
--- START EDIT ---
DEVICE=eth1
MASTER=bond0
SLAVE=yes
HWADDR=00:25:B5:0B:B1:00
TYPE=Ethernet
UUID=6353d563-0a85-47d2-b566-37f6c91c8973
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
--- END EDIT ---

3. create bond module file
# vi /etc/modprobe.d/modprobe.conf
--- START EDIT ---
alias bond0 bonding
options bond0 mode=balance-alb miimon=100
--- END EDIT ---

4. restart network
# service network restart

Sunday, June 3, 2018

Installing Data Domain Plugin in Netbackup media server


Installing
==========

1. download the plugin from the linke below:
(Data Domain Software > Data Domain Boost - Open Storage)

2. copy the file to the media server that needs access to the DD

3. stop nbrmms
# nbrmms -terminate

4. extract the installation files and install
# gunzip -c OST_2.4.1.0-289644_OST_solaris_sparc.tar.gz | tar xvf -
# ./install.sh

5. start nbrmms
# nbrmms

6. verify installation
# bpstsinfo -pi -stype DataDomain
# ls -lt /usr/openv/lib/ost-plugins

plugins are located in these paths:

/usr/openv/lib/ost-plugins --> Unix/Linux
C:\Program Files\Veritas\bin\ost-plugins --> Windows
C:\Program Files\VERITAS\NetBackup\bin\ost-plugins --> Windows

sample ost-plugin in windows:
libstspiDataDomain.dll

sample ost-plugin installer in windows:
ibstspiDataDomainSetup.exe

Uninstalling
============

1. there is no way of uninstalling it (e.g ./uninstall.sh)

2. you just need to install a different version and the previous version
will be overwritten


Saturday, June 2, 2018

Renaming a volume group in RHEL/CentOS 6


1. Verify old name
# vgs

2. Rename VG
# vgrename old_vg_name new_vg_name

3. Backup and replace references in fstab
# cp -p /etc/fstab /etc/fstab.$(date +%F)
# grep old_vg_name /etc/fstab
/dev/mapper/old_vg_name-lv_root    /                       ext4    defaults        1 1
/dev/mapper/old_vg_name-lv_swap    swap                    swap    defaults        0 0

4. Backup and replace referencs in grub.conf
# cp -p /boot/grub/grub.conf /boot/grub/grub.conf.$(date +%F)
# grep old_vg_name /boot/grub/grub.conf
kernel /vmlinuz-2.6.32-642.1.1.el6.x86_64 ro root=/dev/mapper/old_vg_name-lv_root ... rd_LVM_LV=old_vg_name/lv_root ... rd_LVM_LV=old_vg_name/lv_swap ...

5. Backup and rebuild initramfs
# cp /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.bak
# dracut -f

6. Restart server
# reboot

Friday, June 1, 2018

Online data migration w/ LVM


If you want to migrate your data to a new disc - let's say you decided to
purchase another storage appliance and you want your MYSQL datadir to move to
that appliance without any downtime, LVM mirroring is the way to go.

1. Prepare your LUN and assign/zone it to your server

2. Once the LUN is available, add it to the current VG that holds the MYSQL
datadir
# vgextend myql_vg /dev/sdc

3. Create a mirror of the MYSQL logical volume pointing to the newly added LUN
(this is the RIGHT side of the mirror)
# lvconvert -m1 /dev/mapper/mysql_vg-mysql_lv /dev/sdc

4. Wait until the sync is complete (must be 100% under Cpy%Sync field)
# lvs

5. Once the sync is complete, remove/detach the LEFT side of the mirror (that is
the old LUN) from the MYSQL logical volume
# lvconvert -m0 /dev/mapper/mysql_vg-mysql_lv /dev/sdb

6. Remove the old LUN from the MYSQL VG
# vgreduce mysql_vg /dev/sdb

7. Remove PV label
# pvremove /dev/sdb

8. Detach it from your server

9. Hola! You were able to move your MYSQL database to a new and faster disk
without a  downtime!