Thursday, December 4, 2014

Fedora, RHEL or CentOS ?


Fedora

1. Run by Redhat 
2. Community supported.
3. It is the upstream distribution for both CentOS and Red Hat Enterprise  
   Linux. 
4. Acts as a test bed for future versions Red Hat Enterprise Linux (RHEL)
5. Release life cycle is 6 months (approx.), thus it has short 
   development, support life cycle.
6. Focusses more on the newer features rather then stability (since it 
   acts a test bed for future RHEL). Thus, more popular as Desktops 
   rather then production servers, in case of servers stability is 
   expected which is not the case with Fedora due to its short life cycle 
   and support and constant shifting APIs to incorporate newer features. 


Red Hat Enterprise Linux (RHEL)

1. Run by Redhat
2. Fully supported by Redhat but at a cost, thus it is not "free" to use 
   although source code can be used freely and distributed.
3. Red Hat support services, service level agreements, and certification 
   programs are based on RHEL.
4. Long life and support cycles of 7 years (approx) and support extended 
   to 10 years (approx).
5. Due to long life cycle focus is on stability rather then features. 
   Thus, ideal for Servers at the Enterprise level.


CentOS (Community Enterprise Operating System)
1. Community run.
2. Clone of RHEL in fact "binary compatible"
3. Long life and support cycles. The support is purely community based 
   and thus it is free.
4. It is a free drop-in replacement for RHEL.

*  Scientific Linux (SL) is another distribution run by "Fermi National 
   Accelerator Laboratory" . It is free and claims to be a clone of RHEL.

What to choose?
A. If stability and long term support are not the issues but cost is - 
   Fedora
B. If stability and cost are the issues and you want get long term 
   support service for free (community support)  - CentOS
C. But, if you want stability with long term dedicated support and ready 
   to pay  - RHEL


Note:
Who maintains Linux Kernel ?
Linus Torvalds, and he receives patches from different subsystems
Official website:    https://kernel.org/

Friday, October 10, 2014

NFS - Network File System


What is NFS?

Network File System or NFS is a client/server system. It allows users to access files across network and the shared files/directories from remote are treated as if they reside on local system.

Works on both TCP and UDP protocol.

The stateless UDP connection under normal conditions has less Protocol overhead than TCP which can translate into better performance on very clean, non-congested networks.

Note (From CentOS official docs):

NFSv4 has no interaction with portmapper, rpc.mountd, rpc.lockd, and rpc.statd, since protocol support has been incorporated into the v4 protocol. NFSv4 listens on the well known TCP port (2049) which eliminates the need for the portmapper interaction. The mounting and locking protocols have been incorpated into the V4 protocol which eliminates the need for interaction with rpc.mountd and rpc.lockd.

Salient features:

* A NFS file system mounted is very similar to a local file system on the machine
* NFS does not disclose the location of a file on the network
* An NFS server can be made of a completely different architecture and operating system
        than the client
* It also never discloses the underlying file system on the remote machine

Versions:
Currently there are three versions available viz. NFSv2, NFSv3 & NFSv4

Setup on CentOS-6.5 systems:
Following are the IP details of the server and the Client for this example setup;

i. Server: 172.16.20.8
ii. Client: 172.16.20.9

A. NFS Server (IP 172.16.20.8):

1. Install the packages;
yum install nfs*

2. Start the NFS service;
# service nfs start
Starting NFS services:      [  OK  ]
Starting NFS mountd:        [  OK  ]
Starting NFS daemon:        [  OK  ]
Starting RPC idmapd:        [  OK  ]

[NOTE:  Although, it is mentioned that rpcbind (earlier Portmapper) services are incorporated into NFS-V4 and there is no need to start the rpcbind service explicitly, I encountered NFS services failed to start without explicitly starting the "rpcbind" service; thus if you encounter the same, start the rpcbind service as; 

# service rpcbind start

Starting rpcbind:                                          [  OK  ]
]


3. Ports it listen to;

111/tcp  open  rpcbind
2049/tcp open  nfs

4. Open the relevant ports in the firewall so that they are accessible;

# iptables -I INPUT 4 -p tcp --dport 111 -j ACCEPT
# iptables -I INPUT 4 -p tcp --dport 2049 -j ACCEPT

Note: Number '4' is the position in the INPUT chain where these records are inserted, could be different in your case. Find that out using "iptables -L INPUT --line-numbers"

5. Create a NFS share;
mkdir /nfsshare

6. Share the directory with NFS client (172.16.20.9):

vi /etc/exports
/nfsshare 172.16.20.9(rw,sync,no_root_squash)


B. On the Client (172.16.20.9):

1. Install the packages
        #yum install nfs*

2. Start NFS service;
        # service nfs start

3. You may check all the rpcservices information on the server using;
         # rpcinfo -p 172.16.20.8

2. Create the mount point to mount the NFS share;
# mkdir /opt/nfs_share

3. Mount the NFS share directory from the NFS server to the client;
# mount -t nfs 172.16.20.8:/nfsshare/ /opt/nfs_share/

4. Check with mount command;
# mount
/dev/vda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)
172.16.20.8:/nfsshare/ on /opt/nfs_share type nfs (rw,vers=4,addr=172.16.20.8,clientaddr=172.16.20.9)

5. On the client, one may also check with 'nfsstat -m' command (displays details about nfs mounts)

# nfsstat -m
/opt/nfs_share from 172.16.20.8:/nfsshare/
Flags:
rw,relatime,vers=4,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=172.16.20.9,minorversion=0,local_lock=none,addr=172.16.20.8

Now, create files or directories under /opt/nfs/ (on the client) or /nfsshare on the server to see if they are visible/accessible to each other.

C. NFS Options:

1. The following would provide statistics about NFS server and client respectively;
nfsstat -s
nfsstat -c

2. Important configuration files (all man page info);

/etc/sysconfig/nfs
/etc/nfsmount.conf ---->  Configuration  file  for  NFS  mounts  that  allows options to be set
                                             globally, per server or per mount point.
/etc/exports     ---->  Contains  a table of local physical file systems on an NFS server that
                                             are accessible to NFS clients.

3. Any changes to the /etc/exports would only require to have NFS services reloaded to
        make the changes effective. This does not require clients to unmount NFS share or the
        any service restarts on clients and server.

eg.

Thus, an entry like the following in /etc/exports;
/nfsshare 172.16.20.9(rw,sync,no_root_squash)

when changed to ('rw' to 'ro');
/nfsshare 172.16.20.9(ro,sync,no_root_squash)

It only needs 'service nfs restart' on the NFS Server to make the changes effective.


D. NFS share options:

1. ro:    With the help of this option we can provide read only access to the shared files i.e client
                will only be able to read.

2. rw:   This option allows the client server to both read and write access within the shared
                directory.

3. sync:   Sync confirms requests to the shared directory only once the changes have been
                  committed.

4. no_subtree_check:   This option prevents the subtree checking. When a shared directory
                                       is the subdirectory of a larger file system, nfs performs scans of every
                                       directory above it, in order to verify its permissions and details.

                   Disabling the subtree check may increase the reliability of NFS, but reduce security.

5.     no_root_squash:    This phrase allows root to connect to the designated directory.
                                     Thus, remote  "root" user on the client would be treated a                                  
                                     local "root" user on the server.
                                      The opposite is "root_squash".

Issues:
1. # showmount -e
clnt_create: RPC: Unknown host

Solution: Check using;

                # showmount -e localhost
         Export list for localhost:
        /nfsshare 172.16.20.9

         Now, provide a hostname entry with IP in the /etc/hosts as;
         172.16.20.8 ins-1

         Where, ins-1 is the hostname as revealed by;
           # hostname
           ins-1

Friday, September 12, 2014

Toolbox for Linux admin !

A. Network / Diagnostic / Monitoring tools:

1. ifstat - Report InterFace STATistics

2. vmstat - Report virtual memory statistics

3. iostat  -  Report Central Processing Unit (CPU) statistics and input/output statistics for devices,
                     partitions and network filesystems (NFS)

4. Dstat -    A versatile replacement for vmstat, iostat and ifstat. Dstat overcomes some of the
                      limitations and adds some extra features.

5. iptraf - Interactive IP LAN Monitor

6. top - display Linux tasks

7. sar - Collect, report, or save system activity information.

8. mpstat - Report processors related statistics.

9. pmap - report memory map of a process

10. ss - Utility to investigate sockets

eg.
# ss -l
Recv-Q Send-Q                     Local Address:Port                         Peer Address:Port
0      128                                   :::ssh                                    :::*    
0      128                                    *:ssh                                     *:*    
0      5                              127.0.0.1:42551                                   *:*    

11. netstat - Print network connections, routing tables, interface statistics, masquerade con‐nections,
                    and multicast memberships

12. nmap - Network exploration tool and security / port scanner

13. nethogs - Net top tool grouping bandwidth per process

14. tcpdump - dump traffic on a network

eg.
tcpdump -i eth0 'udp port 53'

This shows all packets going in and out of the machine for UDP port 53

15. sosreport - Generate debugging information for system

16. iftop - display bandwidth usage on an interface by host


B. Utilities:

1. AIDE - Advanced Intrusion Detection Environment. A host-based intrusion detection system
                          (HIDS) for checking the integrity of files. It does this by creating a baseline database
                          of files on an initial run, and then checks this database against the system on
                          subsequent runs.

2. DenyHosts - is a log-based intrusion prevention security tool for SSH servers.

3. Fail2ban - Fail2ban scans log files (e.g. /var/log/apache/error_log) and bans IPs that show the
                        malicious signs -- too many password failures, seeking for exploits, etc

4. Monit - utility for monitoring services on a Unix system

5.

Installing Java-7 and Apache Tomcat/8.0.12 on CentOS-6.5


I. JAVA:

1. Search JDK in the yum repositories;

# yum search java | grep -i --color JDK
ldapjdk-javadoc.x86_64 : Javadoc for ldapjdk
icedtea-web.x86_64 : Additional Java components for OpenJDK - Java browser
java-1.6.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.6.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.6.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.6.0-openjdk-javadoc.x86_64 : OpenJDK API Documentation
java-1.6.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.7.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.7.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.7.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.7.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.7.0-openjdk-src.x86_64 : OpenJDK Source Bundle
ldapjdk.x86_64 : The Mozilla LDAP Java SDK

2. Once found, install the required version (if the search does yield any result then please download
         and install manually).

# yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel

3. Check if there are already installed versions of Java;

# alternatives --config java

There is 1 program that provides 'java'.

Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java

Enter to keep the current selection[+], or type selection number:

4. Set JAVA_HOME varaiable and include Java binaries in the system PATH;

# vi /etc/profile

## Set JAVA_HOME
JAVA_HOME="/usr/lib/jvm/jre-1.7.0-openjdk.x86_64"
export JAVA_HOME

## Set JAVA Path
PATH=$PATH:/usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin

5. Execute the file so that the new changes are reflected on the system;
# source /etc/profile

6. Test Java:
A. # echo $JAVA_HOME
/usr/lib/jvm/jre-1.7.0-openjdk.x86_64

B. # cat > HelloWorld.java
  public class HelloWorld {
public static void main(String[] args) {
         System.out.println("Hello, World! ");
    }
  }

# javac HelloWorld.java
# java HelloWorld
Hello, World!

II. Apache Tomcat

1. Download Apache Tomcat/8.0.12:
# wget http://apache.mirrors.pair.com/tomcat/tomcat-8/v8.0.12/bin/apache-tomcat-8.0.12.tar.gz

2. Extract:
# tar -xvzf apache-tomcat-8.0.12.tar.gz

3. I moved the extracted directory under /usr/local;
# mv apache-tomcat-8.0.12 /usr/local/

4. # /usr/local/apache-tomcat-8.0.12/bin/startup.sh
Using CATALINA_BASE:   /usr/local/apache-tomcat-8.0.12
Using CATALINA_HOME:   /usr/local/apache-tomcat-8.0.12
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-8.0.12/temp
Using JRE_HOME:        /usr/lib/jvm/jre-1.7.0-openjdk.x86_64
Using CLASSPATH:       /usr/local/apache-tomcat-8.0.12/bin/bootstrap.jar:/usr/local/apache-
        tomcat-8.0.12/bin/tomcat-juli.jar
Tomcat started.

Monday, August 25, 2014

Setting up OpenVPN Server on CentOS-6.5 (running as a Openstack cloud instance)

I. Setting up OpenVPN Server on CentOS-6.5 (running as a Openstack cloud instance). The setup may just work the same on other *nix based systems.

A. OpenVPN Server

1. Download and Install the EPEL repo which would provide OpenVPN rpm;
(Epel repo: http://dl.fedoraproject.org/pub/epel/6/x86_64/)
# wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm .
# rpm -ivh epel-release-6-8.noarch.rpm

2. Install openvpn and easy-rsa (RSA key management package)
# yum -y install openvpn easy-rsa

3. # mkdir -p /etc/openvpn/easy-rsa/keys
# cp -rf /usr/share/easy-rsa/2.0/* /etc/openvpn/easy-rsa/

B. Generate CA Certificate and CA key

4. Open /etc/openvpn/easy-rsa/vars and set the following attributes which we would use later on Certificates and keys;
# vi /etc/openvpn/easy-rsa/vars

export KEY_COUNTRY="IN"
export KEY_PROVINCE="WB"
export KEY_CITY="KOL"
export KEY_ORG="IT"
export KEY_EMAIL="tu.bijit@gmail.com"
export KEY_OU="MyOrganizationalUnit"

5. Copy the required OpenSSL configuration file which OpenVPN might fail to detect;
# cd /etc/openvpn/easy-rsa/
# cp openssl-1.0.0.cnf openssl.cnf

6. Now, lets build our Certificate Authority (CA) based on the information provided in /etc/openvpn/easy-rsa/vars.
# source ./vars
# ./clean-all
# ./build-ca

7. Generate certificate for the OpenVPN server. Respond "Yes" when prompted for build-key-server to commit.

# ./build-key-server server

8. Generate the same for Client (which we would be copying to client later):
# ./build-key client

9. Generate Diffie Hellman key exchange files using the build-dh script;
# ./build-dh

10. In the above step, the necessary keys and certificates will be generated in the /etc/openvpn/easy-rsa/keys/ directory. Copy the following certificate and key files to the /etc/openvpn/ directory.

ca.crt
dh2048.pem  (please make changes in the server.conf when needed)
server.crt
server.key

# cd /etc/openvpn/easy-rsa/keys/; cp ca.crt dh2048.pem server.crt server.key /etc/openvpn/

11. Copy all client certificates and keys to the remote VPN clients in order to authenticate to the VPN server. Since, we have generated certificates and keys for only one client, so we have to copy the following files (or its contents, keep the name exactly the same) to the VPN client.

ca.crt
client.crt
client.key

C. Configuring VPN Server

12. OpenVPN comes with a sample configuration file, copy that file to /etc/openvpn;

# cp /usr/share/doc/openvpn-2.3.2/sample/sample-config-files/server.conf /etc/openvpn/

15. Open /etc/openvpn/server.conf and make the follwoing changes;

Do the following;
i. Uncomment ( This is route all the traffic from Client through VPN):
push "redirect-gateway def1 bypass-dhcp"

ii. Use VPN to use opendns servers:
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"

iii. Uncomment the following to reduce the OpenVPN daemon's privileges after initialization.

user nobody
group nobody

D. IP forwarding and routing Configuration:

13. vi /etc/sysctl.conf
Find the following line and set value “1” to enable IP forwarding.

# Controls IP packet forwarding
net.ipv4.ip_forward = 1

Run the following command to apply the sysctl changes.
# sysctl -p

14. iptables rules:

i.    Allow proper routing of our VPN subnet,assuming 10.8.0.0/24 is used in /etc/openvpn/server.conf
# iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

ii. Allow port 1194 of openvpn to let traffic come in (iptables -L --line-numbers; iptables -I INPUT -p udp --dport 1194 -j ACCEPT)
# iptables -I INPUT 4 -p udp --dport 1194 -j ACCEPT
# service iptables save

15. # service openvpn start

16.  Check if the OpenVPN is listening on port 1194 (UDP port by default, you can change that to TCP in /etc/server.conf)
# netstat -uapn | grep openvpn
udp        0      0 0.0.0.0:1194                0.0.0.0:*                               11577/openvpn


17. Check interface VPN is using (tun0 is in our case, "dev tun" will create a routed IP tunnel,);
# ip addr show
1: lo: mtu 16436 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
inet6 ::1/128 scope host
  valid_lft forever preferred_lft forever
2: eth0: mtu 1450 qdisc pfifo_fast state UP qlen 1000
link/ether fa:16:3e:7e:11:4a brd ff:ff:ff:ff:ff:ff
inet 172.16.20.8/24 brd 172.16.20.255 scope global eth0
inet6 fe80::f816:3eff:fe7e:114a/64 scope link
  valid_lft forever preferred_lft forever
3: tun0: mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 100
link/[65534]
inet 10.8.0.1 peer 10.8.0.2/32 scope global tun0

18. In certain cases, once VPN client connects to the VPN server, one might face loss of internet connectivity (was in my case); Following IPTABLES rules would be required to proper routing (as client changes the Gateway to VPN, which should be properly forwarded to access the LAN on the VPN server (you may use the specific LAN IP in the destination, here I used entire network in the destination)

    # Allow traffic initiated from VPN to access LAN
 iptables -I FORWARD -i tun0 -o eth0 -s 10.8.0.0/24 -m conntrack --ctstate NEW -j ACCEPT

    # Allow established traffic to pass back and forth
iptables -I FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT

    # service iptables save


II. OpenVPN Client (On Windows)

1. On Windows, one can download OpenVPN GUI for Windows;
http://openvpn.se/

2. Make sure we copied the following files from the server which were generated using "./build-key" earlier.

ca.crt
client.crt
client.key

3. Create a new file called client.ovpn, where "client" should match the name of the client being deployed (from build-key) with following contents;

remote x.x.x.x 1194
# Path to certificates (in windows as follows)
ca "C:\\MyVPN\\ca.crt"
cert "C:\\MyVPN\\client.crt"
key "C:\\MyVPN\\client.key"

4. Save and connect to VPN server using client config file.


III. OpenVPN Client (On Linux, CentOS release 6.5 (Final): 1. Download and install OpenVPN package. You would be required to setup EPEL repo as discussed 
before. 2. Copy the sample Openvpn client configuration to your desired locaton; # cp /usr/share/doc/openvpn-2.3.2/sample/sample-config-files/client.conf . 3. Copy the following files from the server which were generated using "./build-key" earlier. ca.crt client.crt client.key 4. Provide the following information in "client.conf"; remote x.x.x.x 1194 ca ca.crt cert client.crt key client.key 5. Connect to the OpenVPN server as; # openvpn client.conf &