Tuesday, December 8, 2015

Unix useful Tips & Tricks

Python script to check dependency services of a particular service in RHEL 7

Below is a sample python script to check dependency services of a particular service in RHEL 7.  It will prompt you to enter the service need to be verified 
******************************************
#!/usr/bin/python

import os
import subprocess

servname = raw_input ("Please enter servicename need to be checked:")
os.system ("systemctl list-dependencies --after $servname")
************************************************
as an example it prompted me to enter the service and i have entered gdm.service

[root@redhat7-test ~]# python2.7 service_dep.py
Please enter servicename need to be checked:gdm.service

output is below 
************
├─accounts-daemon.service
├─gdm.service
├─network.service
├─rhnsd.service
├─rtkit-daemon.service
└─multi-user.target
  ├─abrt-ccpp.service
  ├─abrt-oops.service
  ├─abrt-vmcore.service
  ├─abrt-xorg.service
  ├─abrtd.service
  ├─atd.service
  ├─avahi-daemon.service
  ├─brandbot.path
  ├─chronyd.service
  ├─crond.service
  ├─cups.path
  ├─dbus.service
  ├─hypervkvpd.service
  ├─hypervvssd.service
  ├─irqbalance.service
  ├─kdump.service
  ├─ksm.service
  ├─ksmtuned.service
  ├─libstoragemgmt.service
  ├─libvirtd.service
  ├─mdmonitor.service
  ├─ModemManager.service
  ├─netcf-transaction.service
  ├─network.service
  ├─NetworkManager.service
  ├─plymouth-quit-wait.service
  ├─plymouth-quit.service
  ├─postfix.service
  ├─rescue.service
  ├─rhel-configure.service
  ├─rhnsd.service
  ├─rhsmcertd.service
  ├─rngd.service
  ├─rsyslog.service
  ├─smartd.service
  ├─sshd.service
  ├─sysstat.service
  ├─systemd-logind.service
  ├─systemd-user-sessions.service
  ├─tuned.service
  ├─vmtoolsd.service
  ├─basic.target
  │ ├─rhel-import-state.service
  │ ├─systemd-ask-password-plymouth.path
  │ ├─paths.target
  │ │ ├─brandbot.path
  │ │ ├─cups.path
...........................................( to be continued)......................................


ASM driver issue after  RHEL patching

In most of the cases we used to face Oracle ASM module loading issue after patching. when we try to /etc/init.d/oracleasm listdisks, we will get the message as unable to load the oracle ASM. So in this case we have patched the server from RHEL 5.6 to RHEL 5.11 which is upgraded the kernel from 2.6.18-238.9.1.el5 to 2.6.18-398.el5.

1. First create a directory in below structure 

#mkdir  /lib/modules/2.6.18-398.el5/kernel/drivers/addon/oracleasm/

2. copy the oracle ASM module from old kernel structure to the new one 

#cp /lib/modules/2.6.18-238.9.1.el5/kernel/drivers/addon/oracleasm/oracleasm.ko lib/modules/2.6.18-398.el5/kernel/drivers/addon/oracleasm/

3. Then load the new module using below command 

#modprobe /lib/modules/2.6.18-398.el5/kernel/drivers/addon/oracleasm/oracleasm.ko

4. Install the module using below command 
#insmod /lib/modules/2.6.18-398.el5/kernel/drivers/addon/oracleasm/oracleasm.ko

5. Now if we check the oracle ASM  rpms you can see below output 

# rpm -qa | grep oracleasm
oracleasm-2.6.18-238.9.1.el5-2.0.5-1.el5
oracleasm-support-2.1.4-1.el5
oracleasmlib-2.0.4-1.el5

6. We need to get the manual installation script from RPM which is need to be executed 
#rpm -q --scripts oracleasm-2.6.18-238.9.1.el5-2.0.5-1.el5
postinstall scriptlet (using /bin/sh):
depmod -ae 2.6.18-238.9.1.el5

7. Now install the depmod script 
#depmod -ae 2.6.18-238.9.1.el5

8. After this installation once you reboot the server and check the oracle ASM status we can see all the disks are visible 
#/etc/init.d/oracleasm listdisks
ASMDATA01
ASMDATA02
ASMDATA03
ASMDATA04
ASMDATA05
ASMDATA06
ASMDATA07
ASMDATA08
ASMDATA09
ASMDATA10
ASMDATA11
ASMDATA12
ASMDATA13
ASMDATA14
ASMDATA15
ASMDATA16
ASMDATA17
ASMDATA18
ASMDATA19
ASMDATA20
ASMDATA21
ASMDATA22
******************************************************************************

Linux Password reuse configuration is not working in RHEL 5 & 6

Generally if we configure password remember option in /etc/pam.d/system-auth our assumption is it should work as expected. But unfortunately in RHEL 5/6 system it will not work 

For example the default /etc/pam.d/system-auth file in RHEL 6 is given below 


The configuration for password remember/reuse is 

password    sufficient    pam_unix.so sha512 shadow nullok try_first_pass use_authtok remember=1                        
 This will create a file called /etc/security/opasswd and used passwords will be stored in encripted format in side this file. But in this case the file is generated but it will not get updated as expected . So what ever we will configure as remember count it will not update. 

-rw-------. 1 root root    0 Aug 15  2011 opasswd_old

So in this case the solution which is working is we need to load one more module inside system-auth called 
pam_pwhistory.so 

[root@rhel6 security]# locate pam_pwhistory.so

/lib64/security/pam_pwhistory.so

So the updated system-auth file will be shown as  below 



Now if you check the /etc/security/opasswd file it is getting updated as expected 

[root@rhel6 security]# cat opasswd
ratheesh:500:4:$6$tL/oHK6s$1lHNvcvFT03XB7r6BiWGSppsra9aU3/dRpXykLr9VXZuNxbB6upACW1iBipLFv3gWrM/neG884MM17ifeijSG/,$6$zkCpe2ES$Fabn1qFnLROzHI7iCDOIbKLnUzhH6kCDj5SARVzEUQ7u4DzlrlBOqmE3snXSXSnRLlhYppRlF4fa1woQtHv.o0,$6$e/AlfJGO$6B0X7zKZMwWZ0ldVg99XqjuvtyM5q3RS/M1ZLIfbOUvz77GgU2B87PijTh.ZE7DkRVUz5TNGZVni4mkZzpDNp0,$6$XNOH1T4B$545KhPkth4nfVEkFBaSJsQk/rbmoZ5JDyyws3PCftInxxmxRaRvHF9bklxRiu6raNJKGEJA5FfkJVssCDEp9c0

Thats it....
********************************************************************************

RHEL7 Graphical User Interface (GUI) is not coming

By default when you install RHEL 7 , GUI will not come up as default and same need to be installed separately. In this case we need to install  Gnome packages and it's dependencies  using the yum repository .

1. Login to the RHEL 7 system

2. We need to check the groups available from yum repository 

[root@redhat7-test tmp]# yum grouplist
Loaded plugins: product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Available environment groups:
   Minimal Install
   Infrastructure Server
   File and Print Server
   Basic Web Server
   Virtualization Host
   Server with GUI
Available Groups:
   Compatibility Libraries
   Console Internet Tools
   Development Tools
   Graphical Administration Tools
   Legacy UNIX Compatibility
   Scientific Support
   Security Tools
   Smart Card Support
   System Administration Tools
   System Management
Done

3. Now we need to install the group called "Server with GUI" 

[root@redhat7-test tmp]# yum groupinstall 'Server with GUI'

...........................output will be omitted......................................


 xorg-x11-drv-void.x86_64 0:1.4.0-23.el7                                          xorg-x11-drv-wacom.x86_64 0:0.23.0-6.el7
  xorg-x11-font-utils.x86_64 1:7.5-18.1.el7                                        xorg-x11-fonts-Type1.noarch 0:7.5-9.el7
  xorg-x11-glamor.x86_64 0:0.6.0-2.20140918git347ef4f.el7                          xorg-x11-server-common.x86_64 0:1.15.0-32.el7
  xorg-x11-server-utils.x86_64 0:7.7-4.el7                                         xorg-x11-xkb-utils.x86_64 0:7.7-9.1.el7
  yajl.x86_64 0:2.0.4-4.el7                                                        yelp-libs.x86_64 1:3.8.1-7.el7
  yelp-xsl.noarch 0:3.8.1-2.el7                                                    zenity.x86_64 0:3.8.0-4.el7

Complete!

4. In RHEL7 init run levels are mentioned as "targets" , so we need to check the current target using below command 

[root@redhat7-test tmp]# systemctl get-default
multi-user.target

5. Modify the target as "graphical target" using below command 

[root@redhat7-test tmp]# systemctl enable graphical.target --force
rm '/etc/systemd/system/default.target'
ln -s '/usr/lib/systemd/system/graphical.target' '/etc/systemd/system/default.target'

[root@redhat7-test tmp]# systemctl get-default
graphical.target

6. Reboot the system and Accept the licence agreement ( bit confusing this option, select carefully with respect to instructions)









That's it ....................

*********************************************************************************

Space cleanup


Sometime while doing the cleanup of filesystem we used to face as , if we compress or delete the big files also it will not reflect in the partition,which will be a big confusion as what is happening .

1. First we need to compress or delete the files which is big in size. Here we need to compress the root .

[root@testserver mail]# ls -lrt
total 660744
----
-rw------- 1 root     root 3131137980 Dec  8 16:30 root.

[root@testserver mail] # df -h /var

Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/rootvg-varvol
                      3.9G  3.5G  300M  95% /var

[root@testserver mail]# gzip root

[root@testserver mail]# df -h /var

Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/rootvg-varvol
                      3.9G  3.9G  0  100% /var
2. From the above exercise we found that the size of the /var is increased to 100 even though we had compressed the root mail. so what will do on next

3. We need to find the process which is doing the cleanup of root mail using below command

[root@testserver mail]#lsof | grep -i deleted
.........................output is omitted............
gdm-rh-se  6085      root  txt       REG              253,1      49184    3801116 /usr/libexec/gdm-rh-security-token-helper.#prelink#.LcSyru (deleted)
yum-updat  6119      root  txt       REG              253,1       4736     263076 /usr/bin/python.#prelink# (deleted)
gzip      10101      root    3r      REG              253,2 3038580296     458776 /var/spool/mail/root (deleted)
gzip      10102      root    3r      REG              253,2 3038580296     458776 /var/spool/mail/root (deleted)
.............................output is omitted ...................

here the pid is 10101 and 10102 which is pointing deleted process

4.So get in to the proc and pid
[root@testserver mail]#cd /proc/10101/fd

[root@testserver fd]# ls -l
total 0
lrwx------ 1 root root 64 Dec  8 16:40 0 -> /dev/pts/1
lrwx------ 1 root root 64 Dec  8 16:40 1 -> /dev/pts/1
lrwx------ 1 root root 64 Dec  8 16:40 2 -> /dev/pts/1
lr-x------ 1 root root 64 Dec  8 16:40 3 -> /var/spool/mail/root (deleted)

5. Now we need to clean up the respective pointers from these PID's

[root@testserver fd]# > 3

6. Just try to check the size of the /var partition again . It is cleaned

[root@ testserver fd]# df -h /var
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/rootvg-varvol
                      3.9G  974M  2.8G  26% /var
*******************************************************************************

How to change hostname and ip address in RHEL 7



RHEL 7 is entirely different than other versions of the redhat and here i am providing the steps to change hostname and ip address in RHEL 7


1. In RHEL7 hostname details are saved in /etc/hostname . So changing the hostname is made easy in RHEL7

[root@redhat7-test ~]# cat /etc/hostname
redhat7-test


2. We need to change the ip in below file for RHEL 7 

[root@redhat7-test ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp0s3
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=enp0s3
UUID=d65c34f9-dd5d-4190-9e4f-f7f89c4df01f
IPADDR=192.168.1.19
NETMASK=255.255.255.0
DEVICE=enp0s3
ONBOOT=yes

3. Once we update the ipaddress restart the network service using below command 

[root@redhat7-test ~]#systemctl restart network

4. check the ipaddress using below command 

[root@redhat7-test ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:6d:64:e7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.19/24 brd 192.168.1.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe6d:64e7/64 scope link
       valid_lft forever preferred_lft forever
*************************************************************************

Creating local repo in RHEL 7


1. first mount the rhel 7 DVD in the local mount point from the drive 

[root@redhat7-test /]#mount -o loop /dev/cdrom /tmp/

[root@redhat7-test /]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   14G  4.6G  9.5G  33% /
devtmpfs               488M     0  488M   0% /dev
tmpfs                  497M     0  497M   0% /dev/shm
tmpfs                  497M  6.6M  491M   2% /run
tmpfs                  497M     0  497M   0% /sys/fs/cgroup
/dev/sda1              4.7G  125M  4.6G   3% /boot
/dev/loop0             3.7G  3.7G     0 100% /tmp

2. Create a directory for RHLE 7 repository 

 [root@redhat7-test /]#mkdir -p /var/www/html/rhel7

3. Now we need to copy the contents to the local directory 

 [root@redhat7-test /]#cd /tmp

[root@redhat7-test /]#tar cvf - . | (cd /var/www/html/rhel7/; tar xvf -)

 [root@redhat7-test /]#cd /; umount /tmp/

4. Go to the directory where the repository configuration is 

[root@redhat7-test tmp]# cd /etc/yum.repos.d/

configure the file as below (file name is rhel7.repo)

[rhel7]
name=rhel7
baseurl=file:///var/www/html/rhel7/
enabled=1
gpgcheck=0

5. Now execute below commands 

[root@redhat7-test /]#yum clean all

[root@redhat7-test /]#yum repolist all

..............output is omitted...................................................

6. Now we need to install create repo command 

[root@redhat7-test /]yum install -y createrepo

Loaded plugins: product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
rhel7                                                                                                                                            | 4.1 kB  00:00:00
Resolving Dependencies
--> Running transaction check
---> Package createrepo.noarch 0:0.9.9-23.el7 will be installed
--> Processing Dependency: deltarpm for package: createrepo-0.9.9-23.el7.noarch
--> Processing Dependency: python-deltarpm for package: createrepo-0.9.9-23.el7.noarch
--> Running transaction check
---> Package deltarpm.x86_64 0:3.6-3.el7 will be installed
---> Package python-deltarpm.x86_64 0:3.6-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================================
 Package                                      Arch                                Version                                      Repository                          Size
========================================================================================================================================================================
Installing:
 createrepo                                   noarch                              0.9.9-23.el7                                 rhel7                               92 k
Installing for dependencies:
 deltarpm                                     x86_64                              3.6-3.el7                                    rhel7                               82 k
 python-deltarpm                              x86_64                              3.6-3.el7                                    rhel7                               31 k

Transaction Summary
========================================================================================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 205 k
Installed size: 553 k
Downloading packages:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                   9.1 MB/s | 205 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : deltarpm-3.6-3.el7.x86_64                                                                                                                            1/3
  Installing : python-deltarpm-3.6-3.el7.x86_64                                                                                                                     2/3
  Installing : createrepo-0.9.9-23.el7.noarch                                                                                                                       3/3
rhel7/productid                                                                                                                                  | 1.6 kB  00:00:00
  Verifying  : python-deltarpm-3.6-3.el7.x86_64                                                                                                                     1/3
  Verifying  : deltarpm-3.6-3.el7.x86_64                                                                                                                            2/3
  Verifying  : createrepo-0.9.9-23.el7.noarch                                                                                                                       3/3

Installed:
  createrepo.noarch 0:0.9.9-23.el7

Dependency Installed:
  deltarpm.x86_64 0:3.6-3.el7                                                     python-deltarpm.x86_64 0:3.6-3.el7

Complete!

7. Create the repository at /var/www/html/rhel7 ( this command will take some time to execute) 

[root@redhat7-test yum.repos.d]# createrepo /var/www/html/rhel7
Spawning worker 0 with 4432 pkgs

Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

Our local repository is ready .. 














No comments:

Post a Comment