Wednesday, August 26, 2015

How to create an account for a specified amount of days.

How to create an account for a specified amount of days.


You can create an account with user add and specify the expiration date.
# useradd -e 2015-10-31 <newUser>

here,
-e, --expiredate EXPIRE_DATE
     The date ison YYYY-MM-DD format.

or,
you can create an account and use chage or usermod command to change the date.

# useradd <newUser>
# usermod --expiredate $(date -d "30 days" "+%Y-%m-%d") <newUser>

$ echo `date -d "30 days" +"%Y-%m-%d"`

or
# useradd <newUser>
# chage -E 2015-10-31 <newUser>

Saturday, August 22, 2015

Script - IP hostname lookup

#!/bin/sh
#
#
for i in `cat hostip`
do
  #/usr/sbin/nslookup $i >>myfile.txt.result
  echo $i "\t" `/usr/sbin/nslookup $i   |tail -2 |awk '{print $2}' |head -1`
done

==================================
echo $i "\t" `/usr/sbin/nslookup $i   |tail -2 |awk '{print $2}' |head -1`
nslookup  hm-mid-98 |tail -3
nslookup  hm-mid-98 |tail -3 | awk -F: '{print $2}' | more
cat myfile.txt.result | egrep "Name|Address" | grep -v "#" | awk -F: '{print $2}' | more


$ more .profile
export PATH=$PATH:

export EDITOR=vi
export PS1=`whoami`@`/bin/hostname`:'$PWD =>'
export SHELL=/bin/ksh
export WHMI=$(id | cut -f2 -d"(" |cut -f1 -d")")
export WHST=$(/bin/hostname)
alias l='ls -ltr;'
alias c='clear;'
alias cl='c l'
ulimit -n 2048
$ id | cut -f2 -d"(" |cut -f1 -d")"



sysctl -p

How to run fsck on root filesystem on virtual servers


How to run root filesystem checks on virtual servers


# server has root filesystem error on reboot
# this requires root filesystem check on reboot

1. Login to vmware and open a console for the server.
2. Click on the CD icon, and connect to ISO image for current version of the OS (In our case Red Hat 64-bit)
3. Use control-D to reboot server at the console session.

Note: It may take about 5-10 minutes to complete.

4. When Linux prompt is displays, click on console and type "linux rescue"  [ At the splash menu ] at the prompt
5. Select "English" for language and "US" for keyboard
6. Select "No" to start network interfaces
7. Click "Continue" for mount filesystems (this is critical to fix the errors)
8. Click "OK" when all filesystems mounted message appears
9. Shows all mounted filesystems

Note: the name of the root filesystem device, whatever is with /mnt/sysimage only

10. Display currently mounted FS.
# mount

/proc on /proc type proc (rw)
/dev on /dev type tmpfs (rw)
/dev/pts on /dev/pts type devpts (rw)
/sys on /sys type sysfs (rw)
none on /tmp type ramfs (rw)
none on /tmp/ramfs type ramfs (rw)
/tmp/loop0 on /mnt/runtime type squashfs (ro)
/selinux on /selinux type selinuxfs (rw)
/dev/sda2 on /mnt/sysimage type ext3 (rw,data=ordered)
/dev/system/TMP on /mnt/sysimage/tmp type ext3 (rw,data=ordered)
/dev/system/PKGS on /mnt/sysimage/pkgs type ext3 (rw,data=ordered)
/dev/system/WWW on /mnt/sysimage/www type ext3 (rw,data=ordered)
/dev/system/WWW-SERVERS on /mnt/sysimage/www/servers type ext3
(rw,data=ordered)
/dev/system/WWW-DOCROOT on /mnt/sysimage/www/docroot type ext3
(rw,data=ordered)
/dev/sda1 on /mnt/sysimage/boot type ext3 (rw,data=ordered)
/tmp/sysfs on /mnt/sysimage/sys type sysfs (rw)
/tmp/proc on /mnt/sysimage/proc type proc (rw)
/dev/DATA1/FMACTS on /mnt/sysimage/fmac/prod/ts/ecvm type ext3
(rw,data=ordered)
/dev/DATA1/FMACDATATS on /mnt/sysimage/fmacdata/prod/ts/ecvm type ext3
(rw,data=ordered)
/dev on /mnt/sysimage/dev type tmpfs (rw)
/dev/pts on /mnt/sysimage/dev/pts type devpts (rw)
/selinux on /mnt/sysimage/selinux type selinuxfs (rw)


11. umount all the non-rescue, real filesystem: anything under /mnt/sysimage
# mount | grep /mnt/sysimage | awk '{print $3}' | sed -n -e '2,$p' | sort -r | awk '{print "umount " $1}'  | sh

a. There may be some stragglers
# mount

/proc on /proc type proc (rw)
/dev on /dev type tmpfs (rw)
/dev/pts on /dev/pts type devpts (rw)
/sys on /sys type sysfs (rw)
none on /tmp type ramfs (rw)
none on /tmp/ramfs type ramfs (rw)
/tmp/loop0 on /mnt/runtime type squashfs (ro)
/selinux on /selinux type selinuxfs (rw)
/dev/sda2 on /mnt/sysimage type ext3 (rw,data=ordered)
/dev/system/WWW on /mnt/sysimage/www type ext3 (rw,data=ordered)

b. Unmount those by hand
# umount /mnt/sysimage/www

12. Unmount the root filesystem last
# umount /mnt/sysimage

13. Run the check on the root filesystem
# e2fsck -y /dev/sda2

14. Disconnect the CD in vmware console

15. reboot the server
# reboot

HBA issue with Linux server


Q. Following complain got from storage team.

HBA cards on the Hosts rm-lx-app-28 is not logged in to the switch rm-san-sw-88. Looks like either cable or card has a problem.
The WWN of the HBA card that's not logged in is 10000000c99eab1c.


Solution,
Run the systool command and see if the link shows down. If it is down, go to datacenter if you can easily access or ask someone on remote site to check it for you.

Check the value of port_state, if you see port_state = "Linkdown" then cable might be loose or might not be connected correctly.


[root@rm-lx-app-28 ~]# systool -c fc_host -v
Class = "fc_host"

  Class Device = "host3"
  Class Device path = "/sys/class/fc_host/host3"
    active_fc4s         = "0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x01 0x00 0x00 0x0                                                                                                                     0 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x0                                                                                                                     0 0x00 0x00 0x00 0x00 0x00 "
    fabric_name         = "0x0"
    issue_lip           = <store method only>
    maxframe_size       = "2048 bytes"
    node_name           = "0x20000000c99eab1c"
    port_id             = "0x019000"
    port_name           = "0x10000000c99eab1c"
    port_state          = "Linkdown"
    port_type           = "Unknown"
    speed               = "unknown"
    supported_classes   = "Class 3"
    supported_fc4s      = "0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x01 0x00 0x00 0x0                                                                                                                     0 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x0                                                                                                                     0 0x00 0x00 0x00 0x00 0x00 "
    supported_speeds    = "2 Gbit, 4 Gbit, 8 Gbit"
    tgtid_bind_type     = "wwpn (World Wide Port Name)"
    uevent              = <store method only>

    Device = "host3"
    Device path = "/sys/devices/pci0000:00/0000:00:04.0/0000:17:00.0/0000:18:02.                                                                                                                     0/0000:22:00.0/host3"
      uevent              = <store method only>


  Class Device = "host4"
  Class Device path = "/sys/class/fc_host/host4"
    active_fc4s         = "0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x01 0x00 0x00 0x0                                                                                                                     0 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x0                                                                                                                     0 0x00 0x00 0x00 0x00 0x00 "
    fabric_name         = "0x100000051eb60f80"
    issue_lip           = <store method only>
    maxframe_size       = "2048 bytes"
    node_name           = "0x20000000c99e1c96"
    port_id             = "0x019000"
    port_name           = "0x10000000c99e1c96"
    port_state          = "Online"
    port_type           = "NPort (fabric via point-to-point)"
    speed               = "8 Gbit"
    supported_classes   = "Class 3"
    supported_fc4s      = "0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x01 0x00 0x00 0x0                                                                                                                     0 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x0                                                                                                                     0 0x00 0x00 0x00 0x00 0x00 "
    supported_speeds    = "2 Gbit, 4 Gbit, 8 Gbit"
    tgtid_bind_type     = "wwpn (World Wide Port Name)"
    uevent              = <store method only>

    Device = "host4"
    Device path = "/sys/devices/pci0000:00/0000:00:04.0/0000:17:00.0/0000:18:01.                                                                                                                     0/0000:25:00.0/host4"
      uevent              = <store method only>


[root@rm-lx-app-28 ~]#

Compressing file

3.0G    opt
5.6G    var



655M    cache

4.8G    log
1.3G    messages
1.8G    messages.1
1.8G    messages.2

1. Check where you are?
# pwd
/var/log

2. Check the size of the file that you are going to compress
# du -sh *
# du -sh messages.2.gz

3. Compress the file
# gzip messages.2
# gzip messages.1

4. If you don't have space on current FS, redirect the content output to other FS to compress the file,
# gzip -c messages >/var/tmp/messages.11.29.2013.gz

5. Null the file that you just compress
# cat /dev/null>messages

6. Move the compressed file back to the same place where you compress the file.
# mv /var/tmp/messages.11.29.2013.gz .
# ls -ltr
-rw-r--r-- 1 root root      55778475 Nov 29 10:47 messages.11.29.2013.gz
-rw------- 1 root root        122998 Nov 29 10:48 messages



Kernel issue


[root@rmdb-db-35 ~]# diff /etc/grub.conf.1.12.2014 ../boot/grub/grub.conf
10c10,12
< default=0
---
> ###default=0 was changed to 1 to point to the old kernel because the new 348.6.1 keep panicking
> #default=0
> default=1
[root@rmdb-db-35 ~]# more ../boot/grub/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/mpath2p3
#          initrd /initrd-version.img
#boot=/dev/mapper/mpath2
###default=0 was changed to 1 to point to the old kernel because the new 348.6.1 keep panicking
#default=0
default=1
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-348.6.1.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-348.6.1.el5 ro root=/dev/mapper/mpath2p3
        initrd /initrd-2.6.18-348.6.1.el5.img
title Red Hat Enterprise Linux Server (2.6.18-348.1.1.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-348.1.1.el5 ro root=/dev/mapper/mpath2p3
        initrd /initrd-2.6.18-348.1.1.el5.img
title Red Hat Enterprise Linux Server (2.6.18-308.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-308.el5 ro root=/dev/mapper/mpath2p3
        initrd /initrd-2.6.18-308.el5.img
title Other
        rootnoverify (hd1,0)
        chainloader +1
[root@rmdb-db-35 ~]# more /etc/grub.conf.1.12.2014
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/mpath2p3
#          initrd /initrd-version.img
#boot=/dev/mapper/mpath2
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-348.6.1.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-348.6.1.el5 ro root=/dev/mapper/mpath2p3
        initrd /initrd-2.6.18-348.6.1.el5.img
title Red Hat Enterprise Linux Server (2.6.18-348.1.1.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-348.1.1.el5 ro root=/dev/mapper/mpath2p3
        initrd /initrd-2.6.18-348.1.1.el5.img
title Red Hat Enterprise Linux Server (2.6.18-308.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-308.el5 ro root=/dev/mapper/mpath2p3
        initrd /initrd-2.6.18-308.el5.img
title Other
        rootnoverify (hd1,0)
        chainloader +1
[root@rmdb-db-35 ~]#



After a fix,

[root@rmdb-db-35 ~]# more /etc/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/mpath2p3
#          initrd /initrd-version.img
#boot=/dev/mapper/mpath2
###default=0 was changed to 1 to point to the old kernel because the new 348.6.1 keep panicking
default=0
#default=1
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-348.6.1.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-348.6.1.el5 ro root=/dev/mapper/mpath2p3
        initrd /initrd-2.6.18-348.6.1.el5.img
title Red Hat Enterprise Linux Server (2.6.18-348.1.1.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-348.1.1.el5 ro root=/dev/mapper/mpath2p3
        initrd /initrd-2.6.18-348.1.1.el5.img
title Red Hat Enterprise Linux Server (2.6.18-308.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-308.el5 ro root=/dev/mapper/mpath2p3
        initrd /initrd-2.6.18-308.el5.img
title Other
        rootnoverify (hd1,0)
        chainloader +1
[root@rmdb-db-35 ~]#

File Encryption with GPG

File Encryption with GPG


1. Installation and generating public/private key pair

Encription method:

It uses asysmmetric encription, what that mean is encription is using a matched pair of encripted private and public keys. They are mathmetically created algorithm at the same time. The public key is given to the end user who wants to have a secure transfer of information with the creator of the public key. End user encript the information with the public key and sends over the network on the other end where ower of the key will decript the information using his/her private key. Sometimes, owner can use symetric key (kind of password) to create a public key where end user has to use it while encripting the messag/file.

Package need to install
crypto-util

Create users and install packages

a. login as a root and create three accounts.
# useradd jay; useradd surya;useradd chandra

b. Install crypto-util paclage
# yum install crypt* -y

c. Now, log out and login as jay and generate a key using pgp -gen-key command.

$ gpg --gen-key

Note: gpg can create different types of keypairs.

just go through and select the default options for practice.

d. List your keys
$ gpg -list-keys


e. Export your public key
$ pgp -armor -export >/var/tmp/jay.key

Note: public key extention should be .key

f. Now, login as a user surya and import the public key generated by Jay.
$ gpg -import /tmp/jay.key; gpg -list-keys

2. Encripting/decripting a file.

Plan: now, surya will encript a file using jay's public key and jay will decript the message that surya sends to jay using the private key.

a. Encript your file.
$ tail -2000 /var/log/messages >/var/tmp/myfile.txt

b. Now, encript the file myfile.txt with jay's public key.
$ gpg -encript -armor -recipient jay@sama.expanor.local myfile.txt


just follow the prompt and your message is encripted.the extenstion is now myfile.txt.asc.

now, copy the message file with proper permission so that jay can access it.


c. login as user jay and decript the message. It will prompt you for password.

$ gpg -decrypt myfile.txt.asc

d. Now, login as chandra and see if you can read the message.

Of course you can't read it because you are now the owner of the private key and you don't have it. to encript the message, you should be owner or should have private key to decript the key.