Archive for the ‘ Linux ’ Category

Redirecting an old domain name to a web page

I needed to redirect globalwindday.org to www.ewea.org/globalwindday

A normal Apache redirect did not work.

For example:
globalwindday.org/faq would redirect to www.ewea.org/globalwindday/faq
This was a page that did not exist so therefore resulted in a 404 error.

Here are the lines added to vhosts.conf to make it work (stripping sub pages):

<VirtualHost *:80>
        ServerName  globalwindday.org
        ServerAlias www.globalwindday.org
        # Redirect permanent / http://www.ewea.org/globalwindday/   <-- This did not work
        RewriteEngine On
        RewriteCond %{HTTP_HOST} globalwindday.org [NC]
        RewriteRule ^(.*)$  http://www.ewea.org/globalwindday/ [R=301,NC]
</VirtualHost>

 

As constructed from this reference page:
https://gist.github.com/ScottPhillips/1721489

Find original process in Linux

When you need to find the original thread in a list of a hundred child threads, you can use “ps auxc | grep

[root@main www.ewea.org]# ps auxc | grep httpd
root      1989  0.0  0.2 319260 12916 ?        Ss   09:24   0:01 httpd
apache    6341  1.4  1.1 483420 69864 ?        S    13:18   3:10 httpd
apache   12844  1.5  1.1 472324 68452 ?        S    14:16   2:32 httpd
apache   12854  1.5  1.0 466824 62652 ?        S    14:16   2:26 httpd
apache   22137  1.6  1.0 465680 61540 ?        S    15:30   1:25 httpd
apache   22145  1.5  1.0 466868 61920 ?        S    15:30   1:17 httpd
apache   22501  1.5  1.0 466620 62464 ?        S    15:32   1:19 httpd
apache   22508  1.5  1.0 369720 62528 ?        S    15:32   1:18 httpd
apache   22509  1.5  1.1 480484 67440 ?        S    15:32   1:17 httpd

Resetting the root password

If you ever forget your root password, you can actually reset it with the following steps.

  • Interrupt the boot at the GRUB stage and boot to runlevel 1, AKA single user mode.
  • Interrupt GRUB by typing a character such as “space” then append to the kernel line by typing “a”
  • Backspace through “rhgb quiet” and append ” 1 “
  • Then hit <enter>

This will give you a root shell and not a login prompt. From there you can use the “passwd” command to set a new root password.
Other user passwords can be reset, and other administrative tasks can be performed as well. Exiting the single user root shell will boot to the multi-user mode defined in /etc/inittab.

CentOS-6 Note: Due to an upstream SELinux bug the root password cannot be reset on a fresh install of CentOS-6 without an additional step. Booting with “selinux=0” appended to the grub kernel line, or doing “setenforce 0” after logging in and before attempting to reset the password are work-arounds.

(If SELinux is enforcing then do not forget to relabel your filesystem afterwards or you will not be able to login until you reboot and repeat the instructions and include the autorelabel)

 From: http://wiki.centos.org/TipsAndTricks/ResetRootPassword

Install MySQL 5.5 on CentOS/RHEL 6.5 and 5.10 via Yum

Redhat Enterprise Linux (RHEL) 5.x and CentOS 5.x have been providing MySQL 5.0 in it’s distribution since that was the active stable branch when RHEL 5.0 was released on 14th March 2007 (CentOS 5.0 on 12th April 2007). Since then, Sun/Oracle have released MySQL 5.1 and MySQL 5.5, so the Webtatic Yum repository has added them to it’s web stack, which now provides updated versions of all the AMP in LAMP.

Update 2014-03-01 – mysql55 packages have been renamed to mysql55w packages (as well as mysql51 to mysql51w). This is to ensure it doesn’t clash with SCL packages.

First, to set up the yum repository, install the webtatic-release RPM based on your CentOS/RHEL release:

Webtatic EL6 for CentOS/RHEL 6.x

rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm

If you already have MySql client or server installed (rpm -q mysql mysql-server), then you can upgrade using the following method:

yum install mysql.`uname -i` yum-plugin-replace
 yum replace mysql --replace-with mysql55w

“yum install mysql” is only there to make sure yum-plugin-replace can resolve dependencies correctly if only mysql-server was installed.

Otherwise, to install MySql client and server, then run:

yum install mysql55w mysql55w-server

You should upgrade existing tables before setting the server to become a production machine, which can be done by starting the server and running the mysql_upgrade script (this may take time depending on the size of the database).

service mysqld start

# This will issue a password prompt for the user. If you don’t have a root user password, remove the “-p”
mysql_upgrade -u root -p

MySQL 5.1

The Webtatic 5 repository for CentOS/RHEL 5.x includes mysql51-* packages for installing MySQL 5.1. This can be installed by replacing in the above commands “mysql55w” with “mysql51w”.

As MySQL 5.1 is standard in CentOS/RHEL 6.x, it is not included in the Webtatic EL 6 repository.

UnixBench to measure server performance

Installing and running UnixBench will allow you to benchmark your *nix servers against each other in order to find issues in performance and latency.

# yum -y groupinstall "Development Tools"
# yum -y install libX11-devel mesa-libGL-devel perl-Time-HiRes

We then fetch and install UnixBench (at the time of writing, the latest version is 5.1.3) :

# wget https://byte-unixbench.googlecode.com/files/UnixBench5.1.3.tgz 
# tar xvfz UnixBench5.1.3.tgz
# cd UnixBench
# ./Run

The ‘Run‘ script will compile everything and launch the tests.

A sample benchmark output will look like this: The higher the final number the better the server.

========================================================================
   BYTE UNIX Benchmarks (Version 5.1.3)

   System: projects2: GNU/Linux
   OS: GNU/Linux -- 3.2.58-xenU-11-50785a6-x86_64 -- #40 SMP Tue May 6 15:39:12 UTC 2014
   Machine: x86_64 (x86_64)
   Language: en_US.utf8 (charmap="UTF-8", collate="UTF-8")
   CPU 0: Intel(R) Xeon(R) CPU E5-2650L 0 @ 1.80GHz (3600.1 bogomips)
          Hyper-Threading, x86-64, MMX, Physical Address Ext, SYSENTER/SYSEXIT, SYSCALL/SYSRET
   11:11:52 up 12 min,  1 user,  load average: 0.40, 0.46, 0.23; runlevel 3

------------------------------------------------------------------------
Benchmark Run: Wed Aug 06 2014 11:11:52 - 11:39:58
1 CPU in system; running 1 parallel copy of tests

Dhrystone 2 using register variables       15348279.8 lps   (10.0 s, 7 samples)
Double-Precision Whetstone                     1568.1 MWIPS (10.3 s, 7 samples)
Execl Throughput                                858.6 lps   (29.9 s, 2 samples)
File Copy 1024 bufsize 2000 maxblocks        193338.8 KBps  (30.0 s, 2 samples)
File Copy 256 bufsize 500 maxblocks           46591.9 KBps  (30.0 s, 2 samples)
File Copy 4096 bufsize 8000 maxblocks        559840.4 KBps  (30.0 s, 2 samples)
Pipe Throughput                              261358.3 lps   (10.0 s, 7 samples)
Pipe-based Context Switching                  51940.6 lps   (10.0 s, 7 samples)
Process Creation                               2065.6 lps   (30.0 s, 2 samples)
Shell Scripts (1 concurrent)                   1423.0 lpm   (60.0 s, 2 samples)
Shell Scripts (8 concurrent)                    178.0 lpm   (60.2 s, 2 samples)
System Call Overhead                         255469.3 lps   (10.0 s, 7 samples)

System Benchmarks Index Values               BASELINE       RESULT    INDEX
Dhrystone 2 using register variables         116700.0   15348279.8   1315.2
Double-Precision Whetstone                       55.0       1568.1    285.1
Execl Throughput                                 43.0        858.6    199.7
File Copy 1024 bufsize 2000 maxblocks          3960.0     193338.8    488.2
File Copy 256 bufsize 500 maxblocks            1655.0      46591.9    281.5
File Copy 4096 bufsize 8000 maxblocks          5800.0     559840.4    965.2
Pipe Throughput                               12440.0     261358.3    210.1
Pipe-based Context Switching                   4000.0      51940.6    129.9
Process Creation                                126.0       2065.6    163.9
Shell Scripts (1 concurrent)                     42.4       1423.0    335.6
Shell Scripts (8 concurrent)                      6.0        178.0    296.7
System Call Overhead                          15000.0     255469.3    170.3
                                                                   ========
System Benchmarks Index Score                                         308.9

Downgrading PHP

I installed PHP on a Centos 6.5 i686 server recently and by default in installed PHP 5.5 which broke some apps.
You can easily downgrade PHP with the following command:

# yum downgrade php*

(Repeat the process again if you want to downgrade a version more)

How to install the HTOP command

  • Navigate to http://packages.sw.be/rpmforge-release/
  • Look for the latest release for your architecture
  • Log into your server
  • Run the command wget http://packages.sw.be/rpmforge-release/rpmforge-release-XXXXX.rpm
  • Run the command rpm -Uhv rpmforge-release*
  • Now you can install htop with yum install htop

Working with iptables and dynamic DNS

My new service provider does not assign static IP addresses to its customers so I had find a way around this issue in order to connect to the servers quickly from home.

Because my IP address is constantly changing, and not even within a consistent set of address blocks, I decided to use my personal domain name as the access point.
See here for information on setting up Dynamic DNS.

iptables will detect and resolve the ip address for any hostnames in the configuration file only the first time the service is started, so this is not ideal for a hostname with a changing (dynamic) IP address. This is why we need to use a script that will detect any changes and force iptables to restart.

I found this script below from here and it does everything I need it to do perfectly (change the values highlighted below to suit your own needs):

#!/usr/bin/python
# Version 1.0
# 6 January 2014
# Jason Bickley - Senior Web Manager
# Dynamic DNS script to update IP address for home network

import os

def gettextoutput(cmd):
    """Return (status, output) of executing cmd in a shell."""
    pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
    pipe = os.popen(cmd + ' 2>&1', 'r')
    text = pipe.read()
    if text[-1:] == '\n': text = text[:-1]
    return text

home_dyndns = "home.jasonbickley.net"
log_dyndns  = "/var/log/dyndns.log"
all_dyndns  = "/var/log/dyndns-all.log"

last_dyndns = gettextoutput("cat  " + log_dyndns)
curr_dyndns = gettextoutput("host " + home_dyndns)

print "Log: "+ last_dyndns
print "Cur: "+ curr_dyndns

if last_dyndns == curr_dyndns:
    print "IPs match, no restart necessary"
else:
    print "Updating last IP with current"
    os.system("echo '" + curr_dyndns + "' > " + log_dyndns)
    os.system("echo `date '+%Y%m%d %H:%M:%S'` '" + curr_dyndns + "' >> " + all_dyndns)
    print "Restarting iptables to update it"
    os.system("/etc/init.d/iptables restart")

Here is a summary of what the script above does:

It checks the IP address in the file /var/log/dyndns.log and compares the IP address with my subdomain’s (home.jasonbickley.net).
If the IP addresses match, then it does nothing. If they differ, it overwrites the IP address file with the new value, logs more specific details in /var/log/dyndns-all.log, and restarts the iptables service. This means that iptables now has the updated value for my subdomain.

I put the script here /root/Scripts/dyndns.py, chmod 700 it, and created a cron entry to run it every 10 mins.

Log files are full of “pam_loginuid(crond:session)”

The /var/log/secure file is full of the following lines:

Dec 16 06:12:01 dev crond[10012]: pam_loginuid(crond:session): set_loginuid failed opening loginuid
Dec 16 07:00:01 dev crond[10891]: pam_loginuid(crond:session): set_loginuid failed opening loginuid
Dec 16 07:12:01 dev crond[11133]: pam_loginuid(crond:session): set_loginuid failed opening loginuid
Dec 16 08:00:01 dev crond[12014]: pam_loginuid(crond:session): set_loginuid failed opening loginuid
Dec 16 08:12:01 dev crond[12227]: pam_loginuid(crond:session): set_loginuid failed opening loginuid
Dec 16 09:00:01 dev crond[13136]: pam_loginuid(crond:session): set_loginuid failed opening loginuid
Dec 16 09:12:01 dev crond[13349]: pam_loginuid(crond:session): set_loginuid failed opening loginuid
Dec 16 10:00:01 dev crond[14240]: pam_loginuid(crond:session): set_loginuid failed opening loginuid

We can stop this from happening by editing the following file:

# vi /etc/pam.d/crond

And commenting out the following line:

#
# The PAM configuration file for the cron daemon
#
auth       sufficient pam_env.so
auth       required   pam_rootok.so
auth       include    system-auth
account    required   pam_access.so
account    include    system-auth
#session    required   pam_loginuid.so
session    include    system-auth

PHP emailing script

This is a tested script for correctly sending emails via php:

<?php
$to      = '[email protected]';
$subject = 'This is the subject';
$message = 'Hello! this is the bottom text';
$headers = 'Content-type: text/html; charset="UTF-8"' . "\r\n" .
'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers,"[email protected]");
?>