Showing posts with label SysAdmin. Show all posts
Showing posts with label SysAdmin. Show all posts

Tuesday, 3 November 2015

Version Control using git in CentOS7


Version control systems allow you to keep track of your software at the source level. You can track changes, revert to previous stages, and branch off from the base code to create alternative versions of files and directories.
One of the most popular version control systems is git. Many projects maintain their files in a Git repository, and sites like GitHub and Bitbucket have made sharing and contributing to code with Git easier than ever.
In this guide, we will demonstrate how to install Git on a CentOS 7 server.

1) Install git.

[shabbir@compute1 ~]$ sudo yum install git


2) Configure your name and email.

[shabbir@compute1 ~]$ git config --global user.name "shabbir rangwala"
[shabbir@compute1 ~]$ git config --global user.email "shabbir.ahr@gmail.com"

[shabbir@compute1 ~]$ git config --list
user.name=shabbir rangwala
user.email=shabbir.ahr@gmail.com


3) Create your workspace. Create a dir. 'git' in the home directory and subfolders for each of our individual projects. 

[shabbir@compute1 ~]$ mkdir -p ~/git/perl
[shabbir@compute1 ~]$ cd ~/git/perl


4) Copy all project files to the dir. Here we create a test file to use in our repository.

[shabbir@compute1 perl]$ touch test_file.pl


5)  Tell git that you want to use your current directory as a git environment.

[shabbir@compute1 perl]$ git init
Initialized empty Git repository in /home/shabbir/git/perl/.git/


6) Add all files and directories to your newly created repository.

[shabbir@compute1 perl]$ git add .


7) Every time you add or make changes to files, you need to write a commit message. Commit messages explain what your change did.

[shabbir@compute1 perl]$ git commit -m "Initial Commit" -a

the -a signifies that we want our commit message to be applied to all added or modified files. This is okay for the first commit, but generally you should specify the individual files or directories that we want to commit.

To commit an individual file.
[shabbir@compute1 perl]$ git commit -m "Initial Commit" test_file.pl

To add additional files or directories, you just add a space separated list to the end of that command.

8) Create an account on github.com and create a repository 'perl'


9) Configure the remote repository. The name is 'origin' and the URL is 'https://github.com/RShabbir53/perl.git'.

[shabbir@compute1 perl]$ git remote add origin https://github.com/RShabbir53/perl.git

[shabbir@compute1 perl]$ git remote -v
origin    https://github.com/RShabbir53/perl.git (fetch)
origin    https://github.com/RShabbir53/perl.git (push)



10) Push code to the remote server. "git push" tells git that we want to push our changes, "origin" is the name of our newly-configured remote server and "master" is the name of the first branch.
In the future, when you have commits that you want to push to the server, you can simply type "git push".

[shabbir@compute1 perl]$ git push -u origin master


11) In the future, when you have commits that you want to push to the server, you can simply type "git push".

Thursday, 4 June 2015

Create Custom Unit File (to run a Custom Daemon) in systemd in RHEL7


In a previous post we configured Swatch Log Monitoring Tool. In this post we will configure swatch as a custom daemon in 'systemd' by creating a custom unit file in 'systemd' and loading it into 'systemd'.


Perform the following steps:

1) Create a unit file in /etc/systemd/system/ directory.
[root@meru ~]# touch /etc/systemd/system/swatch.service

[root@meru ~]# chmod 664 /etc/systemd/system/swatch.service


2) Edit the Unit File.
[root@meru ~]# vi /etc/systemd/system/swatch.service

[Unit]
Description=Swatch Log Monitoring Daemon
After=syslog.target network.target auditd.service sshd.service

[Service]
ExecStart=/bin/swatch --config-file=/etc/swatch/secure.conf --tail-file=/var/log/secure --pid-file=/var/run/swatch.pid --daemon
ExecStop=kill -s KILL $(cat /var/run/swatch.pid)
Type=forking
PIDFile=/var/run/swatch.pid

[Install]
WantedBy=multi-user.target


Where:
  • Description is an informative description that is displayed in journal log files and in the output of the systemctl status command. 
     
  • After setting ensures that the service is started only after the given space-separated list of services or targets are running.
     
  • ExecStart stands for the path to the actual service executable. 
     
  • Type=forking is used for daemons that make the fork system call. The main process of the service is created with the PID specified in PIDFile. 
     
  • WantedBy states the target or targets that the service should be started under. 


3) Notify systemd about the new swatch.service file.
[root@meru system]# systemctl daemon-reload

4) Start Swatch service
[root@meru system]# systemctl start swatch.service

You can now use all systemctl commands for swatch.service.

Tuesday, 26 May 2015

Convert VMware Virtual Machine to KVM



In this tutorial, we will convert a VMware Virtual disk 'Metasploitable.vmdk' to QCOW2 format and install in KVM.

Follow the following steps:
1) Download the Metasploitable 2 zip file from the Internet.

2) Extract the zip file in a folder.

[root@meru iso]# unzip metasploitable-linux-2.0.0.zip
Archive:  metasploitable-linux-2.0.0.zip
   creating: Metasploitable2-Linux/
  inflating: Metasploitable2-Linux/Metasploitable.nvram 
  inflating: Metasploitable2-Linux/Metasploitable.vmdk 
  inflating: Metasploitable2-Linux/Metasploitable.vmsd 
  inflating: Metasploitable2-Linux/Metasploitable.vmx 
  inflating: Metasploitable2-Linux/Metasploitable.vmxf 


3) Convert 'vmdk' to 'qcow2' format.

[root@meru iso]# qemu-img convert -f vmdk Metasploitable2-Linux/Metasploitable.vmdk -O qcow2 Metasploitable.qcow2


4) Change ownership so that 'libvirt' can read the file.
4.1) Change file owner to 'qemu'. 
[root@meru iso]# chown qemu:qemu Metasploitable.qcow2

4.2) The directory should have execute permission for user 'qemu'
[root@meru ~]# setfacl -m u:qemu:x /iso
                OR
[root@meru ~]# chmod o+x /iso

5) Convert 'vmx' file (VM's settings) to 'xml' file using 'vmware2libvirt'. Download 'vmware2libvirt' from here 

[root@meru iso]# python vmware2libvirt -f Metasploitable2-Linux/Metasploitable.vmx > Metasploitable.xml


6) Use 'virsh' to import into KVM. Unfortunately, in this step, virsh is looking for /usr/bin/kvm and the binary is actually installed as /usr/libexec/qemu-kvm, so we need to make a symlink first.

[root@meru iso]# ln -s /usr/libexec/qemu-kvm /usr/bin/kvm

[root@meru iso]# virsh -c qemu:///system define Metasploitable.xml
Domain Metasploitable2-Linux defined from Metasploitable.xml


7) Locate the new Virtual Machine.

[root@meru iso]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     kali                           shut off
 -     Metasploitable2-Linux          shut off
 -     onion                          shut off
 -     oserver1                       shut off
 -     oserver2                       shut off


8) Edit the config file for the VM and modify entry for the virtual disk file and change file type to 'qcow2'

[root@meru iso]# virsh edit Metasploitable2-Linux

Locate the following lines:

    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/iso/Metasploitable2-Linux/Metasploitable.vmdk'/>
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>


And make the following changes:

    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/iso/Metasploitable.qcow2'/>
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>

Save the changes and exit.



9) The Virtual Machine is now ready. Start the VM.

[root@meru iso]# virsh start Metasploitable2-Linux
Domain Metasploitable2-Linux started


Saturday, 23 May 2015

Configure NTP using Chrony for an isolated network

The 'chrony' package provides 'chronyd' daemon and 'chronyc' command-line utility. The 'chronyd' makes adjustments to the system clock by consulting external time sources, using the NTP protocol.

In this tutorial, we configure Chrony for an isolated network (which is not connected to the Internet). One computer is selected to be the master time server . All other computers are clients of the master.

NTP Server IP Address 192.168.122.1

Configure NTP Server:


1) Install chrony
[root@meru ~]# yum install -y chrony

2) Edit the Server Config file '/etc/chrony.conf' and make the following changes.

#Comment the server entries
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#
server 2.centos.pool.ntp.org iburst
#
server 3.centos.pool.ntp.org iburst

# Allow NTP client access from local network.
allow 192.168/16

#Not synchronized to any external source. (local mode)
local stratum 10

# Allow manual time change using chronyc settime command
manual


3) Open UDP Port 123 in firewall
[root@meru ~]# firewall-cmd --zone=public --add-service=ntp --permanent
[root@meru ~]# firewall-cmd --reload

4) Start the server and enable at boot time
[root@meru ~]# systemctl start chronyd
[root@meru ~]# systemctl enable chronyd

5) Verify Chrony tracking
[root@meru ~]# chronyc tracking
Reference ID    : 127.127.1.1 ()
Stratum         : 10
Ref time (UTC)  : Sat May 23 12:31:27 2015
System time     : 0.000000000 seconds fast of NTP time
Last offset     : 0.000000000 seconds
RMS offset      : 0.000000000 seconds
Frequency       : 8.226 ppm fast
Residual freq   : 0.000 ppm
Skew            : 0.000 ppm
Root delay      : 0.000000 seconds
Root dispersion : 0.000001 seconds
Update interval : 0.0 seconds
Leap status     : Not synchronised

The Reference ID field shows the IP Address or name of the NTP Server to which we are synchronized. The value 127.127.1.1 indicates "local" mode (Not synchronized with any external source).


Configure NTP Client:


1) Install chrony
[root@server2 ~]# yum install -y chrony

2) Edit the Client Config file '/etc/chrony.conf' and make the following changes.

#Comment the server entries
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#
server 2.centos.pool.ntp.org iburst
#
server 3.centos.pool.ntp.org iburst

#Add server entry 
server 192.168.122.1 iburst

 
3) Start the server and enable at boot time
[root@server2 ~]# systemctl start chronyd
[root@server2 ~]# systemctl enable chronyd

4) Verify Chrony tracking
[root@server2 ~]# chronyc tracking
Reference ID    : 192.168.122.1 (meru.mycompany.com)
Stratum         : 11
Ref time (UTC)  : Sat May 23 12:39:06 2015
System time     : 0.000001926 seconds fast of NTP time
Last offset     : 0.000005437 seconds
RMS offset      : 0.000270029 seconds
Frequency       : 0.008 ppm slow
Residual freq   : 0.001 ppm
Skew            : 0.050 ppm
Root delay      : 0.000190 seconds
Root dispersion : 0.000184 seconds
Update interval : 260.7 seconds
Leap status     : Normal

The Reference ID field shows the IP Address or name of the NTP Server to which we are synchronized.

4) Verify Chrony Sources
[root@server2 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* meru.mycompany.com           10   9   377    66  -9368ns[  -18us] +/-  117us


NOTE: To quickly synchronize with the server,
[root@server2 ~]# ntpdate 192.168.122.1


Thursday, 14 May 2015

Configure Postfix as a Gateway for Exchange

In this tutorial, we will configure Postfix as a Mail Gateway for a backend Exchange Server or any other back-end mail server. This configuration provides several advantages such as blocking unwanted traffic, virus-scanning ,spam prevention, reduce load on back-end. 

We will configure a Mail Gateway on 'meru.mycompany.com', so that it relays mail for the domain 'mycompany.com' to a mail server on the internal network 'server1.mycompany.com'


1) Edit the file '/etc/postfix/main.cf'. Make the following changes

    myhostname = meru.mycompany.com
    mydomain = mycompany.com


    #the default domain name to append if @domain is missing
    myorigin = $mydomain

    # the network interface addresses on which to receive incoming mail.
    inet_interfaces = all
    # remember to comment this line
    #inet_interfaces = localhost

    #this machine is not the final destination for any domain
    mydestination = 

   #turn off local recipient checking
   local_recipient_maps =



    #Trusted SMTP clients - can relay mail through this server.  
   mynetworks = 192.168.122.0/24, 127.0.0.0/8

    # Relay mail for the domains listed below
    relay_domains = mycompany.com

    # Accept mail only for relay users listed in relay_recipients file
    relay_recipient_maps = hash:/etc/postfix/relay_recipients

    # Where to relay mail
    transport_maps = hash:/etc/postfix/transport


2) Create the file  '/etc/postfix/relay_recipients' containing the list of mail recipients for the domain 'mycompany.com'
shabbir@mycompany.com    x
ali@mycompany.com            x
batul@mycompany.com        x


3) Recreate the relay map
[root@meru ~]# postmap /etc/postfix/relay_recipients

4) Edit the file '/etc/postfix/transport' and add the name of the back-end mail server to which to relay mail for the domain 'mycompany.com'.
mycompany.com  smtp:[server1.mycompany.com]

5) Recreate the transport map.
[root@meru ~]# postmap /etc/postfix/transport

6) To view the parameters changed run the following command.
     postconf -n

7) To check for systax errors, run the following command.
     postfix check

8) Open firewall port 25/TCP.
    firewall-cmd --zone=public --add-service=smtp --permanent
    firewall-cmd --reload

9) Restart postfix service
    systemctl restart postfix


Monday, 11 May 2015

RAID with LVM Configuration in RHEL7


In this tutorial, we will create RAID Level 5 device using 3 disks. On top of this RAID array, we will create a LVM partition. RAID with LVM configuration gives us several advantages over a traditional system:

1) RAID 5 gives fault tolerance. If one of our disks fails, our data remains intact.  
2) LVM gives us resizable volumes (partitions).
3) More disk space can be easily be added without the need to recreate any partition. Disks can be added to the RAID array and the LVM volume group can be extended. 

For basic RAID configuration, refer to this post . And for basic LVM configuration, refer to this post

Consider 3 disks,  /dev/sda, /dev/sdb, /dev/sdc each of size 1Gb. We will assemble these 3 disks into 1 logical RAID 5 device. We will add this RAID 5 device to a Volume Group. From the volume group, we will create a logical volume.

1) Create partitions /dev/sda1, /dev/sdb1, /dev/sdc1 on the devices  /dev/sda, /dev/sdb, /dev/sdc respectively.

2) Build a RAID 5 array '/dev/md0' with the above 3 partitions.
[root@server3 ~]# mdadm --create /dev/md0 --level=5 --raid-devices=3 /dev/sda1 /dev/sdb1 /dev/sdc1
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.


View Status
[root@server3 ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdc1[3] sdb1[1] sda1[0]
      2093056 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]
unused devices: <none>

2) Initialize the RAID device as physical volume.
[root@server3 ~]# pvcreate /dev/md0
  Physical volume "/dev/md0" successfully created

3) Create a volume group consisting of the  physical volume initialized above.
[root@server3 ~]# vgcreate vg_linux /dev/md0
  Volume group "vg_linux" successfully created

The name of the volume group is 'vg_linux' and size is 2Gb as shown below.
[root@server3 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_linux
  Format                lvm2
  VG Access             read/write
  VG Status             resizable
  VG Size               1.99 GiB
  PE Size               4.00 MiB
  Total PE              510
  Alloc PE / Size       0 / 0  
  Free  PE / Size       510 / 1.99 GiB
  VG UUID               6tQeK2-NRmX-gaUb-jcfi-hlvR-2g6r-VxHk4P

4) Create a logical volume of size 1Gb, which you can mount and actually use.
[root@server3 ~]# lvcreate -L 1G -n lv_data vg_linux
  Logical volume "lv_data" created
 
The logical volume can now be accessed as '/dev/vg_linux/lv_data'.

5) Create filesystem on the logical volume 'lv_data'.
[root@server3 ~]# mkfs -t xfs /dev/vg_linux/lv_data

6) Create mount point
[root@server3 ~]# mkdir /var/data

7) Mount the logical volume 'lv_data' and create files in it.
[root@server3 ~]# mount -t xfs /dev/vg_linux/lv_data  /var/data
[root@server3 ~]# touch /var/data/foo{1,2,3}
[root@server3 ~]# ls /var/data/
foo1  foo2  foo3

8) Add the following entry in '/etc/fstab' (to automatically mount the logical volume at boot time).
          /dev/vg_linux/lv_data            /var/data         xfs           defaults        1  2

Configure LVM (Logical Volume Management) in RHEL7


The physical disks, or disk partitions, such as /dev/sda or /dev/sda1 are called "physical volumes". Using LVM, we can combine multiple "physical volumes" into a "volume group". 

The size of a "volume group" is the combined size of all the constituent "physical volumes". For example, 4x10Gb drives can be combined into one 40Gb volume group. "Physical Volumes" can also be added on demand to increase the size of the "volume group".

The total capacity of a volume group can then be allocated to "logical volumes", which are accessed as regular block devices. For example, in a 40Gb volume group, we can create two 20Gb logical volumes. It is the "logical volumes" on which we create a file system and mount for data storage.

Thus, LVM Configuration consists of the following 3 steps:
1) Initialize physical volumes
2) Add the physical volumes to a volume group
3) Create logical volumes from the volume group.

Consider 2 disks '/dev/sda' and  '/dev/sdb' of size 1Gb each. We will combine these 2 disks into a "volume group", which will be of size 2Gb.

From this "volume group", we will create a logical volume of size 1Gb. We will create 'xfs' filesystem on the logical volume, and mount it to store files.

We will add another disk '/dev/sdc' of size '2Gb' to the volume group. The size of the volume group will now be 4Gb.

We will then increase the size of the "logical volume" to 3Gb.

Perform the following steps:

1) Create partition '/dev/sda1' on disk '/dev/sda' and '/dev/sdb1' on disk '/dev/sdb' using 'fdisk' or 'parted'.
    NOTE: Although you can directly initialize a disk as a physical volume, it is recommended that you first create a partition on the disk, and then initialize the partition as a physical volume.

2) Initialize the partitions as physical volumes.
[root@server3 ~]# pvcreate /dev/sda1
  Physical volume "/dev/sda1" successfully created

[root@server3 ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created


3) Create a volume group consisting of the  physical volumes initialized above.
[root@server3 ~]# vgcreate vg_linux /dev/sda1 /dev/sdb1
  Volume group "vg_linux" successfully created

The name of the volume group is 'vg_linux' and size is 2Gb (the combined size of both the physical volumes) as shown below.
[root@server3 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_linux
  Format                lvm2
  VG Access             read/write
  VG Status             resizable
  VG Size               1.99 GiB
  PE Size               4.00 MiB
  Total PE              510
  Alloc PE / Size       0 / 0  
  Free  PE / Size       510 / 1.99 GiB
  VG UUID               7e3JSb-YXhr-LLru-vZBr-qsM1-qqxi-bHRb0p


4) Create a logical volume of size 1Gb, which you can mount and actually use.
[root@server3 ~]# lvcreate -L 1G -n lv_data vg_linux
  Logical volume "lv_data" created
 
The logical volume can now be accessed as '/dev/vg_linux/lv_data'.

We can see that from the total size of the volume group, 1Gb has been allocated and 1Gb is free, as shown below.
[root@server3 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_linux
  Format                lvm2
  VG Access             read/write
  VG Status             resizable
  VG Size               1.99 GiB
  PE Size               4.00 MiB
  Total PE              510
  Alloc PE / Size       256 / 1.00 GiB
  Free  PE / Size       254 / 1016.00 MiB
  VG UUID               7e3JSb-YXhr-LLru-vZBr-qsM1-qqxi-bHRb0p


4) Create filesystem on the logical volume 'lv_data'.
[root@server3 ~]# mkfs -t xfs /dev/vg_linux/lv_data

5) Create mount point
[root@server3 ~]# mkdir /var/data

6) Mount the logical volume 'lv_data' and create files in it.
[root@server3 ~]# mount -t xfs /dev/vg_linux/lv_data  /var/data
[root@server3 ~]# touch /var/data/foo{1,2,3}
[root@server3 ~]# ls /var/data/
foo1  foo2  foo3

7) Add the following entry in '/etc/fstab' (to automatically mount the logical volume at boot time).
          /dev/vg_linux/lv_data            /var/data         xfs           defaults        1  2


Increase the size of the Volume Group (VG) 
Consider disk '/dev/sdc' of size 2Gb, which we will add to the volume group

1) Create partition '/dev/sdc1' on disk '/dev/sdc' using 'fdisk' or 'parted'.
    NOTE: Although you can directly initialize a disk as a physical volume, it is recommended that you first create a partition on the disk, and then initialize the partition as a physical volume.

2) Initialize the partition as physical volume.
[root@server3 ~]# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created

3) Add the physical volume to the volume group
[root@server3 ~]# vgextend vg_linux /dev/sdc1
  Volume group "vg_linux" successfully extended

The size of the volume group has now been increased to 4Gb, of which 3Gb is free, as shown below.
[root@server3 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_linux
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  VG Size               3.99 GiB
  PE Size               4.00 MiB
  Total PE              1021
  Alloc PE / Size       256 / 1.00 GiB
  Free  PE / Size       765 / 2.99 GiB
  VG UUID               7e3JSb-YXhr-LLru-vZBr-qsM1-qqxi-bHRb0p


Extend the size of Logical Volume(LV) 

Consider logical volume '/dev/vg_linux/lv_data' mounted on '/var/data'
1) Unmount the logical volume
[root@server3 ~]# umount /dev/vg_linux/lv_data

2) Increase the size of the logical volume to 3Gb.
[root@server3 ~]#  lvextend -L 3G /dev/vg_linux/lv_data
  Extending logical volume lv_data to 3.00 GiB
  Logical volume lv_data successfully resized

3) Mount the logical volume
[root@server3 ~]# mount -t xfs /dev/vg_linux/lv_data  /var/data

4) Resize the filesystem.(XFS filesystem)
[root@server3 ~]# xfs_growfs /dev/vg_linux/lv_data

5) Confirm the new size.
[root@server3 ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/vg_linux-lv_data  3.0G   33M  3.0G   2% /var/data


Delete a Logical Volume(LV)  1) You can delete a logical volume as follows:
[root@server3 ~]# lvremove /dev/vg_linux/lv_data
Do you really want to remove active logical volume lv_data? [y/n]: y
  Logical volume "lv_data" successfully removed

Friday, 8 May 2015

Configuring Software RAID in RHEL7

RAID (Redundant Array of Independent Disks) is a system that uses multiple hard drives to distribute or replicate data across several disks.

RAID can do 2 basic things:
First, it can improve performance by "striping" data across multiple drives, thus allowing several drives to work simultaneously.
Second, it can "mirror" data across multiple drives, decreasing the risk associated with a single failed disk. 

RAID has several levels.
RAID 0 : Striped Set with no fault tolerance. Data is striped across all the disks in the RAID array.

RAID 1: Disk Mirroring. Data is mirrored to provide fault tolerance.

RAID 1+0 and RAID 0+1 : Combines the performance benefits of RAID 0 with the redundancy benefits of RAID 1. They use stripping and mirroring.

RAID 3: Striped set with parallel disk access and a dedicated parity disk.

RAID 4: Striped set with independent disk access and a dedicated parity disk.

RAID 5: Striped set with independent disk access and a distributed parity.

RAID 6: Striped set with independent disk access and a dual distributed parity to enable survival if two disk failure occur.
 
 In this tutorial, we will create Level 5 RAID device using 3 disks. RAID 5 strips data for performance and uses parity for fault tolerance. The drives (strips) are independently accessible. Parity is distributed across all disks to overcome the write bottleneck of a dedicated parity disk.


Consider the partitions /dev/sda1, /dev/sda2, /dev/sda3. We will assemble these 3 partitions into 1 logical RAID Level 5 partition.

1) Build a RAID 5 array '/dev/md0' with the above 3 partitions.
[root@oserver1 ~]# mdadm --create /dev/md0 --level=5 --raid-devices=3 /dev/sda1 /dev/sda2 /dev/sda3
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.


2) View Status
[root@oserver1 ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sda3[3] sda2[1] sda1[0]
      203776 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]
unused devices: <none>

3) Create 'xfs' filesystem on the RAID device '/dev/md0'
[root@oserver1 ~]# mkfs -t xfs /dev/md0
log stripe unit (524288 bytes) is too large (maximum is 256KiB)
log stripe unit adjusted to 32KiB
meta-data=/dev/md0               isize=256    agcount=8, agsize=6272 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=50176, imaxpct=25
         =                       sunit=128    swidth=256 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=624, version=2
         =                       sectsz=512   sunit=8 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

[root@oserver1 ~]# blkid /dev/md0
/dev/md0: UUID="89aa5617-4f4e-4345-8dbe-22a7af46dbe6" TYPE="xfs"

4) Mount the RAID device and create files in it.
[root@oserver1 ~]# mount /dev/md0 /mnt
[root@oserver1 ~]# touch /mnt/foo{1,2,3}
[root@oserver1 ~]# ls /mnt
foo1  foo2  foo3

5) Preserving the configuration.
[root@oserver1 ~]# mdadm --detail /dev/md0 > /etc/mdadm.conf

At boot time, the 'mdmonitor' service reads the contents of the '/etc/mdadm.conf' file to see which RAID devices to start.

6) To mount automatically at boot time, add entry to '/etc/fstab'
    /dev/md0        /mnt        xfs        defaults      0  2

Simulating a failed disk

1) Simulate a failed disk using mdadm
[root@oserver1 ~]# mdadm /dev/md0 --fail /dev/sda1
mdadm: set /dev/sda1 faulty in /dev/md0

2) View Status
[root@oserver1 ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sda3[3] sda2[1] sda1[0](F)
      203776 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/2] [_UU]
unused devices: <none>

3) Verify that the RAID device is working even when one disk has failed
[root@oserver1 ~]# umount /mnt
[root@oserver1 ~]# mount /dev/md0 /mnt
[root@oserver1 ~]# ls /mnt
foo1  foo2  foo3

4) Remove the failed partition.
[root@oserver1 ~]# mdadm /dev/md0 -r /dev/sda1
mdadm: hot removed /dev/sda1 from /dev/md0

5) View Status
[root@oserver1 ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sda3[3] sda2[1]
      203776 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/2] [_UU]
unused devices: <none>

6) Add the partition back to the RAID array.
[root@oserver1 ~]# mdadm /dev/md0 -a /dev/sda1
mdadm: added /dev/sda1

7) View Status
[root@oserver1 ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sda1[4] sda3[3] sda2[1]
      203776 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]
unused devices: <none>

Monday, 27 April 2015

Add RHEL7 Server to Active Directory Domain



This tutorial is based on the following configuration:
domain name : mycompany.com
workgroup : MYCOMPANY
kerberos realm : MYCOMPANY.COM

Windows Server DNS Name: winserver.mycompany.com
Windows Server IP Address: 192.168.122.10

Linux Server DNS Name: server3.mycompany.com
Linux Server IP Address: 192.168.122.4

Ensure that DNS Server is properly configured on the Windows Server.

1) Install packages
yum install krb5-workstation pam_krb5

yum install samba samba-client samba-winbind

yum install authconfig



2) Ensure that the clocks on both systems are in sync. Time synchronization is essential for Kerberos to work.

3) Configure the DNS Service to use AD as its name server. DNS is critical for proper resolution of host names and domains for kerberos.
Edit the file '/etc/resolv.conf' and add the following entries:
search mycompany.com

nameserver 192.168.122.10


4) Configure Kerberos to use AD Kerberos realm. Edit the file '/etc/krb5.conf'.
[logging]
default = FILE:/var/log/krb5libs.log
kdc = FILE:/var/log/krb5kdc.log
admin_server = FILE:/var/log/kadmind.log

[libdefaults]
dns_lookup_realm = true
dns_lookup_kdc = true

ticket_lifetime = 24h
renew_lifetime = 7d
forwardable = true
rdns = false
default_realm = MYCOMPANY.COM

5) Verify Kerberos operation.

[root@server3 ~]# kinit Administrator
Password for Administrator@MYCOMPANY.COM:
[root@server3 ~]# klist
Ticket cache: FILE:/tmp/krb5cc_0
Default principal: Administrator@MYCOMPANY.COM

Valid starting Expires Service principal
04/27/2015 00:42:19 04/27/2015 10:42:19 krbtgt/MYCOMPANY.COM@MYCOMPANY.COM
renew until 05/04/2015 00:42:10
[root@server3 ~]# kdestroy

6) Configure Samba to connect to AD server. Edit the file '/etc/samba/smb.conf' and make the
following changes:

workgroup = MYCOMPANY

server string = Samba Server Version %v



netbios name = SERVER3



interfaces = lo eth0 192.168.122.4/24

hosts allow = 127. 192.168.122.



security = ads

passdb backend = tdbsam

realm = MYCOMPANY.COM



kerberos method = secrets and keytab



template shell = /bin/sh

winbind offline logon = true



winbind separator = +

winbind use default domain = yes



idmap uid = 10000-19999

idmap gid = 10000-19999

idmap config MYCOMPANY:backend = rid

idmap config MYCOMPANY:range = 10000000-19999999



7) Check for configuration errors
testparm

8) Configure NSS and PAM to use winbind
authconfig –enablewinbind –enablewins –enablewinbindauth --update

9) Start services
systemctl start smb
systemctl start winbind

10) Add the linux machine to the AD Domain
[root@server3 ~]# kinit Administrator

[root@server3 ~]# net ads join -U Administrator
Enter Administrator's password:
Using short domain name -- MYCOMPANY
Joined 'SERVER3' to dns domain 'mycompany.com'

10) Verify AD Server status

[root@server3 ~]# klist -k
Keytab name: FILE:/etc/krb5.keytab
KVNO Principal
---- --------------------------------------------------------------------------
3 SERVER3$@MYCOMPANY.COM

[root@server3 ~]# net ads info
LDAP server: 192.168.122.10
LDAP server name: WINSERVER.mycompany.com
Realm: MYCOMPANY.COM
Bind Path: dc=MYCOMPANY,dc=COM
LDAP port: 389
Server time: Mon, 27 Apr 2015 21:51:54 IST
KDC server: 192.168.122.10
Server time offset: 19835

Thursday, 23 April 2015

Postfix SMTP AUTH and STARTTLS in RHEL7

SMTP AUTH:
The problem with SMTP protocol is that it does not require authentication. So anyone can send mail through your email server. This has lead to the problem of spam. 

To prevent an attacker from using your mail server to relay their spam, an email server is generally configured to relay mail only from the local site's address range. This creates a problem for roaming users. The solution is SMTP AUTH , which requires mail senders to authenticate before submitting messages.

SMTP AUTH is used to allow roaming users to relay mail through a server safely without configuring the SMTP server to be an open relay. Postfix's SMTP AUTH uses an authentication library called SASL, which is not part of Postfix itself. Postfix can use either the Cyrus library or Dovecot as a source for SASL authentication. This tutorial uses Dovecot as a source for SASL authentication.

SMTP STARTTLS:
Normally SMTP is transmitted as cleartext over the wire, making it vulnerable to both passive sniffing and active alteration via man-in-the-middle attacks. SMTP STARTTLS, which compliant ESMTP clients and servers can use to encrypt the SMTP session.

Typically, this is done by first starting TLS, to encrypt the SMTP session, and then issuing the SMTP AUTH command, to authenticate the client; this combination ensures that the username and password transferred as part of the SMTP AUTH are protected by the TLS encrypted session.
 
NOTE:
- Ensure that DNS Server is configured for the network.
- Ensure MX record entry for the domain is made in the DNS Server zone file.
- Ensure reverse dns entry for the mail server is made in the DNS Server zone file.
- To check for errors, view the log file '/var/log/maillog'

This tutorial is based on the following configuration:
- Domain Name: mycompany.com
- Email Gateway Name: meru.mycompany.com
- Email Gateway IP address: 192.168.122.1
- Network served by the mail gateway: 192.168.122.0/24

IMPORTANT: This tutorial assumes that 'dovecot' has been installed and configured. For dovecot installation and configuration refer to this post.

This tutorial is divided into 2 parts:
PART 1) Configuration of SMTP AUTH
PART 2) Configuration of STARTTLS

PART 1) Configuration of SMTP AUTH


Postfix Incoming/Outgoing Server (Gateway) Configuration: This server will receive incoming mail for the domain. And relay outgoing mail from client machines.

1) Edit the file '/etc/postfix/main.cf'. Make the following changes

    myhostname = meru.mycompany.com
    mydomain = mycompany.com


    #the default domain name to append if @domain is missing
    myorigin = $mydomain

    # the network interface addresses on which to receive incoming mail.
    inet_interfaces = all
    # remember to comment this line
   #inet_interfaces = localhost

    #the domains for which this machine is the final destination.
    mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain


   #Trusted SMTP clients - can relay mail through this server.  
    mynetworks = 192.168.122.0/24, 127.0.0.0/8


    #enable SMTP AUTH through SASL
     smtpd_sasl_auth_enable = yes

    #tells SASL to use dovecot for authentication
    smtpd_sasl_type = dovecot

    #the sasl path
    smtpd_sasl_path = private/auth

    #prevent anonymous authentications
    smtpd_sasl_security_options = noanonymous

    #allow authentication from non standard clients such as Microsoft Outlook
    broken_sasl_auth_clients = yes

    #allow authenticated users, allow networks configured with the mynetworks directive, reject destinations other than the postfix server
    smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination


2) To view the parameters changed run the following command.
     postconf -n

3) To check for systax errors, run the following command.
     postfix check

4) Open firewall port 25/TCP.
    firewall-cmd --zone=public --add-service=smtp --permanent
    firewall-cmd --reload

5) Edit the file  '/etc/dovecot/conf.d/10-master.conf' and make the following changes. This is the socket through which postfix communicates with dovecot for the authentication info.
    service auth {
      unix_listener /var/spool/postfix/private/auth {
       group = postfix
       mode = 0660
       user = postfix
     }
    }


6) Restart postfix service
    systemctl restart postfix 

7) Restart dovecot  
     systemctl restart dovecot

 

PART 2) Configuration of STARTTLS

1) Create Server Certificate. Follow the following steps:
    1.1) Create local CA (Certificate Authority)
            /etc/pki/tls/misc/CA  -newca

    1.2) Create public-private key pair
          openssl genrsa -out postfixkey.pem

    1.3) Create a certificate signing request (CSR)
          openssl req -new -key postfixkey.pem -out postfix.csr

    1.4) Sign the Certificate.
          openssl ca -in postfix.csr -out postfixcert.pem
   1.5) Copy the CA cert file '/etc/pki/CA/cacert.pem'.


2) The CA certificate file 'cacert.pem', Server Certificate file 'postfixcert.pem' and Server key file 'postfixkey.pem' is copied to the '/etc/postfix/certs/' directory.

3) Edit the file '/etc/postfix/main.cf'. Make the following changes

       #Certificate file of the CA who has signed the server certificate
       smtpd_tls_CAfile = /etc/postfix/certs/cacert.pem

       #Server certificate 
       smtpd_tls_cert_file = /etc/postfix/certs/postfixcert.pem

       #Server private key
       smtpd_tls_key_file = /etc/postfix/certs/postfixkey.pem

       #not to require STARTTLS for all SMTP exchanges
       smtpd_tls_security_level = may

       #require STARTTLS for SMTP AUTH.
       smtpd_tls_auth_only = yes

2) To view the parameters changed run the following command.
     postconf -n

3) To check for systax errors, run the following command.
     postfix check


4) Restart postfix service
    systemctl restart postfix



Wednesday, 22 April 2015

POP, IMAP using Dovecot in RHEL7

POP and IMAP are 2 protocols to retrieve mail from a mail server.

POP is best for users who have one system on which to read email. Email clients generally delete messages from the server, after downloading. However, this setting can be changed. Also, the entire content of each message is downloaded.

IMAP is best for users who access their email using multiple machines. Email messages remain on the server, where users can read or delete them. IMAP also allows client apps to create mail dir(s) on the server. Also, only email header information is downloaded for messages, until opened.

Follow the following steps:

1) Install packages
     yum -y install dovecot
     yum -y install mutt

2) Edit the file '/etc/dovecot/dovecot.conf' and make the following changes.
     protocols = imap pop3 

3) Edit the file '/etc/dovecot/conf.d/10-mail.conf' and make the following changes.
     mail_location = mbox:~/mail:INBOX=/var/mail/%u

4) Edit the file '/etc/dovecot/conf.d/10-auth.conf' and make the following changes.
     auth_mechanisms = plain login

5) Change permission for mail spool file.
     cd /var/spool/mail
     chmod 600 *

6) Create new Dovecot self-signed certificate (Optional)  
    6.1)Edit the file '/etc/pki/dovecot/dovecot-openssl.cnf' and enter server hostname and other details.

    6.2)Delete default certificate and private key
           rm /etc/pki/dovecot/private/dovecot.pem
           rm /etc/pki/dovecot/certs/dovecot.pem


    6.3)Run the following script to generate new certificate.
           /usr/libexec/dovecot/mkcert.sh

    6.4)Edit in file '/etc/dovecot/conf.d/10-ssl.conf'
           ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
           ssl_key = </etc/pki/dovecot/private/dovecot.pem


7) Verify parameters changed in config files.
        doveconf  -n 

8) Start the service.
      systemctl start dovecot     
      systemctl enable dovecot

9) Open Firewall ports.(POP3-110/tcp,IMAP-143/tcp, POP3S-995/tcp, IMAPS-993/tcp)
      firewall-cmd --zone=public --add-service=pop3s --permanent
      firewall-cmd --zone=public --add-service=imaps --permanent
      firewall-cmd --zone=public --add-port=110/tcp --permanent
      firewall-cmd --zone=public --add-port=143/tcp --permanent

10) Check the connection
      mutt -f pops://meru.mycompany.com

Tuesday, 21 April 2015

Postfix Server for a domain in RHEL7/CentOS7

In this tutorial, we will configure an email server (gateway) for a domain 'mycompany.com'. This central email gateway will receive all mail for the domain. And also, send all mail originating from the internal network. In other words, it will act as a single entry/exit point for all incoming and outgoing mail.

Client machines in the network will relay outgoing mail through the mail gateway. And client machines can read incoming mail from the mail gateway by using POP or IMAP. 

NOTE:
- Ensure that DNS Server is configured for the network.
- Ensure MX record entry for the domain is made in the DNS Server zone file.
- Ensure reverse dns entry for the mail server is made in the DNS Server zone file. (Not necessary for this tutorial)
- To check for errors, view the log file '/var/log/maillog'

This tutorial is based on the following configuration:
- Domain Name: mycompany.com
- Email Gateway Name: meru.mycompany.com
- Email Gateway IP address: 192.168.122.1
- Network served by the mail gateway: 192.168.122.0/24

Postfix Incoming/Outgoing Server (Gateway) Configuration: This server will receive incoming mail for the domain. And relay outgoing mail from client machines.

1) Edit the file '/etc/postfix/main.cf'. Make the following changes

    myhostname = meru.mycompany.com
    mydomain = mycompany.com


    #the default domain name to append if @domain is missing
    myorigin = $mydomain

    # the network interface addresses on which to receive incoming mail.
    inet_interfaces = all
    # remember to comment this line
   #inet_interfaces = localhost

    #the domains for which this machine is the final destination.
    mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain


   #Trusted SMTP clients - can relay mail through this server.  
    mynetworks = 192.168.122.0/24, 127.0.0.0/8

2) To view the parameters changed run the following command.
     postconf -n

3) To check for systax errors, run the following command.
     postfix check

4) Open firewall port 25/TCP.
    firewall-cmd --zone=public --add-service=smtp --permanent
    firewall-cmd --reload

5) Restart postfix service
    systemctl restart postfix


Postfix Client Configuration(Submission-only mode): These machines will not receive mail over the network. And will relay outgoing mail through the Postfix Server configured above.

1) Edit the file '/etc/postfix/main.cf'. Make the following changes

    myhostname = meru.mycompany.com
    mydomain = mycompany.com

    #the default domain name to append if @domain is missing
    myorigin = $myhostname

    # the network interface addresses on which to receive incoming mail.
    inet_interfaces = localhost


    #the domains for which this machine is the final destination.
    mydestination = localhost

    #the mail gateway to send mail to.(The server configured above)
     relayhost = [192.168.122.1]     

2) To view the parameters changed run the following command.
     postconf -n

3) To check for systax errors, run the following command.
     postfix check

4) Open firewall port 25/TCP.
    firewall-cmd --zone=public --add-service=smtp --permanent
    firewall-cmd --reload

5) Restart postfix service
    systemctl restart postfix

Postfix Server for a Stand-alone host in RHEL7

In this tutorial, we configure Postfix Email Server for a stand-alone host, which can send and receive mail.

NOTE:
- Ensure that DNS Server is configured for the network.
- To check for errors, view the log file 'var/log/maillog'

1) Edit the file '/etc/postfix/main.cf'. Make the following changes

    #the hostname of the machine
    myhostname = meru.mycompany.com
 

    # the domain name
    mydomain = mycompany.com  

    #the default domain name to append if @domain is missing
    myorigin = $myhostname

    # the network interface addresses on which to receive incoming mail.
    inet_interfaces = all

    # remember to comment this line
    #inet_interfaces = localhost

    #the domains for which this machine is the final destination.
    mydestination = $myhostname, localhost.$mydomain, localhost

2) To view the parameters changed run the following command.
     postconf -n

3) To check for systax errors, run the following command.
     postfix check

4) Open firewall port 25/TCP.
    firewall-cmd --zone=public --add-service=smtp --permanent
    firewall-cmd --reload

5) Restart postfix service
    systemctl restart postfix

Wednesday, 15 April 2015

Linux & Windows File Sharing Using Samba in RHEL7/CentOS7

This tutorial has the following configuration:
- Samba Server IP Address:    192.168.122.1
- Samba Server Netbios Name: MERUBHAI

- Windows 7 IP Address:         192.168.122.156
- Windows 7 Netbios Name:    SHABBIR-PC

- Windows Workgroup Name: WORKGROUP
- The user account 'shabbir' exists on both linux and windows machines. This account is used for file sharing.


Samba Server Configuration:

NOTE:1) In the Samba config file '/etc/samba/smb.conf', lines starting with ';' or '#' are comments.
           2) All commands are to be run as 'root' user
           3) It is recommended to have the same username & password on both linux and windows machines.

1) Install Samba Server
      yum -y install samba samba-client

2) Edit the Samba config file '/etc/samba/smb.conf'. Make the following changes.

      workgroup = WORKGROUP     /'name of Windows workgroup'
      netbios name = MERUBHAI   /' name of server'
      interfaces = lo eth0 192.168.122.1/24    /'the network interfaces of the server'
      hosts allow = 127. 192.168.122.     /'hosts which are allowed to access the server

     NOTE: Remember to uncomment the above lines by removing the leading ';'

3) Check for config errors
       testparm

4) Create user account
       useradd shabbir
      passwd shabbir

5) Create user in Samba database.
       smbpasswd -a shabbir
       
6) Enable SELinux boolean to allow Samba to access the home directory.
       setsebool -P samba_enable_home_dirs on

7) Create the following public share
       [public]
        comment = Files shared by shabbir
        path = /var/pub/files
        public = yes
        writeable = yes
        printable = no


8) Create directory and create files in the dir.
        mkdir -p /var/pub/files
        touch /var/pub/files/foo{1,2,3,4,5}
        chown -R shabbir:shabbir /var/pub/files

9) Set SELinux file context for the shared directory
       chcon -R -t samba_share_t /var/pub/files

10) Open Firewall Ports.( 137/udp, 138/udp, 139/tcp, 445/tcp)
      firewall-cmd --zone=public --add-service=samba --permanent
      firewall-cmd --reload

11) Start Samba Service and enable on boot
       systemctl start smb
       systemctl start nmb

      systemctl enable smb
      systemctl enable nmb     



Windows Client Configuration:
1)

Open Network and Sharing Center.
Turn on Network Discovery and File Sharing for all public networks OR
Make this network a private network.
   
Click on Network.

2)


Click on MERUBHAI.


3)

Click on public.


4)



NOTE:
1)
To view/change workgroup , right click on My Computer , Select Properties.

2)
To view ip address, type ipconfig in command terminal.

To change ip address, open Network and Sharing Center.
Click on Change Adapter Settings.
Click on Local Area Network.
Click on Properties.
Select Internet Protocol Version 4. Click Properties.Enter IP address.
       
      
View Windows Shared Folders in Linux :


1) View Remote Shares
       smbclient -L 192.168.122.156  -U shabbir    /'windows ip and username'
       Enter shabbir's password:
       Domain=[SHABBIR-PC] OS=[Windows 7 Professional 7601 Service Pack      1]    Server=[Windows 7 Professional 6.1]

    Sharename       Type      Comment
    ---------       ----      -------
    ADMIN$          Disk      Remote Admin
    C$              Disk      Default share
    IPC$            IPC       Remote IPC
    Users           Disk     

2) Create mount point and mount the remote share
       mkdir -p /mnt
      mount //192.168.122.156/Users  /mnt -o username=shabbir,password=pass

3) View shared folder contents
        ls /mnt

         Default  desktop.ini  Public  shabbir


4) Create entry in '/etc/fstab' to automatically mount at boot time.

       //192.168.122.2/Users           /mnt     cifs      rw,username=shabbir,password=pass      0  0

    This is a security risk as '/etc/fstab' is world-readable. So you can configure a dedicated credentials file which is readable only by 'root'.

       //192.168.122.2/Users           /mnt    cifs      rw,credentials=/etc/smbshabbir       0  0


   Create the file '/etc/smbshabbir'
       username=shabbir
      password=pass      

      
   chmod 600 /etc/smbshabbir







Caching-Only DNS Server in RHEL7/CentOS7


Recursive caching servers answer local users' queries about sites on the Internet.

A Caching-only DNS server will query other DNS servers and cache the response. Next time when the same query is given, it will provide the response from the cache. This will reduce the number of external queries and improve response time for users.

A Caching-only name server loads the addresses of the servers for the root domain from a startup file and accumulates the rest of its data by caching answers to the queries it resolves.

It is not advisable to configure 'bind' to work as your authoritative server for your own zones and as a caching server for your users. The function of serving your authoritative data to the world should be separated from serving the world's data to your users. 


DNS Server IP Address: 192.168.122.1


1) Install DNS Server
[root@meru ~]# sudo yum -y install bind bind-utils

2) Edit the configuration file '/etc/named.conf' and make the following changes.

//The network interface on which to listen for queries
           listen-on port 53 { 127.0.0.1; 192.168.122.1; };  

//Clients allowed to query the DNS Server
           allow-query     { localhost; 192.168.122.0/24; }; 

 //All queries are forwarded to this server. The use of a forwarder circumvents the normal DNS procedure of starting at a root server and following a chain of referrals.
         forwarders { 8.8.8.8; 8.8.4.4; }; 
 
//Optional. A forward only server caches values and queries forwarders, but it never queries anyone else. If the forwarders do not respond, queries will fail.
           forward only;    


4) Check the config files for errors.
[root@meru ~]# named-checkconf


5) Open Firewall Port
[root@meru ~]# firewall-cmd --zone=public --add-service=dns --permanent
[root@meru ~]# firewall-cmd --reload


6) Start the DNS Server
[root@meru ~]# systemctl start named

7) Enable on boot
[root@meru ~]# systemctl enable named

8) Check status
[root@meru ~]# rndc status
        CPUs found: 4
        worker threads: 4
        UDP listeners per interface: 4
        number of zones: 104
        recursive clients: 0/0/1000
        tcp clients: 0/100
        server is up and running



Client Configuration

1) Install package
[root@server2 ~]# yum -y install bind-utils

2) Edit the file '/etc/resolv/conf'. Add the following line
          nameserver 192.168.122.1

3) Query the DNS Server
[root@server2 ~]# dig @192.168.122.1 www.google.com

; <<>> DiG 9.9.4-RedHat-9.9.4-14.el7 <<>> www.google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 51202
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 13, ADDITIONAL: 16

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.google.com.            IN    A

;; ANSWER SECTION:
www.google.com.        206    IN    A    173.194.36.51
www.google.com.        206    IN    A    173.194.36.49
www.google.com.        206    IN    A    173.194.36.50
www.google.com.        206    IN    A    173.194.36.48
www.google.com.        206    IN    A    173.194.36.52

;; AUTHORITY SECTION:
com.            111341    IN    NS    m.gtld-servers.net.

Tuesday, 14 April 2015

Configure DNS Server for a Domain in RHEL7

A DNS Server (name server) resolves host names to ip addresses and vice versa. A DNS Server are of several types:

Caching-only DNS server: which stores recent requests like a proxy server. It refers to  other DNS servers for requests not in its current cache.

Forward-only DNS server: A forward only server caches values and queries forwarders, but it never queries anyone else. If the forwarders do not respond, queries will fail.

Authoritative Name Server: An official representative of a zone. Authoritative Name Servers are of 2 types:

Primary (Master) Server: stores the zone files on disk. Modifications to zone files are made ONLY on the Primary Server. Each zone has one master name server.

Secondary (Slave) Server: Secondary servers retrieve information about the zone through a zone transfer from the master server or from another secondary server. DNS information about a zone is never modified directly on the secondary server

In this tutorial, we configure a Master Server for the domain 'mycompany.com'

Consider the following scenario:

DNS Server HostName: meru.mycompany.com
DNS Server IP Address: 192.168.122.1/24
DNS Domain Name : mycompany.com

DNS Server Configuration:


1) Install DNS Server
[root@meru ~]# yum -y install bind bind-utils

2) Edit the configuration file '/etc/named.conf' and make the following changes.

//The network interface on which to listen for queries
listen-on port 53 { 127.0.0.1; 192.168.122.1; };    

//Clients allowed to query the DNS Server  
allow-query     { localhost; 192.168.122.0/24; };  

//Restrict zone transfer to the IP Address of the Slave Name Server
allow-transfer { 192.168.122.2; };

// Forward any unresolved requests to your ISP's name server. Or use google server  8.8.8.8 or 8.8.4.4.
forwarders { 8.8.4.4; }; 

//Add the zone declaration for the domain 'mycompany.com' at the end of the file. 
// forward zone declaration
zone "mycompany.com" IN {
      type master;
      file "named.mycompany.com";
};


//reverse zone declaration
zone "122.168.192.in-addr.arpa" IN {
        type master;
        file "named.122.168.192.in-addr.arpa";
};


3)Create the forward zone definition file '/var/named/named.mycompany.com' as shown below. 

$TTL 1D
@       IN SOA  meru.mycompany.com. root.meru.mycompany.com. (
                                        0      
; serial
                                        1D     
; refresh
                                        1H     
; retry
                                        1W     
; expire
                                        3H )   
; minimum
        IN      NS      meru.mycompany.com.   
; DNS Server for the domain mycompany.com
        IN      MX      10      meru.mycompany.com.  
; Mail Server for the domain mycompany.com

meru        IN      A            192.168.122.1   
;IP Address of meru
server1    IN      A            192.168.122.2 
;IP Address of server1  
server2    IN      A            192.168.122.3  ;IP Address of server2

www         IN      CNAME   meru            ;Alias for meru


5) Create the reverse zone definition file '/var/named/named.122.168.192.in-addr.arpa' as shown below. 

$TTL 1D
@       IN SOA  root.meru.mycompany.com. meru.mycompany.com. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
@       IN      NS      meru.mycompany.com.

1       IN      PTR     meru.mycompany.com.
2       IN      PTR     server1.mycompany.com.
3       IN      PTR     server2.mycompany.com.


4) Check the config file and zone files for errors.
[root@meru ~]# named-checkconf

[root@meru ~]# named-checkzone mycompany.com /var/named/named.mycompany.com

[root@meru ~]# named-checkzone 122.168.192.in-addr.arpa /var/named/named.122.168.192.in-addr.arpa

5) Open Firewall Ports. UDP/53 for DNS queries, TCP/53 for Zone Transfer.
[root@meru ~]# firewall-cmd --zone=public --add-service=dns --permanent
[root@meru ~]# firewall-cmd --reload


6) Start the DNS Server
[root@meru ~]# systemctl start named

7) Enable at boot
[root@meru ~]# systemctl enable named

8) Check status
[root@meru ~]# rndc status
        CPUs found: 4
        worker threads: 4
        UDP listeners per interface: 4
        number of zones: 104
        recursive clients: 0/0/1000
        tcp clients: 0/100
        server is up and running



Client Configuration


1) Install packages
[root@server1 ~]# yum -y install bind-utils

2) Edit the file '/etc/resolv.conf'. Add the following line
          nameserver 192.168.122.1

3) Query the DNS Server
[root@server1 ~]# dig @192.168.122.1 server2.mycompany.com
; <<>> DiG 9.9.4-RedHat-9.9.4-14.el7 <<>> @192.168.122.1 server2.mycompany.com
; (1 server found)
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;server2.mycompany.com.        IN    A

;; ANSWER SECTION:
server2.mycompany.com.    86400    IN    A    192.168.122.3




[root@meru ~]# dig  @localhost -x 192.168.122.2
 ; <<>> DiG 9.9.4-RedHat-9.9.4-14.el7 <<>> @localhost -x 192.168.122.2
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;2.122.168.192.in-addr.arpa.    IN    PTR

;; ANSWER SECTION:
2.122.168.192.in-addr.arpa. 86400 IN    PTR    server1.mycompany.com.

Configure PXE Installation Server in CentOS7




A PXE Installation Server allows your client computers to boot and install Linux over the network.

 The following steps must be performed to prepare for a PXE installation
Step 1) Configure Installation server :- Provides the distribution tree for the installation

Step 2) Configure the tftp server necessary for PXE booting :- Provides files necessary to start the installation program.

Step 3) Configure the dhcp server :- Provides ip address of the tftp server and the loacation of files on tftp server


Install Packages

1) Install dhcp server

          sudo yum install dhcp

2) Install tftp server
          sudo yum install tftp-server

3) Install ftp server
           sudo yum install vsftpd

4) Install xinetd server
           sudo yum install xinetd

5) Install syslinux
           sudo yum install syslinux



IP Address of PXE Install Server: 192.168.1.4

Step 1) Configure Installation Server using FTP Server


1) Copy Installation Media from DVD or ISO File to '/var/ftp' directory
  1.1) Mount Iso file on  /mnt folder to create distribution tree
                sudo mount -o loop CentOS-7.0-1406-x86_64-DVD.iso  /mnt

  1.2) Copy distribution tree to '/var/ftp'
                sudo cp -a /mnt/.  /var/ftp

2) Change SELinux file context for FTP
                sudo chcon -R -t  public_content_t  /var/ftp 

3) Open ftp service in firewall
                 sudo firewall-cmd --zone=public --add-service=ftp --permanent
                 sudo firewall-cmd --reload

4) Start ftp service
                 sudo systemctl start vsftpd

5) Enable ftp  service on boot
                  sudo systemctl enable vsftpd



Step 2) Configure tftp Server


1) Edit the file '/etc/xinetd.d/tftp' and make the following changes.
           service tftp            {
                  socket_type = dgram
                  protocol = udp
                  wait = yes
                  user = root
                  server = /usr/sbin/in.tftpd
                  server_args = -s /var/lib/tftpboot
                  disable = no
                  per_source = 11
                  cps = 100 2
                  flags = IPv4
            } 

2) Make directory pxelinux in /var/lib/tftpboot
           sudo mkdir -p /var/lib/tftpboot/pxelinux

3) Copy file pxelinux.0 to /var/lib/tftpboot/pxelinux
         sudo cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/pxelinux

4) Copy file vesamenu.c32 to /var/lib/tftpboot/pxelinux
         sudo cp /usr/share/syslinux/vesamenu.c32 /var/lib/tftpboot/pxelinux

5) Make directory pxelinux.cfg in pxelinux directory
         sudo mkdir -p /var/lib/tftpboot/pxelinux/pxelinux.cfg

6) Create the file '/var/lib/tftpboot/pxelinux.cfg/default' and add following entry in it.
            default vesamenu.c32

          prompt 1

          timeout 600



          label linux

          menu label ^Install system

          menu default

          kernel vmlinuz

          append initrd=initrd.img ip=dhcp inst.repo=ftp//:192.168.1.4 




7) Copy initrd.img and vmlinuz from /var/ftp/images/pxeboot folder of installation folder  to pxelinux
          sudo cp /var/ftp/images/pxeboot/vmlinuz /var/lib/tftpboot/pxelinux

          sudo cp /var/ftp/images/pxeboot/initrd.img  /var/lib/tftpboot/pxelinux


8) Open tftp service in firewall
        sudo firewall-cmd --zone=public –add-service=tftp –permanent
        sudo firewall-cmd --reload

9) Start xinetd service
         sudo systemctl start xinetd


Step 3) Configure Dhcp server

1) Edit /etc/dhcp/dhcpd.conf file

             option space pxelinux;

             option pxelinux.magic code 208 = string;

             option pxelinux.configfile code 209 = text;

             option pxelinux.pathprefix code 210 = text;

             option pxelinux.reboottime code 211 = unsigned integer 32;



             default-lease-time 600;

             max-lease-time 7200;



             subnet 192.168.1.0 netmask 255.255.255.0

             {

                   range 192.168.1.25 192.168.1.82;

             }





             allow booting;

             allow bootp;



             class "pxeclients" {

                      match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";

                      next-server 192.168.1.4;     

                      filename "pxelinux/pxelinux.0";

                    }




2) Open dhcp service in firewall
          sudo firewall-cmd --zone=public –add-service=dhcp –permanent
        sudo firewall-cmd --reload

3) Start dhcp service
         sudo systemctl start dhcp