Tuesday, October 28, 2014

CentOS / RHEL 6 - Setup SELinux to work with Apache

By default SeLinux is enabled and it will mark some labeling on each and every file and ports on OS based on Service.

SeLinux work on following parameters:

1) Context
2) Boolean

1) Context, we can say label. Commands chcon and semanage
2) Boolean, SeLinux having properties for each service like need to enable read or write access to directory, enable http with ftp or not etc.. Commands semanage, getseboolean, setseboolean

In our case we are going to setup SeLinux to work with Apache

Context

Port Settings

As per SeLinux, services can work on only assigned specific ports like

semanage port -l|grep -i mysql

mysqld_port_t                  tcp      1186, 3306, 63132-63164

semanage port -l|grep -i http_port_t

http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000

So, port given in above output, by default allowed for mysql and apache services

Lets, take an example and change following line in /etc/httpd/conf/httpd.conf

Listen *:2222

Save and restart httpd service

While httpd restart, this will show following:

service httpd restart
Stopping httpd:                                            [FAILED]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.56.103 for ServerName
(13)Permission denied: make_sock: could not bind to address [::]:2222
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:2222
no listening sockets available, shutting down
Unable to open logs
                                                           [FAILED]

It's showing Permission denied: make_sock, that means SeLinux not allowing to create sock on 2222 port

As we want run Apache / httpd on 2222 port, execute following command to enable 2222 port for httpd service

semanage port -a -t http_port_t -p tcp 2222

semanage port -l|grep -i http_port_t

http_port_t                    tcp      2222, 80, 81, 443, 488, 8008, 8009, 8443, 9000

Now 2222 port has been assigned to http_port i.e.; http/apache service

Start http/apache service, you should not get any error this time

service httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.56.103 for ServerName
                                                           [  OK  ]

netstat -antp|grep http
tcp        0      0 :::2222                     :::*                        LISTEN      1403/httpd

File/Folder Labeling

SeLinux keep labeling on each and every file which is as per service

ls -ldZ /var/www/

drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/

ls -ldZ /var/www/html/

drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/

ls -ldZ /etc/httpd/

drwxr-xr-x. root root system_u:object_r:httpd_config_t:s0 /etc/httpd/

ls -ldZ /etc/httpd/conf

drwxr-xr-x. root root system_u:object_r:httpd_config_t:s0 /etc/httpd/conf

ls -lZ /usr/sbin/httpd

-rwxr-xr-x. root root system_u:object_r:httpd_exec_t:s0 /usr/sbin/httpd

Here (starting with http means its Domain for only httpd service):

httpd_config_t                          # http configuration
httpd_sys_content_t                 # http system content
httpd_exec_t                            # http executable

So, each and every called by httpd/apache service should have proper label.

Lets try to run httpd with different DocumentRoot with im-proper labeling on files and folders

mkdir /data/www -p

ls -ldZ /data/

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /data/

ls -ldZ /data/www/

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /data/www/


cat > /data/www/index.html
this is test

ctrl+D

ls -lZ /data/www/index.html

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 /data/www/index.html


Restart httpd service and access through your web browser http://server-IP:2222

You should get following error in web broweser:

Forbidden

You don't have permission to access / on this server.


and following in apache/httpd service error logs:

[Tue Oct 28 21:17:07 2014] [error] [client 192.168.56.1] (13)Permission denied: access to /index.html denied


How to recover from above error:

Change label of DocumentRoot i.e.; /data/www and its files

As this is going to be http content directory, we should change label to httpd_sys_content_t

chcon -t httpd_sys_content_t /data -R

for permanent changes

semanage fcontext -t httpd_sys_content_t /data
semanage fcontext -t httpd_sys_content_t /data/www

Now you should be able to access server pages without any error

Command to see running process context

ps auxZ         # will provide process context too

Boolean

Commands to get or set Boolean variables

getsebool
setsebool

Some of the http/apache boolean variables

getsebool -a|grep http

httpd_enable_cgi --> on
httpd_enable_homedirs --> off
httpd_unified --> on
httpd_use_cifs --> off
httpd_use_fusefs --> off
httpd_use_gpg --> off
httpd_use_nfs --> off

If you do not want to disable apache/http to serve cgi files

setsebool httpd_enable_cgi off

Enable user's home directory to be used for data/pages serving for apache/http

setsebool httpd_enable_homedirs on

Enable Apache/http to use CIFS/NFS directory to serve pages

setsebool httpd_use_cifs off
setsebool httpd_use_nfs off


Other commands to use

Disable SeLinux

setenforce 0

modify /etc/sysconfig/selinux file and set
SELINUX=disabled

Reboot machine

Command to restore context

restorecon -Rv /data

Create /.autorelabel for allow OS to relabel complete system at next boot time

Wednesday, October 1, 2014

How to Install and Configure NTP Server and Client in RHEL/CentOS6?

NTP Server

1) Install NTP package using yum

yum -y install ntp

2) Setup Restrict Values

vim /etc/ntp.conf               # Add below given lines

# Permit time synchronization with our time source, but do not # permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

  • noquery prevents dumping status data from ntpd.
  • notrap prevents control message trap service.
  • nomodify prevents all ntpq queries that attempts to modify the server.
  • nopeer prevents all packets that attempts to establish a peer association.
  • Kod – Kiss-o-death packet is to be sent to reduce unwanted queries
The value -6 in the second line allows forces the DNS resolution to the IPV6 address resolution. For more information on the access parameters list, Please refer to documentation on “man ntp_acc”The value -6 in the second line allows forces the DNS resolution to the IPV6 address resolution.

3) Allow network / IP range for synchronizing time from this server

restrict 10.0.0.0 mask 255.255.255.0 nomodify notrap
restrict 127.0.0.1

4) Add local clock as backup

In case NTP server disconnected from internet, NTP server will provide time from its local system clock

server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10

5) Specify log files

driftfile /var/lib/ntp/ntp.drift
logfile /var/log/ntp.log

6) Finally start NTP server

service ntpd start

NTP Client

1) Modify /etc/ntp.conf configuration file, 

Sample entries, for internet servers :

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

iburst: After every poll, a burst of eight packets is sent instead of one. When the server is not responding, packets are sent 16s interval. When the server responds, packets are sent every 2s.

Edit your NTP.conf to reflect appropriate entries for your own NTP server.

server 10.0.0.1 prefer

prefer: If this option is specified that server is preferred over other servers.

2) Start NTP service

service ntpd start

3) Finally check NTP status

ntpq -p

4) Set date and time synchronized with desired server

ntpdate -u SERVER-NAME

5) Get the current status of  ntpd:

ntpdc -c sysinfo

system peer:          web10.hnshosting.com
system peer mode:     client
leap indicator:       00
stratum:              3
precision:            -24
root distance:        0.20857 s
root dispersion:      0.27481 s
reference ID:         [120.88.46.10]
reference time:       d7d65c1f.14c6b054  Wed, Oct  1 2014 16:25:27.081
system flags:         auth monitor ntp kernel stats
jitter:               0.014099 s
stability:            0.000 ppm
broadcastdelay:       0.000000 s
authdelay:            0.000000 s