Ever wanted to copy files from one linux server to another, via SSH? Here’s the command:

scp -P 22 -r /home/username/local-directory ip-or-hostname-of-remote-server:/home/username/remote-directory.

scp = Secure Copy.

-r = Recursive (copy the contents of the directory and the contents of any directories inside that directory).

-P = (specifying the port number).

22 = The port number SSH is running on, on the remote server. (That’s right, some of us who are little security conscious, don’t leave SSH running on port 22).

/home/username/local-directory = The local directory on the ‘source’ server, containing the files you want to copy to the remote servers

/home/username/remote-directory = The directory on the ‘destination’ server, where you want to copy the files to.

ip-or-hostname-of-remote-server = The example says it all. Replace this section with the remote IP Address or Hostname of the destination server.

Easy enough? If not, or you would like further examples or assistance, please feel free to ask you questions in the comments.

I came across a conundrum some time ago. I have a CentOS Linux based VPS running a few various different ‘processes’ for different purposes. My problem was that from time to time, these processes would stop or die, for whatever reason. Given that I require absolute uptime from these processes (or at least as close as I can get) I needed a script, a way to check if the process was running, and if not, to start it, all without my manual help. Now, Before you read any further, I just want to make it clear, I am talking about uncommon processes (not system services or daemons). To name a few:

Initially, I searched and searched the internet and found all sorts of solutions dating back to the 1990’s all the way through to 2010. I even managed to cobble something together, but it was pretty terrible and failed sometimes (more often than not actually).

Some readers may have noticed that one of my examples above, already contains an almost perfectly good solution!

Services for IRC (developed primarily by Andrew Church), contains an additional script (once compiled and installed) under the file name of ircservces-chk (located here: /usr/local/sbin/ircservices-chk on most linux distributions). The contents of this file looks like this:

#!/bin/sh
#
# Script to check whether IRC Services is running, and restart it if not.
# Usage: ircservices-chk [-q] [ircservices-options]
# -q: don't write any output
# ircservices-options: options to pass to ircservices executable
# If you change PIDFile in ircservices.conf, also change PIDFILE below.
#
# IRC Services is copyright (c) 1996-2009 Andrew Church.
# E-mail:
# Parts written by Andrew Kempe and others.
# This program is free but copyrighted software; see the file GPL.txt for
# details.

PIDFILE=ircservices.pid

if [ "X$1" = "X-q" ] ; then
exec 1>/dev/null
exec 2>/dev/null
shift
fi

ok=
if [ -f "/usr/local/lib/ircservices/$PIDFILE" ] ; then
pid=`cat "/usr/local/lib/ircservices/$PIDFILE"`
if echo "0$pid" | grep -q '[^0-9]' ; then
rm -f "/usr/local/lib/ircservices/$PIDFILE"
elif kill -0 $pid ; then
ok=1
fi
fi

if [ ! "$ok" ] ; then
"/usr/local/sbin/ircservices" "$@"
fi

This script is almost perfect for my use (it does an exactly perfect job for ircservices), but not quite. This script uses the process id (PID) of ircservices to check if it is running or not. That’s ok, but not all programs output their current process ID to a file based location on the system or in any way that’s useful to the end user. I added a line to the beginning of the script, using the pgrep command to output the PID of my selected process to a file.

pgrep process-name > /home/username/a-directory-of-your-choosing/a-file-name-of-your-choosing.pid

Obviously you would replace process-name with the name of the process you want to monitor and username with your actual username.

You can use the command ps aux to list all the processes currently running on your system and locate the process you want to monitor.

The ‘.pid’ file extension isn’t really necessary, however it might be handy for identifying what the file is, should you come across it later on down the track. Here’s my example for checking that ircbot is running:

#!/bin/sh
#
# Script to check whether IRC Services is running, and restart it if not.
# Usage: ircservices-chk [-q] [ircservices-options]
# -q: don't write any output
# ircservices-options: options to pass to ircservices executable
# If you change PIDFile in ircservices.conf, also change PIDFILE below.
#
# IRC Services is copyright (c) 1996-2009 Andrew Church.
# E-mail:
# Parts written by Andrew Kempe and others.
# This program is free but copyrighted software; see the file GPL.txt for
# details.
pgrep ircbot > /home/username/ircbot/ircbot.pid
PIDFILE=ircbot.pid

if [ "X$1" = "X-q" ] ; then
exec 1>/dev/null
exec 2>/dev/null
shift
fi

ok=
if [ -f "/home/username/ircbot/$PIDFILE" ] ; then
pid=`cat "/home/username/ircbot/$PIDFILE"`
if echo "0$pid" | grep -q '[^0-9]' ; then
rm -f "/home/username/ircbot/$PIDFILE"
elif kill -0 $pid ; then
ok=1
fi
fi

if [ ! “$ok” ] ; then

"/home/username/ircbot/ircbot" "$@"
fi

Here’s my final version.

#!/bin/sh
#
# Script to check whether a process is running, and restart it if not.
# Usage: running [-q]
# -q: don't write any output
# Props to Andrew Church (http://www.ircservices.za.net) for creating
# the original script ircservices-chk of which this script you are using
# now is a slight modification by Quin Rose of Mokona Modoki
# http://www.mokonamodoki.com/

pgrep ircbot > /home/username/ircbot/ircbot.pid

PIDFILE=ircbot.pid

if [ "X$1" = "X-q" ] ; then
exec 1>/dev/null
exec 2>/dev/null
shift
fi

ok=
if [ -f "/home/username/ircbot/$PIDFILE" ] ; then
pid=`cat "/home/username/ircbot/$PIDFILE"`
if echo "0$pid" | grep -q '[^0-9]' ; then
rm -f "/home/username/ircbot/$PIDFILE"
elif kill -0 $pid ; then
ok=1
fi
fi

if [ ! "$ok" ] ; then
"/home/username/ircbot/ircbot" "$@"
fi

To use this script, just copy and past the code above into your favourite editor and be sure to save the script, and make it executable with:  chmod +x filename

Last but not least, you don’t want to have to run this script all the time, and manually by yourself do you? Wondering if you can just get put it in a cron job and let it rip? Well, Yes! You can! Add the following line to cron for automatic (once a minute) process checking goodness:

* * * * * /home/andrew/ircbot/ircbot-chk -q

There are plenty of resources on the web relating to cron, so if you would rather have cron execute the script less often, you’ll need to do a little googling.

Adding the -q sends all output from the script to /dev/null (a black hole of sorts) thus not showing any output on the screen. You can test it with or without the -q.

If you have any questions or would like for information or examples, please do let me know in the comments, and I will be only too happy to be of assistance! This certainly helped me, so I thought I would share, so that it could help you too!

The contents of this guide should work perfectly fine on a base (yes that’s base, not bare bones) only installation of either CentOS 32/64 bit or RHEL 5.3 32/64 bit. If you have any troubles, please do leave a comment and I’ll do my best to assist you. This guide assumes you are setting up your machine to be a server, although the steps will work just fine on a machine configured to be a desktop.

Install CentOS 5.3 32/64 Bit

If you haven’t installed CentOS 5.3 already, you should do so. Kinda hard to use VMware Server 1.0.9 without an operating system. Most people want the host OS to have as little additional crap as possible. In regards to that, during the installation, make a point of not installing any additional packages, or package groups except the ‘Base’ group (oh and leave the base group in it’s default state, don’t go ticking on any extras, you’ll make the installation install more dependancies thus more crap). So in other words, only the ‘Base’ group should be ticked, untick the rest!

Nope, you won’t have any GUI, and even if you did, you still have to do the rest of the configuration at the command line anyway. VMware Server 1.0.9 host with GUI = bad idea, unless of course your host is also your desktop.

Once installed, configure your host as you like it from the ‘firstboot’ screen and than login as root. Here’s a few tips.

  • Disable any services you aren’t using.
  • Disable SE Linux
  • Unless you’re going to configure and use it with VMware Server 1.0.9 machines, disable ip6tables
  • Don’t go configuring special firewall rules just yet, leave that until later.

Even though it’s the host, its’ software should still be up to date. Issue the following command and download any updates.

yum update

Updates finished? Awesome! Lets reboot the server and start the next step afresh.

shutdown -r now

Install VMware Server 1.0.9 Required Components.

Plenty of you will argue about what you do and don’t need to complete the install. Well, I’ve completed this configuration of server with VMware Server more times than I care to remember now, and sometimes I needed all of these packages and sometimes I didn’t. No, I didn’t bother to find out why. Do you know why? Great! Put it in the comments!

Login in as root and issue the following commands one after the other. Yes, you can issue them all at once if you’re clever, I like to take my time, in case the worlds explodes.

yum install kernel-devel
yum install kernel-headers
yum install libXtst*
yum install xinetd
yum install gcc*

On a couple of occasions, I needed the following package – It could have been when I was using CentOS 5.3 64Bit

yum install libXdmcp-devel
yum install libXrender*

What does the ‘*’ mean? It means grab any packages that start with ‘anytextbeforethe*’

Installed all the packages? Fantastic! Lets reboot the server and start the next step afresh.

shutdown -r now

Install VMware Server 1.0.9

How you get the installation package of VMware Server 1.0.9 onto your server is up to you. Here are a few tips.

  • Download VMware Server 1.0.9 Here or Here
  • Make sure you register for a VMware Server serial number or numbers. It’s free.
  • You can use WinSCP to transfer VMware Server 1.0.9. to your server (it uses SSH). Get it Here or Here.
  • You could use the wget command with the direct link above, directly on your server, like this:
    • wget -c http://download3.vmware.com/software/vmserver/VMware-server-1.0.9-156507.i386.rpm
  • Burn it to a CD
  • Burn it to a DVD
  • Transfer it to a USB Drive

Once you have the VMware Server 1.0.9 installation file on your server, installing is pretty straight forward. If you’ve never installed a RPM file before, pay attention, people go to university for this stuff. Make sure you are logged in as root, and issue the following:

rpm -ivh VMware-server-1.0.9-156507.i386.rpm

Easy, no?

Depending on the power of your server, it may take a few minutes to install. Once installed, we have one last final step. No need to reboot this time!

Configuring VMware Server 1.0.9

Issue the following command. If some or none of the required packages are not installed, this will tell you straight away.

vmware-config.pl

or if you are so inclined:

/usr/bin/vmware-config.pl

Both do the exact same thing. Don’t run both (either by concatenating them both together or one after the other), that would just be silly and make you leave a comment about how you ran both and got an error message.

NB: If you are told that some packages or components are missing, I suggest going back and re-issuing the ‘yum’ commands above in the section ‘Install VMware Server 1.0.9 Required Components‘. Before you issue any commands, press ctrl-c to quit the setup program and return to the command prompt.

If you see something like the following, you are good to go:

Making sure services for VMware Server are stopped.

Stopping VMware services:
   Virtual machine monitor                                 [  OK  ]

You must read and accept the End User License Agreement to continue.
Press enter to display it.

The configuration settings of VMware Server 1.0.9 is now up to you. Here are a few tips:

  • Accept the agreement, type Y or Yes and press the enter key.
  • The defaults are all fine.
  • In most cases, Bridged networking is what you are after, so say ‘No’ to host only and NAT (unless you know what they are and you are going to use them)
  • Sometimes the installation would tell me that port 902 is unavailable. This is not true. Override by typing in 902, otherwise you’ll need to remember 904 (or whatever other number you select) and type it in every time when you use the VMware Server 1.0.9 client. Not cool.
  • At the very least, choose your own location for the storage of VMware Server 1.0.9 servers. I always put mine in /home/vmware (because I have /home on a separate partition)
  • Put your Serial Key in now, rather than later.

Installation and Configuration is now complete! If you need any further help / assistance, please, feel free to leave a comment

These are two solutions that worked for me. They are by no means the only solutions.

My Predicament.
A server (a Dell SC440), with two physical network interfaces. Our office has two ISP connections (DSL broadband). We have the one internal network range (192.168.1.0/24) with two gateways (1.254 and 1.221).

I have installed Promox on the server. I wanted to be able to choose which gateway the guest containers used, instead of them being locked into using the same gateway as the host server. Some of the guest OS containers will host services via the first ISP, and the others will host services via the second ISP. (obviously with firewall rules etc configured in our gateways). So the container gateway entries need to point to their respective ISP/Gateway.

Note: While the security advantage of venet over veth is certainly worthwhile in a hosting environment (and others as well), in my case, we have two hardware firewalls that haven’t let us down yet, and there is little to no concern for the possibility of an internal hacker (there are only three of us!)

Read More →