Archive for November, 2012

Handling Multiple SSH Keys and Ports for Multiple Machines

You might have to deal with a lot of machines with different keys and ports. Here is a simple way to make that easier.

Edit / Create ssh config file.

vi ~yourUserName/.ssh/config

Put the information about your hosts into the config file:



IdentityFile ~/.ssh/server1_rsa
User userName
Port xxxx
ServerAliveInterval 30
ServerAliveCountMax 120

PROMPT_COMMAND='echo -ne "33]0;${USER}@${HOSTNAME}: ${PWD}07"'

Now whenever you try to connect to that host, it will use the key, username and port defined in this config file.

Setting up Git on Centos

In this tutorial, I am showing how to setup GIT on Centos with SSH .

Packages to be installed :

yum install git-core gitosis

Create user and group which will own the repositories :

useradd -d /path/to/repositories gituser

Generate or copy a ssh key for use with git :

su gituser

ssh-keygen -t rsa

Now initialize the gitosis :

gitosis-init < /path/to/ur/sshPrivateKey

Be safe by changing permissions on the post update :

sudo chmod 755 /path/to/repositories/gitosis-admin.git/hooks/post-update

Clone the gitosis-admin repo into your local machine :

git clone gituser@yourServer:gitosis-admin.git

Now you should have gitosis.conf and keydir/ in your gitosis-admin directory.

Creating New Repositories :

Now is the time to create a new Repository (eg. testrepo)

Open up the gitosis.conf and see the default configuration :

vi gitosis.conf

[gitosis] [group gitosis-admin] writable = gitosis-admin members = gitUser

Now to Create a new repo, we just authorize writing to the repo and push the changes . Add the following to the gitosis.conf file :

[group mydevel]
members = user1@hostname user2@hostname 
writable = repoName

Save the changes commit and push it to the server :

git commit -a -m "New users to the repo repoName"
git push

Now create the repository and initialize it :

mkdir repoName

cd repoName git init git remote add origin gitUser@serverName:repoName.git

Now create some files , save changes, add to repo. If you want to add existing files then you can use * to represent all files:

git add fileName


git add *

Now commit & push to the origin

git commit –a –m “Initializing new Repo”

git push origin master:refs/heads/master

Adding Users to the Repo :

In order to add users, first get their key file and put them in the key dir folder. Please note that all keys must have .pub extension and the username is anything before .pub

Example :

cd gitosis-admin
cp ~/ keydir/
cp ~/ keydir/
git add keydir/ keydir/

Now add the members to the list and commit changes :

git commit -a -m "Created New users newUser1 and newUser2"
git push

Now they can clone and start working on the project :

git clone newUser1@gitServer:testRepo.git

Configure Public access to the repo :

git-daemon --base-path=/path/to/repo/to/grantaccess/ --export-all

Note : In windows, you can use tortoise Git, you can save the session in putty and use the session name in stead of hostname in the commit and clone operations.

In unix , you can use non standard port by creating ssh config file :

Adding a Secondary Gateway / Static Route in Linux

In order to create a static Route you can enter the following command :

ip route add via dev eth0


route add -net netmask gw dev eth1

Note : In order to make this permanent, you would have to create a file named routes-eth0

vi /etc/sysconfig/network-scripts/route-eth0

Append the following line to the file, if the file does not exist , create new file via

You could also have something like this :



Installing and uninstalling redmine plugins

Download the plugin and extract it into the plugins directory.

Go to the plugins directory and run :

bundle install --without development test RAILS_ENV=production

If you need to migrate the database then run the following command :

rake redmine:plugins:migrate RAILS_ENV=production

Now restart redmine :

touch /path/to/redmineDir/tmp/restart.txt

You should now be able to see the plugin list in Administration -> Plugins and configure the newly installed plugin

Uninstalling the plugins :

rake redmine:plugins:migrate NAME=plugin_name VERSION=0 RAILS_ENV=production

Remove the plugins directory and restart redmine.

Installing Redmine with Passenger and Apache on Centos 6.3

1. Install prerequisite / dependencies :

yum install subversion make gcc gcc-c++ zlib-devel ruby-devel rubygems ruby-libs apr-devel apr-util-devel httpd-devel mysql-devel mysql-server automake autoconf ImageMagick ImageMagick-devel curl-devel


2. Go to the directory where you want to download Redmine Code and check out the files using svn command :

svn co redmine

3. Install the ruby gem named bundle :

gem install bundle

4. Now go to the redmine directory and  install the redmine’s ruby dependencies using the bundler :

bundle install --without postgresql sqlite test development

Now Lets Setup the MySQL Database

Now Create the MySQL Database for the redmine and create a user with access to that database :


create database redmine character set utf8;

grant all privileges on redmine.* to ‘redmineUser’@’localhost’ identified by redminePassword;

flush privileges;


Now create the database configuration file for redmine :

cd /path/to/redmineDir/config

cp database.yml.example database.yml

vi database.yml

Now in the database.yml  file enter the database information in the production section as shown below :

  adapter: mysql
  database: redmine
  host: localhost
  username: redmineUser
  password: redminePassword
  encoding: utf8

Now populate the database using the rake command :

cd /path/to/redmineDir

rake generate_secret_token rake db:migrate RAILS_ENV="production" rake redmine:load_default_data RAILS_ENV="production"

Configure outgoing Email :

cd /path/to/redmineDir/config
cp configuration.yml.example configuration.yml

Please read the email configuration section at the end for setting up outgoing email configuration, for now we will proceed and come back to email configuration at the end.

As of now you should be able to test the redmine in standalone mode. Run the following command to run redmine in the standalone mode  :

ruby script/server webrick -e production

Note : You need to be inside your redmine dir to run above command .

Now open the following URL and you should see the redmine  in your browser :



Now let’s Install Passenger module for apache  which will allow us to browse ruby application using apache . We will install phusion passenger for this and we will need to install the gem first :

gem install passenger

Now install the apache module for passenger :



Now we create the apache configuration for redmine :

vi /etc/httpd/conf.d/redmine.conf

Now paste the following content into the redmine.conf , please adjust the configuration to suite your environment :

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-3.0.18/ext/apache2/
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-3.0.18
PassengerRuby /usr/bin/ruby
<VirtualHost *:80>
    DocumentRoot /path/to/redmineDir/public
    <Directory /path/to/redmineDir/public>
        AllowOverride all #loosen apache security
        Options -MultiViews #turn of multi views allow from all
    ErrorLog "| /usr/sbin/rotatelogs /etc/httpd/logs/redmine-error.%Y-%m-%d.log 86400"
    CustomLog "| /usr/sbin/rotatelogs /etc/httpd/logs/redmine-access.%Y-%m-%d.log 86400" "%h %l %u %t %D \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""

Make sure that these directories are writable by the web user :  (nobody user is used for rack )

chown -R apache:nobody files/ log/ tmp/ tmp/pdf/

chmod -R 770 files/ log/ tmp/ tmp/pdf/

Now Change the ownership of the directory, test the configuration and start / restart apache :

chown -R apache:root /path/to/redmineDir/

service httpd configtest

service httpd restart


Quick Tips :

Clean up all the devel packages that were installed during installation :

yum remove '*-devel' make automake autoconf

Restarting Redmine :

touch /path/to/redmineDir/tmp/restart.txt

Migrating restored data from previous  version :

First Backup the database from old Redmine install, restore into the new database, sync the files directory from old installation to new installation and then run the migrate command :

cd /path/to/redmineDir

rake db:migrate RAILS_ENV=”production”


Email Settings :

Using Gmail as outbound SMTP :

    delivery_method: :smtp
      enable_starttls_auto: true
      address: "" 
      port: '587'
      domain: "" 
      authentication: :plain
      user_name: "" 
      password: "your_password" 

Using your own SMTP with authentication :

    delivery_method: :smtp
      port: 25
      authentication: :login
      password: redmine

Your own SMTP without authentication :

    delivery_method: :smtp
      port: 25
      authentication: :none

Using Local Sendmail :

    delivery_method: :sendmail


Common Issues and Resolutions :

Error while sending mail notificaiton  “ hostname was not match with the server certificate”

Add this line to the email settings :

openssl_verify_mode: none
  • mysql2 is not part of the bundle. Add it to Gemfile

If you get the above error, that means, mysql2 adapter has not been included in your Gem file , open the gem file in your redmine root directory and add it to the list :

gem 'rails', '3.2.8'
gem "jquery-rails", "~> 2.0.2"
gem "i18n", "~> 0.6.0"
gem "coderay", "~> 1.0.6"
gem "fastercsv", "~> 1.5.0", :platforms => [:mri_18, :mingw_18, :jruby]
gem "builder", "3.0.0"

Make it look like :

gem 'rails', '3.2.8'
gem "jquery-rails", "~> 2.0.2"
gem "i18n", "~> 0.6.0"
gem "coderay", "~> 1.0.6"
gem "fastercsv", "~> 1.5.0", :platforms => [:mri_18, :mingw_18, :jruby]
gem "builder", "3.0.0"
gem "mysql2", "~> 0.3.11"

Now run the bundle installer again with the following command :

bundle install --without postgresql sqlite test development

Now verify that the mysql2 adapter has been installed :

 bundle show mysql2

Now you should be good.. continue with the other steps from where you got this issue.


Clearing Redmine Cache

rake tmp:cache:clear RAILS_ENV=production
rake tmp:sessions:clear RAILS_ENV=production

Port Forwarding with Iptables

You can use port forwarding in iptables, to forward a port to private ip , here is how you can do that :

iptables -t nat -I PREROUTING -p tcp -d <listenip> --dport 22 -j DNAT --to <ipToForwardTo>:22
iptables -I FORWARD -p tcp -d <ipToForwardTo> --dport 22 -j ACCEPT

Setting Default permissions on installed packages

If you mess up the permissions on installed packages some how , you can set default permissions by issuing the following command :

for p in $(rpm -qa); do rpm –setugids $p; rpm –setperms $p; done

Function to write a message to a text file

Sometimes , we need to write some logging information to a text file in php. The following function takes  filename and message as the parameter and logs whatever you pass to the file with date and time.

function logToFile($fileName, $logMessage)
    $logDirectory = “/path/to/log/dir”;     
    $fileHandler = fopen($logDir . '/' . $fileName , "a");
    $logText = date("Y-m-d h:i:s") . "---" . $logMessage . "\r\n";
    fwrite($fileHandler, $logText);

PAM Login Notification Centos

Edit the file :  /etc/pam.d/sshd

vi /etc/pam.d/sshd

Add this line to the above file :

session optional     /path/to/

Now Create the Script that will send the notification :

vi /path/to/script

Paste The Following content, and adjust the admin email address :

[ "$PAM_TYPE" == "open_session" ] || exit 0
        echo "User: $PAM_USER"
        echo "Remote Host: $PAM_RHOST"
        echo "Service: $PAM_SERVICE"
        echo "TTY: $PAM_TTY"
        echo "Date: `date`"
        echo "Server: `hostname -s`"
        } | mail -s "`hostname -s` $PAM_SERVICE login: $PAM_USER" $adminEmail

Redhat Package Manager Tips & Tricks

View a List of packages installed by Date :

rpm -qa --qf '%{INSTALLTIME} (%{INSTALLTIME:date}): %{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' | sort -n