Wednesday, December 23, 2015

Performance Analysis in solaris 10- Memory

In real world memory performance issues plays a major role in system performance. Unix system memory contains 2 types , one is physical memory which is attached to the DIMM modules of the hardware and second is swap space which is a dedicated space in the disk which is treated as a memory by OS ( since the disk I/O is much slower than the I/O to the memory in generic way we will prefer to use swap space less frequently as possible).
tivity
Swap space is only used when the physical memory is too small to accommodate system memory requirements . At that time space is freed in physical memory by paging (moving) it out to swap space ( also keep it in mind that if the increase in paging to swap space will degrade system CPU performance )

vmstat command
***********






vmstat reports virtual memory statics regarding kernel thread, virtual memory,disk,thread,cpu activity etc. Also note that in multiple CPU systems this will show the average of the number of CPU output. 

The details of the command is given below 

kthr - This indicates the kernel threads details in 3 states . 
    r - The number of kernel threads in run queue
    b - The number of blocked threads which are waiting for I/O paging 
    w - Number of swapped out lightweight processes (LWP - processes which is running under same kernel thread and shares its system resources and addresses with other lwp's)  that are waiting for resources to finish 

memory - Report the usage of real and virtual memory 
    swap - available swap space ( in kb)
    free - size of the free list (in kb)

page - Report about the page faults and paging activity . Details of this section is given below 
    re - page reclaims 
    mf - minor faults 
    pi - kilobytes paged in 
    po - kilobytes paged out
    fr - kilibytes freed
    de - anticipated short term memory shortfall ( in KB)
    sr- pages scanned by clock algorithm 

disk - Reports the number of disk operations per second . There are slots up to 4 disks with a letter and number ( letter indicates the disk type like scsi.ide ) and number is the logical number

faults - Reports trap/interrupt rates 
    in - interrupts
    sy - system calls
    cs- cpu context switches 

cpu - Breakdown usage of the cpu time. In multi processor systems it will be average of all the CPU's

    us - user time
    sys - system time
    id - idle time

swap usage analysis
****************
If you concentrate in swap analysis we need to use below mentioned two commands . 

bash-3.2# swap -s
total: 267032k bytes allocated + 86184k reserved = 353216k used, 964544k available

bash-3.2# swap -l
swapfile dev swaplo blocks free
/dev/zvol/dsk/rpool/swap 181,1 8 2097144 2097144

But there is major difference between these two commands , in the first one we are using 353216k of (964544 + 353216)k  which means of 26% in use. In the second one you can see as all the 2097144 is free means 0% is used.  In the first command (swap -s ) includes the  portion of physical memory also which is using as swap. The major difference in usage of these two commands are in generic if you are checking the swap usage over time you can use swap -s. (if the system performance is good). But if the system performance is degraded you need to concentrate more  about  the change in swap usage and what causes that change  ( also keep it in mind that swap -l displays output in 512 bytes and swap -s displays in 1024 byte blocks) .

If the system run's out of swap space it will show the error messages given below and we might think about expanding the same using creating the swap file. In general while creating the swap you have to provide size as half of the system physical memory

for example if the system memory is 8GB the ideal swap size should be 4GB

***********************************************************
application is out of memory

malloc error O

messages.1:Sep 21 20:52:11 mars genunix: [ID 470503 kern.warning]
WARNING: Sorry, no swap space to grow stack for pid 100295 (myprog)
***********************************************************

Creating the swap file

1.Login as super user
2. Create the swap file using mkfile <name> <size in k/m/g> filename
3. Activate the swap file using /usr/sbin/swap -a /path/filename
4. Add the entry at /etc/vfstab 

   /path/filename - - swap - no -

5. Verify the swap file using /usr/bin/swap -l

As a nutshell while configuring the swap , please keep it in below points 

  • Never allocate swap with size less then 30% of RAM.
  • Determine whether large applications (such as compilers and databases) will be using the /tmp  directory. If one or several of your application have a huge demand for swap space, use the swap -s  command to monitor swap resources on a similar existing system tro get estimate of the actual requirements.

Cache

If we check the free -m command in a unix box we can see major portion of the memory is in cached column. So what is mean by that cache, is it currently used by system?

[root@testserver ~]# free -m
             total       used       free     shared    buffers     cached
Mem:         15976      15195        781          0        167       9153
-/+ buffers/cache:       5874      10102
Swap:         2000          0       1999


In this case you can see 9GB is cached . These caches are called page caches / dirty caches which will act as a temporary memory for read and write process. During the write process the contents of these dirty cache will be periodically transferred to the system storage . Till 2.6.31 version , the process called pdflush will ensure that the data is transferring to system storage and clearing the dirty pages periodically. But after this kernel version there will be a thread for each device ( like sda/sdb) will monitor this mechanism 

root@pc:~# ls -l /dev/sda
brw-rw---- 1 root disk 8, 0 2011-09-01 10:36 /dev/sda
root@pc:~# ls -l /dev/sdb
brw-rw---- 1 root disk 8, 16 2011-09-01 10:36 /dev/sdb
root@pc:~# ps -eaf | grep -i flush
root       935     2  0 10:36 ?        00:00:00 [flush-8:0]
root       936     2  0 10:36 ?        00:00:00 [flush-8:16]



This same mechanism is applicable for reading also, file blocks will be transferred from disk to page cache for reading . For example if you access 100MB file twice , in second time the access will be faster as it is fetching from the cache. If Linux needs more memory for normal applications than is currently available, areas of the Page Cache that are no longer in use will be automatically deleted.

Mostly log files or database dump file ( data files) are mostly accumulated by page cache as it is accessed continuously . So configuring perfect log rotate or zipping it periodically will release the page cache when it will be really needed for system performance .







 


    











Tuesday, December 15, 2015

Performance analysis in solaris 10 - CPU

Performance analysis is one of the key task for every system admins which is an important point for the  the system availability ( especially production systems with SLA basis).We should do the periodical check for the various system parameters and ensure nothing is getting in to wrong way which is hampering the normal operations of the production systems .

The main factores we should consider for system performance analysis are disk IO, CPU,Memory & Swap,network and zones ( i am omitting other service components like  name services , NFS, kernel tuning etc.. which can be discussed separately).

CPU Loading

Load average is the average over time the number of processes in run queue. This is used to represent the load on CPU and  load average refers to three numbers with 1-5-15 minutes intervals . Typically the load average divided by the number of cpu cores are used to find the load per cpu and the load average above 1 per cpu is considered as cpu is fully utilized . Also a general rule of thumb in load average is "average value which is 4 times the number of cpu results a sluggish performance".

Load average can be monitored by the command uptime or monitoring the run queue time of the processors using sar -q command

uptime
*******
bash-3.2# uptime
4:29pm  up 34 day(s), 14:45,  2 users,  load average: 0.45, 0.49, 0.54

The last 3 numbers are the load average which will be 1,5,15 minutes interval. Now we need to find what is load metric . This metric for a particular load at given point of time is  how many processes are queued per the running process ( including the current running ones). For example in last minute if the load average is 0.50 means half of the time of the last minute CPU was idle with out any running processes. Another example of the load average is 2.50 in last minute means average of 1.5 processes are waiting to run in the queue and the CPU was overloaded by 150%

The load average can be monitored by analyzing the run queue length and amount of time to take for that using the sar -q command .

Using the sar-q command we will getto know the following information
1. The average  queue length while the queue is occupied
2. The percentage of time that the queue is occupied.

If you check the command output header you will get below details from sar -q 

SunOS testsolaris 5.10 Generic_144488-05 sun4v    12/15/2015

00:00:00 runq-sz %runocc swpq-sz %swpocc
01:00:01     1.0       1     0.0       0
02:00:00     1.0       1     0.0       0
03:00:01     1.0       1     0.0       0
04:00:00     1.0       1     0.0       0
05:00:00     1.1       1     0.0       0
06:00:00     1.0       1     0.0       0
07:00:00     1.0       1     0.0       0

08:00:01     1.1       5     0.0       0
............................................

Average      1.0       3     0.0       0

run-sz - This indicates the number of kernel threads in the memory which is waiting to occupy the CPU. Normal value of this should be less than 2 and if it is consistently become high the system CPU is fully utilized ( can consider about adding more CPU)

%runocc - This indicates the run queue (dispatch) occupancy . The consistent run queue occupancy is the CPU saturation .

swap-sz - The average number of swapped out processes

%swapocc- The percentage of time in which the processes are swapped out.

So by over all if the %runocc is greater than 90 and runq-sz value is greater than 2 we should consider about adding more CPU for a consistent system performance.

Prstate
***********
This is one of the most widely utilized system utility for below cases

1. How much my system utilized in case of CPU & memory
2. Utilization of the system ( zone wise,user wise,process wise )
3. How are the processes/threads utilizing the system ( user bond, I/O bond)


















PID: the process ID of the process.


USERNAME: the real user (login) name or real user ID.


SIZE: the total virtual memory size of the process, including all mapped files and devices, in kilobytes (K), megabytes (M), or gigabytes (G).


RSS: the resident set size of the process (RSS), in kilobytes (K), megabytes (M), or gigabytes (G).


STATE: the state of the process (cpuN/sleep/wait/run/zombie/stop).


PRI: the priority of the process. Larger numbers mean higher priority.


NICE: nice value used in priority computation. Only processes in certain scheduling classes have a nice value.


TIME: the cumulative execution time for the process.


CPU: The percentage of recent CPU time used by the process. If executing in a non-global zone and the pools facility is active, the percentage will be that of the processors in the processor set in use by the pool to which the zone is bound.


PROCESS: the name of the process (name of executed file).


NLWP: the number of lwps in the process.

Also you can sort the prstat by ascending ( S option) or descending (s option) with respect to below parameters 

cpu - sort by cpu usage ( by default this option is applicable)
pri - By process priority 
rss- Set by resident set size
size- By size of the process image
time- Sort by execution time

If you want the utilization report according to zone wise use prstat -Z . Here you can see global zone and testzone separately 


















Also one more option in prstat which is called microstat accounting (prstat -m) and it will provide the CPU latency , system time, etc


















In nutshell we can assume CPU performance issues as below

1. The number of processes in run queue is greater than the number of CPU's in the system
2. If the process queue is 4 times more than the number of available CPU's in the system
3. Also if the CPU idle time is 0 and system time is double than the user time , then the system is facing some major CPU shrink.

Also we have 3rd party performance analysis tools like Dtrace which will be discussed separately in other occasion .



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 .. 














Tuesday, November 17, 2015

How to check ID 's with sudo access in solaris 10

Here i am providing command to list sudo sccess ID's in solaris 10


*********************************************************************************
cat /etc/passwd | cut -d: -f1 | xargs -L1 sudo -l -U | ggrep -B1  "(ALL) ALL" | grep ^User | cut -d" " -f2

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

Monday, November 9, 2015

Booting procedure in Solaris SPARC architecture

                       Booting procedure in Solaris  SPARC architecture 




The different phases of solaris boot process are describing below.


Power on –> POST –>Boot Device (1-15) –>ufs boot loader –>Kernel –>/file system–>/sbin/init –> /svc/lib/svc.startd

Boot PROM base

The PROM displays the system identification number along with Banner,Hostid, macaddress,prompt chip release, version and physical memory size. This process also contains POST ( Power On Self Test) which is the hardware diagnosis routine and initializes the installed hardware.

We can see the POST messages in a serial through serial console . If serial console is not connected you can see the output of the POST through the command prtdiag -v

Sample output is given below















After the POST the PROM loads the boot compilation program called bootblk

Boot program phase



This phase will start reading the boot program which is available in 1 - 15 sector of the HDD. The OBP (Open Boot PROM) loads the primary boot program called bootblk from the boot device . ( if the bootblk is not present it has to be regenerated by running the command installgrub from a CDROM.)

ufsboot: This is a secondary boot program and this program loads the kernel core image files.

kernel: The kernel file location is /Platform/arch-i/kernel/sparkv9/unix ( if the processor is amd sparkv9 will change to amd etc) . As a part of kernel loading process the kernel banner will display including the kernel version number. The kernel initializes itself and reading modules with the help of ufsboot program untill it will load enough modules to mount the root file system . If the system complains not able to write to the root file system the booting procedure will struck in this phase.


The system parameters which is needed for booting is set at /etc/system file . Its main contents are given below


  • moddir: Changes path of kernel modules.
  • forceload: Forces loading of a kernel module.
  • exclude: Excludes a particular kernel module.
  • rootfs: Specify the system type for the root file system. (ufs is the default.)
  • rootdev: Specify the physical device path for root.
  • set: Set the value of a tuneable system parameter.
  • Init initialization phase



The kernel starts the PID 1 and which starts the /sbin/init process and internally this will starts /lib/svc/bin/svc.started which is responsible for below processes



a. configuring all network devices

b. mounting all file system

c. starts all network services

d. runs rc-scripts which brings the machine to multi user mode



In solaris 10 svc.startd is a separate boot process which is responsible for starting and stopping services during boot process. But the services which starts during start up and ends while down is configured in /etc/init.d directory



Different runlevels in solaris

Init s –>single user mode

Init 1 –> maintenance mode

Init 2 –> multiuser mode (NFS disabled)

Init 3 –> multiuser serve mode (NFS enable to share)

Init 4 –> not implemented for future purpose

Init 5 –> Shutdown & power off

Init 6 –> Shutdown & reboot

Init 0 –> Shutdown & skipped to ok prompt



All these processes as a nutshell i am providing you as a flowchart below










Monday, November 2, 2015

Linux system hardening using python 2.6

 
Here i am providing a script to perform linux system hardening using python 2.6 . Most of the commands used in this script are shell commands which is packed using python processes.

########################################################################
#!/usr/bin/python

import os
import subprocess
import pw


active_services = ['autofs','auditd','crond','iscsi','iscsid','netfs','nfs','nfslock','ntpd','restorecond','snmpd','sshd','syslog','sysstat','acpid','anacron','atd','cpuspeed','lvm2-monitor','messagebus','ntpd','network'
'syslog','vncserver']

for i in active_services:

os.system ("chkconfig +i on")

out = os.system ("echo $?")

if (out == 0):

file = open("/tmp/linuxout.txt","a")
file.write("activated i in the system");
file.close


inactive_services = ['acpid','anacron','apmd','arptables_jf','arpwatch','atd','avahi-dnsconfd','bluetooth','conman','cups','cyrus-imapd','dc_client','dc_server',
'firstboot','dovecot','dund','haldaemon','hidd','hplip','isdn','iptables','ip6tables','winbind','wpa_supplicant','xfs','readahead_early','readahead_later']

for j in inactive_services:

os.system("chkconfig +j off")
out = os.system ("echo $?")

if (out == 0):

file = open("/tmp/linuxout.txt","a")
file.write("disabled j in the system");
file.close

##backing up important files

path = /linux_bkp

os.mkdir (path, 0755)

import shutil

 imp_files = ['/etc/pam.d/system-auth','/etc/grub.conf','/etc/inittab','/etc/sysctl.conf','/etc/sysconfig/init','etc/sysconfig/prelink','/etc/security/limits.conf','/etc/syslog.conf',
 '/etc/audit/audit.conf','/etc/audit/audit.rules','/etc/ssh/sshd_config','/etc/login.defs']

 dst = /linux_bkp

 for i in imp_files:

shutil.copy(i,dst)

file = open ("/tmp/linuxout.txt","a")
file.write("important file i is backedup in /linux_bkp");
file.close


#adding nodev in /dev/shm partition

nodev_chk1 = os.system("cat /etc/fstab|grep -i /dev/shm/|grep -i nodev|wc -l")
nodev_chk2 = os.system("mount|grep -i /dev/shm|grep nodev|wc -l")

i = 1

if i in ("nodev_chk1", "nodev_chk2"):

file = open ("/tmp/linuxout.txt","a")
file.write("nodev is already configured in /dev/shm");
file.close

else:

    os.system("mount -o remount,nodev,noexec,nosuid /dev/shm")

file = open ("/tmp/linuxout.txt","a")
file.write("nodev is configured in /dev/shm");
file.close



#adding nosuid in /dev/shm

nosuid_chk1 = os.system ("cat /etc/fstab |grep -i /dev/shm|grep -i nosuid |wc -l")
nosuid_chk2 = os.system ("mount |grep -i /dev/shm |grep -i nosuid |wc -l")
i = 1

if i in ("nosuid_chk1", "nosuid_chk2"):

file = open ("/tmp/linuxout.txt","a")
file.write("nosuid is already configured in /dev/shm");
file.close

else:

os.system("mount -o remount,nodev,noexec,nosuid /dev/shm")
file.close


#to check aide rpm installed or not

p = subprocess.Popen("rpm -qa |grep -i aide",shell=True)
p.communicate()
var1 = p.returncode

if ( var1 != 0 ):

os.system ("rpm --ivh aide*")

else:

file = open ("/tmp/linuxout.txt","a")
file.write("aide is already installed ")
file.close

#periodic execution of the file integrity

p = subprocess.Popen("crontab -l |grep -i aide",shell=True)

p.communicate()

var1 = p.returncode

if ( var1 != 0):

os.system ("crontab -l > mycron")

os.system ("echo"0 1 * * * /usr/sbin/aide -check" >> mycron")

os.system ("crontab mycron")

file = open ("/tmp/linuxout.txt","a")
file.write("Periodic execution of the file integrity is configured")
file.close

else:

file = open ("/tmp/linuxout.txt","a")
file.write("Periodic execution of the file integrity is already available")
file.close

#Set user and group owner for grub.conf

os.system ("chown:root:root/etc/grub.conf")

file = open ("/tmp/linuxout.txt","a")
file.write("user and group ownership is set for /etc/grub.conf")
file.close

#Set permission on /etc/groub.conf

os.system ("chmod og-rwx /etc/grub.conf")

file = open ("/tmp/linuxout.txt","a")
file.write ("permission is setted for /etc/grub.conf")
file.close

#set authentication for single user mode

p = subprocess.Popen("cat /etc/inittab|grep -i sulogin",shell=True)
p.communicate()
var1 = p.returncode

if (var1 != 0):

os.system("echo ~:S:wait:/sbin/sulogin >> /etc/inittab")

file = open ("/tmp/linuxout.txt","a")
file.write ("authentication is configured for single user mode")
file.close

else:

file = open ("/tmp/linuxout.txt","a")
file.write ("authentication is already configured for single user mode")
file.close


#Disable interactive boot

p = subprocess.Popen("grep 'PROMPT=no' /etc/sysconfig/init",shell=True)

p.communicate()

var1 = p.returncode

if (var1 == 0):

os.system("sed -i 's/PROMPT=yes/PROMPT=no/g' /etc/sysconfig/init")

file = open ("/tmp/linuxout.txt","a")
file.write ("Disabling interactive boot")
file.close

else:

file = open ("/tmp/linuxout.txt","a")
file.write ("Interactive boot is already disabled")
file.close

#Restrict Core Dumps

p = subprocess.Popen ("grep 'hard core 0'/etc/security/limits.conf",shell=True)
p.communicate()
var1 = p.returncode

p = subprocess.Popen ("grep 'fs.suid.dumpable = 0' /etc/sysctl.conf",shell=True)
p.communicate()
var2 = p.returncode

if ( var1 == 0 ) & ( var2 == 0):


file = open ("/tmp/linuxout.txt","a")
file.write ("core dump is already restricted in this server, no action required")
file.close

else:

os.system("echo * hard core 0 >> /etc/security/limits.conf")
os.system("echo fs.suid.dumpable = 0 >> /etc/sysctl.conf")

file = open ("/tmp/linuxout.txt","a")
file.write ("core dump is restricted in this server")
file.close

# Configure ExecShield

p = subprocess.Popen ("grep 'kernel.exec-shield = 1' /etc/sysctl.conf",shell=True)
p.communicate()

var1 = p.returncode

if ( var1 != 0 ):

os.system("echo kernel.exec-shield = 1 >> /etc/sysctl.conf")
file = open ("/tmp/linuxout.txt","a")
file.write ("execShild is configured ")
file.close

else:

file = open ("/tmp/linuxout.txt","a")
file.write ("execShild is configured already ")
file.close


#Disable Prelink

p = subprocess.Popen ("grep 'PRELINKING=no' /etc/sysconfig/prelink",shell=True)
p.communicate()
var1 = p.returncode

if ( var1 != 0 ):

os.system("sed -i 's/PRELINKING=yes/PRELINKING=no/g'  /etc/sysconfig/prelink")
file = open ("/tmp/linuxout.txt","a")
file.write ("prelink is disabled successfully ")
file.close

else:
file = open ("/tmp/linuxout.txt","a")
file.write ("prelink is already disabled")
file.close


#Remove telnet server

p = subprocess.Popen ("rpm -qa |grep telnet-server",shell=True)
p.communicate()
var1 = p.returncode

tntsev = os.system ("rpm -qa |grep -i telnet-server")

if ( var1 == 0 ):

os.system ("rpm -e $tntsev")
file = open ("/tmp/linuxout.txt","a")
file.write ("removed  telnet server")
file.close

else:

file = open ("/tmp/linuxout.txt","a")
file.write ("telnet server is not installed")
file.close


#remove RSH server

p = subprocess.Popen ("rpm -qa |grep -i rsh-server",shell=True)
p.communicate()
var1 = p.returncode

rshsrv = os.system ("rpm -qa |grep -i rsh-server")

if ( var1 == 0 ):

os.system ("rpm -e $rshserv")
file = open ("/tmp/linuxout.txt","a")
file.write ("removed  rsh server")
file.close

else:

file = open ("/tmp/linuxout.txt","a")
file.write ("rsh server is not installed")
file.close

#Remove NIS client

p = subprocess.Popen ("rpm-qa |grep -i ypbind",shell=True)
p.communicate()
var1 = p.returncode

ypbnd = os.system ("rpm -qa |grep -i ypbind")

if ( var1 == 0 ):

os.system ("rpm -e $ypbnd")
file = open ("/tmp/linuxout.txt","a")
file.write ("removed NIS client")
file.close

else:

file = open ("/tmp/linuxout.txt","a")
file.write ("NIS client is not installed")
file.close

#remove NIS server

p = subprocess.Popen ("rpm-qa |grep -i ypserv",shell=True)
p.communicate()
var1 = p.returncode

ypserv = os.system ("rpm -qa |grep -i ypserv")

if ( var1 == 0 ):

os.system ("rpm -e $ypserv")
file = open ("/tmp/linuxout.txt","a")
file.write ("removed NIS server")
file.close

else:

file = open ("/tmp/linuxout.txt","a")
file.write ("NIS server is not installed")
file.close

#remove TFTP

p = subprocess.Popen ("rpm-qa |grep -i tftp",shell=True)
p.communicate()
var1 = p.returncode

tftp = os.system ("rpm -qa |grep -i tftp")

if ( var1 == 0 ):

os.system ("rpm -e $tftp")
file = open ("/tmp/linuxout.txt","a")
file.write ("removed TFTP server")
file.close

else:

file = open ("/tmp/linuxout.txt","a")
file.write ("TFTP server is not installed")
file.close


#remove talk

p = subprocess.Popen ("rpm-qa |grep -i talk",shell=True)
p.communicate()
var1 = p.returncode

tlk = os.system ("rpm -qa |grep -i talk")

if ( var1 == 0 ):

os.system ("rpm -e $tlk")
file = open ("/tmp/linuxout.txt","a")
file.write ("removed TALK server")
file.close

else:

file = open ("/tmp/linuxout.txt","a")
file.write ("TALK server is not installed")
file.close



#remove Davecot

p = subprocess.Popen ("rpm -qa |grep -i davecot",shell=True)
p.communicate()
var1 = p.returncode

devct = os.system ("rpm -qa |grep -i davecot")

if ( var1 == 0 ):

os.system ("rpm -e $devct -nodeps")
file = open ("/tmp/linuxout.txt","a")
file.write ("removed Davecot ")
file.close

else:

file = open ("/tmp/linuxout.txt","a")
file.write ("Davecot is not installed")
file.close

#remove samba

p = subprocess.Popen ("rpm -qa |grep -i samba",shell=True)
p.communicate()
var1 = p.returncode

smb = os.system ("rpm -qa |grep -i samba")

if ( var1 == 0 ):

os.system ("rpm -e $smb -nodeps")
file = open ("/tmp/linuxout.txt","a")
file.write ("removed samba ")
file.close

else:

file = open ("/tmp/linuxout.txt","a")
file.write ("samba is not installed")
file.close

##########secure network configuration###########

P = subprocess.Popen ("/sbin/sysctl net.ipv4.conf.all.send_redirects |grep 1",shell=True)
p.communicate ()
var1 = p.returncode
if ( var1 != 0):

os.system ("/sbin/sysctl -w net.ipv4.conf.all.send_redirects=0")
os.system ("/sbin/sysctl -w net.ipv4.conf.send_redirects=0")
file = open ("/tmp/linuxout.txt","a")
file.write ("Disabling send redirects")
file.close

else:

file = open ("/tmp/linuxout.txt","a")
file.write ("Send redirects configuration is failed")
file.close

#Enable TCP SYN Cookies

P = subprocess.Popen ("/sbin/sysctl net.ipv4.tcp_syncookies |grep 0",shell=True)
p.communicate ()
var1 = p.returncode
if ( var1 != 0):

os.system ("/sbin/sysctl -w net.ipv4.tcp_syncookies=1")
os.system ("/sbin/sysctl -w net.ipv4.route.flush=1")
file = open ("/tmp/linuxout.txt","a")
file.write ("Enabled TCP SYN cookies")
file.close

else:

file = open ("/tmp/linuxout.txt","a")
file.write ("Enabling TCP SYN cookies is failed")
file.close

#Disable IPv6

p = subprocess.Popen ("grep ipv6 /etc/modprobe.conf",shell=True)
p.communicate ()
var1 = p.returncode
if (var1 != 0):

os.system ("echo options ipv6 "disable=1" >> /etc/modprobe.conf")
file = open ("/tmp/linuxout.txt","a")
file.write ("Disabled ipv6")
file.close

else:

file = open ("/tmp/linuxout.txt","a")
file.write ("ipv6 is not enabled")
file.close

#Secure Logging and Auditing functions
#Configure /etc/syslog.conf

os.system ("echo auth,user.* /var/log/messages >> /etc/syslog.conf")
os.system("echo kern.* /var/log/kern.log >> /etc/syslog.conf")
os.system("echo daemon.* /var/log/daemon.log >> /etc/syslog.conf")
os.system ("echo syslog.* /var/log/syslog >> /etc/syslog.conf")
os.system("echo news,uucp,local0,local1,local2,local3,local4,local5,local6.* /var/log/unused.log >> /etc/syslog.conf")

p = subprocess.Popen ("pkill -HUP syslogd",shell=True)
p.communicate ()
var1 = p.returncode
if (var1 == 0):
file = open ("/tmp/linuxout.txt","a")
file.write ("syslog is configured succesfully")
file.close
else:
file = open ("/tmp/linuxout.txt","a")
file.write ("syslog configuration is failed")
file.close

#Create and Set Permissions on syslog Log Files

log_files = ['messages', 'kern.log', 'daemon.log', 'syslog']

os.system ("cd /var/log")

for i in log_files:

os.system ("chown -R root:root /var/log/$i")
os.system ("chmod og-rwx /var/log/$i")

file = open ("/tmp/linuxout.txt","a")
file.write ("permissions are set for log files")
file.close

#Enable auditd service

os.system ("chkconfig auditd on")

file = open ("/tmp/linuxout.txt","a")
file.write ("auditd service is enabled")
file.close

#configure auditd storage size

p = subprocess.Popen ("grep max_log_file = 5 /etc/audit/auditd.conf",shell=True)
p.communicate ()
var1 = p.returncode

if (var1 != 0):

os.system("sed -i 's/max_log_file = 5/#max_log_file = 5/g' /etc/audit/auditd.conf")
os.system ("echo max_log_file = MB >> /etc/audit/auditd.conf")
file = open ("/tmp/linuxout.txt","a")
file.write ("auditd storage size is configured")
file.close
else:

file = open ("/tmp/linuxout.txt","a")
file.write ("auditd storage size is already configured")
file.close

# Keep All Auditing Information

p = subprocess.Popen ("grep max_log_file_action /etc/audit/auditd.conf",shell=True)
p.communicate ()
var1 = p.returncode

if (var1 != 0):
os.system ("echo max_log_file_action = keep_logs >> /etc/audit/auditd.conf")
file = open ("/tmp/linuxout.txt","a")
file.write ("configure auditlog is successful")
file.close
else:

file = open ("/tmp/linuxout.txt","a")
file.write ("configure auditlog is failed")
file.close

#Record Events That Modify Date and Time Information

os_arch = os.system ("getconf LONG_BIT")

if (os_arch == 32):

os.system ("echo -a always,exit -F arch=b32 -S adjtimex -S settimeofday -S stime -k time-change >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -F arch=b32 -S clock_settime -k time-change >> /etc/audit/audit.rules")
os.system ("echo -w /etc/localtime -p wa -k time-change >> /etc/audit/audit.rules")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("Date & time configuration details are recorded in audit logs")
file.close

else:

os.system ("echo -a always,exit -F arch=b64 -S adjtimex -S settimeofday -k time-change >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -F arch=b32 -S adjtimex -S settimeofday -S stime -k time-change >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -F arch=b64 -S clock_settime -k time-change >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -F arch=b32 -S clock_settime -k time-change >> /etc/audit/audit.rules")
os.system ("echo -w /etc/localtime -p wa -k time-change >> /etc/audit/audit.rules")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("Date & time configuration details are recorded in audit logs")
file.close

#record events that modify usr/group information

os.system ("echo -w /etc/group -p wa -k identity >> /etc/audit/audit.rules")
os.system("echo -w /etc/passwd -p wa -k identity >> /etc/audit/audit.rules")
os.system("echo -w /etc/gshadow -p wa -k identity >> /etc/audit/audit.rules")
os.system ("echo -w /etc/shadow -p wa -k identity >> /etc/audit/audit.rules")
os.system ("echo -w /etc/security/opasswd -p wa -k identity >> /etc/audit/audit.rules")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("User& group modification configuration details are recorded in audit logs")
file.close

#Record Events That Modify the System’s Network Environment

os_arch = os.system ("getconf LONG_BIT")

if (os_arch == 32):

os.system ("echo -a exit,always -F arch=b32 -S sethostname -S setdomainname -k system-locale >> /etc/audit/audit.rules")
os.system ("echo -w /etc/issue -p wa -k system-locale >> /etc/audit/audit.rules")
os.system ("echo -w /etc/issue.net -p wa -k system-locale >> /etc/audit/audit.rules")
os.system ("echo -w /etc/hosts -p wa -k system-locale >> /etc/audit/audit.rules")
os.system ("echo -w /etc/sysconfig/network -p wa -k system-locale >> /etc/audit/audit.rules")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("network configuration changes are recorded in auditlogs from now onwards")
file.close
else:
os.system ("echo -a exit,always -F arch=b64 -S sethostname -S setdomainname -k system-locale >> /etc/audit/audit.rules")
os.system ("echo -a exit,always -F arch=b32 -S sethostname -S setdomainname -k system-locale >> /etc/audit/audit.rules")
os.system ("echo -w /etc/issue -p wa -k system-locale >> /etc/audit/audit.rules")
os.system ("echo -w /etc/issue.net -p wa -k system-locale >> /etc/audit/audit.rules")
os.system ("echo -w /etc/hosts -p wa -k system-locale >> /etc/audit/audit.rules")
os.system ("echo -w /etc/sysconfig/network -p wa -k system-locale >> /etc/audit/audit.rules")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("network configuration changes are recorded in auditlogs from now onwards")
file.close

#Collect Login and Logout Events

p = subprocess.Popen ("grep faillog /etc/audit/audit.rules",shell=True)
p.communicate ()
var1 = p.returncode

if (var1 != 0):
os.system ("echo -w /var/log/faillog -p wa -k logins >> /etc/audit/audit.rules")

p = subprocess.Popen ("grep lastlog /etc/audit/audit.rules",shell=True)
p.communicate ()
var1 = p.returncode

if (var1 != 0):
os.system ("echo -w /var/log/lastlog -p wa -k logins >> /etc/audit/audit.rules")

p = subprocess.Popen ("grep tallylog /etc/audit/audit.rules",shell=True)
p.communicate ()
var1 = p.returncode

if (var1 != 0)
os.system ("echo -w /var/log/tallylog -p -wa -k logins >> /etc/audit/audit.rules")

p = subprocess.Popen ("grep session /etc/audit/audit.rules",shell=True)
p.communicate ()
var1 = p.returncode
if (var1 != 0):
os.system("echo -w /var/log/btmp -p wa -k session >> /etc/audit/audit.rules")

os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("login and logout events are configured in auditlogs from now onwards")
file.close

#Collect Session Initiation Information

os.system ("echo -w /var/run/utmp -p wa -k session >> /etc/audit/audit.rules")
os.system ("echo -w /var/log/wtmp -p wa -k session >> /etc/audit/audit.rule")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("session initiation info are configured in auditlogs from now onwards")
file.close

#Collect Discretionary Access Control Permission Modification Events

os_arch = os.system ("getconf LONG_BIT")

if (os_arch == 32):
os.system ("echo -a always,exit -F arch=b32 -S chmod -S fchmod -S fchmodat -F auid>=500 \ >> /etc/audit/audit.rules")
os.system ("echo -F auid!=4294967295 -k perm_mod >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -F arch=b32 -S chown -S fchown -S fchownat -Slchown -F auid>=500 \ >> /etc/audit/audit.rules")
os.system ("echo -F auid!=4294967295 -k perm_mod >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -F arch=b32 -S setxattr -S lsetxattr -S fsetxattr -S removexattr -S \ >> /etc/audit/audit.rules")
os.system ("echo lremovexattr -S fremovexattr -F auid>=500 -F auid!=4294967295 -k perm_mod >> /etc/audit/audit.rules")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("ACL modification info are configured in auditlogs from now onwards")
file.close

else:

os.system ("echo -a always,exit -F arch=b64 -S chmod -S fchmod -S fchmodat -F auid>=500 \ >> /etc/audit/audit.rules")
os.system ("echo -F auid!=4294967295 -k perm_mod >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -F arch=b32 -S chmod -S fchmod -S fchmodat -F auid>=500 \ >> /etc/audit/audit.rules")
os.system ("echo -F auid!=4294967295 -k perm_mod >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -F arch=b64 -S chown -S fchown -S fchownat -S lchown -F auid>=500 >> /etc/audit/audit.rules")
os.system ("echo -F auid!=4294967295 -k perm_mod >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -F arch=b32 -S chown -S fchown -S fchownat -S lchown -F auid>=500 \ >> /etc/audit/audit.rules")
os.system ("echo -F auid!=4294967295 -k perm_mod >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -F arch=b64 -S setxattr -S lsetxattr -S fsetxattr -S removexattr -S \ >> /etc/audit/audit.rules")
os.system ("echo lremovexattr -S fremovexattr -F auid>=500 -F auid!=4294967295 -k perm_mod >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -F arch=b32 -S setxattr -S lsetxattr -S fsetxattr -S removexattr -S \ >> /etc/audit/audit.rules")
os.system ("echo lremovexattr -S fremovexattr -F auid>=500 -F auid!=4294967295 -k perm_mod >> /etc/audit/audit.rules")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("ACL modification info are configured in auditlogs from now onwards")
file.close

#Collect Unsuccessful Unauthorized Access Attempts to Files

os_arch = os.system ("getconf LONG_BIT")

if (os_arch == 32):
os.system ("echo -a always,exit -F arch=b32 -S creat -S open -S openat -S truncate -S ftruncate \ >> /etc/audit/audit.rules")
os.system ("echo -F exit=-EACCES -F auid>=500 -F auid!=4294967295 -k access >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -F arch=b32 -S creat -S open -S openat -S truncate -S ftruncate \ >> /etc/audit/audit.rules")
os.system ("echo -F exit=-EPERM -F auid>=500 -F auid!=4294967295 -k access >> /etc/audit/audit.rules")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("Collect Unsuccessful Unauthorized Access Attempts to Files is updated ")
file.close

else:

os.system ("echo -a always,exit -F arch=b64 -S creat -S open -S openat -S truncate -S ftruncate \ >> /etc/audit/audit.rules")
os.system ("echo -F exit=-EACCES -F auid>=500 -F auid!=4294967295 -k access >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -F arch=b32 -S creat -S open -S openat -S truncate -S ftruncate \ >> /etc/audit/audit.rules")
os.system ("echo -F exit=-EACCES -F auid>=500 -F auid!=4294967295 -k access >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -F arch=b64 -S creat -S open -S openat -S truncate -S ftruncate \ >> /etc/audit/audit.rules")
os.system ("echo -F exit=-EPERM -F auid>=500 -F auid!=4294967295 -k access >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -F arch=b32 -S creat -S open -S openat -S truncate -S ftruncate \ >> /etc/audit/audit.rules")
os.system ("echo -F exit=-EPERM -F auid>=500 -F auid!=4294967295 -k access >> /etc/audit/audit.rules")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("Collect Unsuccessful Unauthorized Access Attempts to Files is updated ")
file.close

#Collect Successful File System Mounts

os_arch = os.system ("getconf LONG_BIT")

if (os_arch == 32):

os.system ("echo -a always,exit -F arch=b32 -S mount -F auid>=500 -F auid!=4294967295 -k mounts >> /etc/audit/audit.rules")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("Configuration to Collect Successful File System Mounts is configured in audit rules")
file.close

else:

os.system ("echo -a always,exit -F arch=b64 -S mount -F auid>=500 -F auid!=4294967295 -k mounts >> /etc/audit/audit.rules")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("Configuration to Collect Successful File System Mounts is configured in audit rules")
file.close

#Collect File Deletion Events by User

os_arch = os.system ("getconf LONG_BIT")

if (os_arch == 32):

os.system ("echo -a always,exit -F arch=b32 -S unlink -S unlinkat -S rename -S renameat -F auid>=500 \ >> /etc/audit/audit.rules")
os.system ("echo -F auid!=4294967295 -k delete >> /etc/audit/audit.rules")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("Configuration to Collect File Deletion Events by User is configured in audit rules")
file.close

else:

os.system ("echo -a always,exit -F arch=b64 -S unlink -S unlinkat -S rename -S renameat -F auid>=500 \ >> /etc/audit/audit.rules")
os.system ("echo -F auid!=4294967295 -k delete >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -F arch=b32 -S unlink -S unlinkat -S rename -S renameat -F auid>=500 \ >> /etc/audit/audit.rules")
os.system ("echo -F auid!=4294967295 -k delete")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("Configuration to Collect File Deletion Events by User is configured in audit rules")
file.close

#Collect Changes to System Administration Scope (sudoers)

p = subprocess.Popen ("grep sudoers /etc/audit/audit.rules",shell=True)
p.communicate ()
var1 = p.returncode

if (var1 != 0):

os.system ("echo -w /etc/sudoers -p wa -k scope >> /etc/audit/audit.rules")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("Configuration to Collect Changes to System Administration Scope (sudoers) is configured in audit rules")
file.close
else:

file = open ("/tmp/linuxout.txt","a")
file.write ("Configuration to Collect Changes to System Administration Scope (sudoers) is already configured in audit rules")
file.close

#Collect System Administrator Actions

p = subprocess.Popen ("grep sudo.log /etc/audit/audit.rules",shell=True)
p.communicate ()
var1 = p.returncode

if (var1 != 0):

os.system ("echo -w /var/log/sudo.log -p wa -k actions >> /etc/audit/audit.rules")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("Configuration to Collect  System Administration actions configured in audit rules")
file.close
else:

file = open ("/tmp/linuxout.txt","a")
file.write ("Configuration to Collect  System Administration actions already configured in audit rules")
file.close

#Collect Kernel Module Loading and Unloading

p = subprocess.Popen ("grep modules /etc/audit/audit.rules",shell=True)
p.communicate ()
var1 = p.returncode

if (var1 != 0):

os.system ("echo -w /sbin/insmod -p x -k modules >> /etc/audit/audit.rules")
os.system ("echo -w /sbin/rmmod -p x -k modules >> /etc/audit/audit.rules")
os.system ("echo -w /sbin/modprobe -p x -k modules >> /etc/audit/audit.rules")
os.system ("echo -a always,exit -S init_module -S delete_module -k modules >> /etc/audit/audit.rules")
os.system ("pkill -HUP auditd")
file = open ("/tmp/linuxout.txt","a")
file.write ("Configuration to Collect  kernel module loading and unloading is configured in audit rules")
file.close

else:

file = open ("/tmp/linuxout.txt","a")
file.write ("Configuration to Collect  kernel module loading and unloading is already configured in audit rules")
file.close


# Configure logrotate

p = subprocess.Popen ("ls -l /etc/logrotate.d/syslog",shell=True)
p.communicate()
var1 = p.returncode

if (var1 != 0):

os.system ("echo /var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron {daily compress } >> /etc/logrotate.d/syslogd")
file = open ("/tmp/linuxout.txt","a")
file.write ("logrotate is configured succesfully ")
file.close

else:

   file = open ("/tmp/linuxout.txt","a")
file.write ("logrotate is already configured ")
file.close

####Secure System Access, Authentication & Authorization#######

#enable cron in startup

 os.system ("chkconfig cron on")
 file = open ("/tmp/linuxout.txt","a")
 file.write ("enabled cron in startup ")
 file.close

 #set owner and group permission for cron.hourly,cron.daily,cron.monthly

os.system ("chown root:root /etc/cron.hourly")
os.system ("chmod og-rwx /etc/cron.hourly")
os.system ("chown root:root /etc/cron.daily")
os.system ("chmod og-rwx /etc/cron.daily")
os.system ("chown root:root /etc/cron.monthly")
os.system ("chmod og-rwx /etc/cron.monthly")
file = open ("/tmp/linuxout.txt","a")
file.write ("owner and group permission for cron.hourly and cron.daily,cron.monthly is configured")
file.close

#Set User/Group Owner and Permission on /etc/cron.d

os.system ("chown root:root /etc/cron.d")
os.system ("chmod og-rwx /etc/cron.d")
p = subprocess.Popen ("stat -c "%a %u %g" /etc/cron.d | egrep ".00 0 0",shell=True)
p.communicate()
var1 = p.returncode

if (var1 == 0):
file = open ("/tmp/linuxout.txt","a")
file.write (" User/Group Owner and Permission on /etc/cron.d is configured succesfully")
file.close
else:

file = open ("/tmp/linuxout.txt","a")
file.write (" User/Group Owner and Permission on /etc/cron.d is failed")
file.close

#Set SSH Protocol to 2

ssh_port = subprocess.check_output("cat /etc/ssh/ssh_config |grep Protocol |awk '{print $2}',shell=True")
if (ssh_prot == 1):
os.system ("sed -i 's/#Protocol 2,1/Protocol 2/g' /etc/ssh/sshd_config")
file = open ("/tmp/linuxout.txt","a")
file.write ("ssh protocol is succesfully changed from 2,1 to 2")
file.close
else:
os.system ("sed -i 's/Protocol 2,1/Protocol 2/g' /etc/ssh/sshd_config")
file = open ("/tmp/linuxout.txt","a")
file.write ("ssh protocol is changed from 2,1 to 2")
file.close

#Set LogLevel to VERBOSE

ssh_log = subprocess.check_output ("cat /etc/ssh/ssh_config |grep LogLevel |awk '{print $2}',shell=True")

if (ssh_log == INFO):
os.system ("sed -i 's/#LogLevel INFO /LogLevel VERBOSE/g'  /etc/ssh/sshd_config")
file = open ("/tmp/linuxout.txt","a")
file.write ("ssh protocol log level succesfully changed from INFO to VERBOSE")
file.close
else:
file = open ("/tmp/linuxout.txt","a")
file.write ("ssh protocol log level change is failed")
file.close

#Set Permissions on /etc/sshd_config

os.system ("chown root:root /etc/ssh/sshd_config")
os.system ("chmod 644 /etc/ssh/sshd_config")
p = subprocess.Popen ("stat -c "%a %u %g" /etc/ssh/sshd_config | egrep ".00 0 0"",shell=True)
p.communicate()
var1 = p.returncode
if (var1 == 0):
file = open ("/tmp/linuxout.txt","a")
file.write ("ssh protocol permission changed succesfully")
file.close
else:
file = open ("/tmp/linuxout.txt","a")
file.write ("ssh protocol permission change is failed")
file.close

#Set SSH MaxAuthTries to 5 or Less

ssh_tries = subprocess.check_output ("cat /etc/ssh/ssh_config |grep MaxAuthTries |awk '{print $2}',shell=True")

if (ssh_tries == 6):
os.system ("sed -i 's/#MaxAuthTries 6/MaxAuthTries 5/g'  /etc/ssh/sshd_config")
file = open ("/tmp/linuxout.txt","a")
file.write ("ssh protocol max tries are changed to 5 or less")
file.close
else:
file = open ("/tmp/linuxout.txt","a")
file.write ("ssh protocol max tries are change is failed")
file.close

#Disable hostbased authentication in sshd



ssh_host = subprocess.check_output ("cat /etc/ssh/sshd_config |grep "HostbasedAuthentication no" |awk '{print $2}',shell=True")
if (ssh_host == no):
os.system ("sed -i 's/#HostbasedAuthentication no/HostbasedAuthentication no/g'  /etc/ssh/sshd_config")
file = open ("/tmp/linuxout.txt","a")
file.write ("ssh hostbased authentication is disabled succesfully")
file.close
else:
file = open ("/tmp/linuxout.txt","a")
file.write ("ssh hostbased authentication is disable is failed")
file.close

###########Adding alternate user in the system#############

username = raw_input("Please enter username:")
password = raw_input("Please enter password:")
try:
pwd.getpwnam(username)
except KeyError:
     os.system (useradd -m -p $password $username)
else:
     print "User, %s is already exists" %username
continue

#Disable SSH Root Login

ssh_root = subprocess.check_output ("cat /etc/ssh/sshd_config |grep PermitRootLogin |awk '{print $2}',shell=True")
if (ssh_root == yes):
os.system ("sed -i 's/#PermitRootLogin yes/PermitRootLogin no/g'  /etc/ssh/sshd_config")
file = open ("/tmp/linuxout.txt","a")
file.write ("ssh root login is disabled succesfully")
file.close
else:
file = open ("/tmp/linuxout.txt","a")
file.write ("ssh root login is already disabled ")
file.close

#Use Only Approved Ciphers

    os.system ("echo Ciphers aes128-ctr,aes192-ctr,aes256-ctr >> /etc/ssh/sshd_config")
    file = open ("/tmp/linuxout.txt","a")
file.write ("approved ciphers are configured succesfully")
file.close
#configure the banner

    file = open ("/etc/issue","a")
file.write ("""************************************************************NOTICE*** SECURITY POLICY****************************************************************
    WARNING! This is an  secure computer system and may be accessed only by authorized users. These computer systems are provided for business purposes and must be used in an ethical lawful manner. All data contained here is owned by company authority ., and may be monitored, examined, intercepted, blocked, deleted, captured and disclosed in any manner, by authorized personnel. Individuals or groups using this system in excess of their authorization will have all access terminated. Unauthorized use or misuse of this system is strictly prohibited and may be subject to disciplinary action.
    **************************************************************NOTICE*** SECURITY POLICY******************************************************""")
    file.close

p = subprocess.Popen ("grep Banner /etc/ssh/sshd_config",shell=True)
    p.communicate ()
    var1 = p.returncode
if (var1 == 0):
   os.system ("sed -i 's/Banner/#Banner/g' /etc/ssh/sshd_config")
os.system ("echo Banner /etc/issue >> /etc/ssh/sshd_config")
file = open ("/tmp/linuxout.txt","a")
       file.write ("banner is configured succesfully")
       file.close

else:

file = open ("/tmp/linuxout.txt","a")
       file.write ("banner is already configured ")
       file.close

###Configure PAM

p = subprocess.Popen ("grep ^password.*pam_cracklib.so.* /etc/pam.d/system-auth,shell=True")
p.communicate ()
var1 = p.returncode
    if (var1 == 0):
os.system ("sed -i 's/^password.*requisite.*pam_cracklib.so.*/password required pam_cracklib.so try_first_pass retry=3 minlen=14,dcredit=-1,ucredit=-1,ocredit=-1 lcredit=-1/g' /etc/pam.d/system-auth")
file = open ("/tmp/linuxout.txt","a")
file.write ("PAM is configured succesfully")
file.close
##Set Strong Password Creation Policy Using pam_passwdqc

P = subprocess.Popen ("grep ^password.*pam_passwdqc.so.* /etc/pam.d/system-auth,shell=True")
p.communicate ()
var1 = p.returncode
    if (var1 == 0):
os.system ("sed -i 's/^password.*requisite.*pam_passwdqc.so.*/password    requisite     pam_passwdqc.so min=disabled,disabled,16,12,8/g' /etc/pam.d/system-auth")
file = open ("/tmp/linuxout.txt","a")
file.write ("Strong password is configured succesfully in PAM")
file.close

###Upgrade Password Hashing Algorithm to SHA-512

p = subprocess.Popen ("authconfig --test|grep hasing|grep sha512,shell=True")
p.communicate ()
var1 = p.returncode
if (var1 == 0):
file = open ("/tmp/linuxout.txt","a")
file.write ("Password hashing algorithm is already avilable in this system")
file.close
else:
os.system ("authconfig --passalgo=sha512 --update")

###Limit Password Reuse

p = subprocess.Popen ("grep ^password.*pam_unix.so.* /etc/pam.d/system-auth,shell=True")
p.communicate ()
var1 = p.returncode
if (var1 == 0):
os.system ("sed -i 's/password    sufficient    pam_unix.so sha512 shadow nullok try_first_pass use_authtok/password    sufficient    pam_unix.so sha512 shadow nullok try_first_pass use_authtok remember=3/g' /etc/pam.d/system-auth")
file = open ("/tmp/linuxout.txt","a")
file.write ("Password reusing algoritham is configured in the system")
file.close
###Set Password Expiration Days

os.system ("sed -i 's/PASS_MAX_DAYS/#PASS_MAX_DAYS/g' /etc/login.defs")
os.system ("echo PASS_MAX_DAYS 90 >> /etc/login.defs")
os.system ("chage --maxdays 90 omadmin")
file = open ("/tmp/linuxout.txt","a")
file.write ("Password expiration date configured as 90 in the system")
file.close

###Set Password Change Minimum Number of Days

os.system ("sed -i 's/PASS_MIN_DAYS/#PASS_MIN_DAYS/g' /etc/login.defs")
os.system ("echo PASS_MIN_DAYS 7 >> /etc/login.defs")
os.system ("chage --mindays 7 omadmin")
file = open ("/tmp/linuxout.txt","a")
file.write ("Password change minimum number of days are configured as 7 in the system")
file.close

###Set Default Group Account (root)

  os.system ("usermod -g 0 root")
  dfpass = subprocess.check_output ("grep root /etc/passwd | awk -F ":" '{print $4}'")
    if (dfpass == 0):
file = open ("/tmp/linuxout.txt","a")
   file.write ("Default group account is configured for root)
file.close
###Set Warning Banner for Standard Login Services

p = subprocess.Popen ("cat /etc/motd |grep computer system,shell=True")
p.communicate ()
var1 = p.returncode
if (var1 != 0):
os.system ("echo "This is a secure computer system. Authorized uses only. All activity may be monitored and reported." >> /etc/motd")
os.system ("chown root:root /etc/motd")
os.system ("chown root:root /etc/issue")
os.system ("chmod 644 /etc/motd")
os.system ("chmod 644 /etc/issue")
       file = open ("/tmp/linuxout.txt","a")
       file.write ("Warning banner is configured","a")
   file.close