Tag Archives: server

Ubuntu Server: How to Attach and Mount a Hard Drive Tips, Tricks and Tutorials 24 JAN 2014

ubuntu orange logoThe other day we needed to pull data off a restored VM running an older LTS version of Ubuntu Server. My IT contact helpfully attached a small 500 MB drive to the VMware vSphere server and then left me to do the rest. This is what was needed to be done:

I first ran the fdisk utility to get a listing of devices currently seen by the system.

sudo fdisk -l

This gave me a list of all devices, from which I was able to determine the size and logical name assigned to the newly added disk, in this case a 500mb drive located as /dev/sdb.

Unfortunately for me, this time around the drive wasn’t actually picked up as being formatted, and as such I needed to first partition the disk using fdisk:

sudo fdisk /dev/sdb 

fdisk will display the following menu:

  Command (m for help): m <enter>
  Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

  Command (m for help):

We want to add a new partition. Type “n” and press enter.

  Command action
   e   extended
   p   primary partition (1-4)

We want a primary partition. Enter “p” and enter.

  Partition number (1-4):

Since this will be the only partition on the drive, number 1. Enter “1” and enter.

If it asks about the first cylinder, just type “1” and enter. (We are making 1 partition to use the whole disk, so it should start at the beginning.)

Now that the partition is entered, choose option “w” to write the partition table to the disk. Type “w” and enter.

If all went well (i.e. “The partition table has been altered!” appeared), you now have a properly partitioned hard drive that’s ready to be formatted. Since this is the first partition, Linux will recognize it as /dev/sdb1, while the disk that the partition is on is still /dev/sdb.

Next up, I needed to format the new partition.

There are a variety of options you could choose from, but seeing as I was working with Ubuntu server, ext3 made sense for this particular use case:

sudo mkfs -t ext3 /dev/sdb1

Of course, if you prefer to use something that would work under both Ubuntu and Windows, then FAT32 remains a good option:

sudo mkfs -t fat32 /dev/sdb1

(Note I had to do the whole partition and format thing because of the nature of drive plugged in. If this had not been the case, the system would have recognised the drive appropriately – as seen in the fdisk listing – and you would be able to skip straight to this mount part of the walkthrough).

With the disk partitioned, formatted and now ready for use, the next step is to actually make it usable on the system, in other words mount it.

First up, create a mount point, basically the path through which you will access it. (I would recommend using a mount point with “/media”, as it is the default used by Ubuntu.)

sudo mkdir /media/external

Now to actually mount the drive:

sudo mount /dev/sdb1 /media/external

Note, depending on the filesystem of your attached drive, you might have to alter the mount string accordingly. If the filesystem is FAT16 or FAT32 (like it is for most USB flash drives), and we want to mount it at /media/external (having already created the mount point):

sudo mount -t vfat /dev/sdb1 /media/external -o uid=1000,gid=1000,utf8,dmask=027,fmask=137

The options following the “-o” allow your user to have ownership of the drive, and the masks allow for extra security for file system permissions. If you don’t use those extra options you may not be able to read and write the drive with your regular username.

Otherwise if the device is formatted with NTFS, run:

sudo mount -t ntfs-3g /dev/sdb1 /media/external

And you should be good to go, happily copying data to and from /media/external. When you are done and need to plug out the device, first unmount it using:

sudo umount /media/external


sudo umount /dev/sdb1

Note that the unmount command will fail if your current working directory is in the /media/external path. You need to step out of the disk if you want to unmount it! :)

Related Links: https://help.ubuntu.com/community/Mount/USB, https://help.ubuntu.com/community/InstallingANewHardDrive

Ubuntu Server: How to Free up Disk Space Software & Sites 03 APR 2013

ubuntu-10-logoEvery now and then your server will throw up its hands and declare that it has run out of space, which means that you need to go in and figure out what is taking up all the space and then a) increase the amount of available disk space or b) delete or archive things in order to free up some disk space.

As it turns out, this is mostly a manual process of locating things and deleting or clearing out if possible, and more often than not, you’ll find that overzealous logging, which even logrotate couldn’t effectively deal with, sitting at the root of your problem.

Now we’re interested in b) for this particular post, so we start by doing a Disk Free check to get a better idea of our current situation:

sudo df -h

With that information in hand we then systematically look at the problem areas with targeted calls to Disk Usage:

sudo du -h โ€“-max-depth=1 /
sudo du -h โ€“-max-depth=1 /var/
sudo du -h โ€“-max-depth=1 /var/log/

Once you’ve identified a particularly large folder, check it’s contents with the usual human readable listing:

sudo ls -lha

Spot the overly large offenders and happily delete with the usual rm command. Note that for log files it might be necessary to first identify the controlling service and stop that, in order to allow you to remove the file. Also, a nifty trick if you don’t want to delete what could be important program files is to simply overwrite them with blank, thus leaving their group and file permissions perfectly intact.

How to Install Nagios on an Ubuntu Server CodeUnit 18 MAY 2012

Nagios is a popular open source computer system monitor, network monitoring and infrastructure monitoring software application. Nagios offers complete monitoring and alerting for servers, switches, applications, and services.

Nagios, originally created under the name NetSaint, was written and is currently maintained by Ethan Galstad, along with a group of developers actively maintaining both official and unofficial plugins. N.A.G.I.O.S. is a recursive acronym: “Nagios Ain’t Gonna Insist On Sainthood”, “Sainthood” being a reference to the original name NetSaint, which was changed in response to a legal challenge by owners of a similar trademark. “Agios” is also a transliteration of the Greek word which means “saint”.

Nagios was originally designed to run under Linux, but also runs well on other Unix variants. It is free software, licensed under the terms of the GNU General Public License version 2 as published by the Free Software Foundation.

To install the newest version on an Ubuntu server turns out to be quite trivial, thanks to a greatly streamlined installer. To grab for your server, run:

sudo apt-get install -y nagios3

Follow the prompts when asked, and when the final bits and pieces have been pulled down and installed, you should have a working Nagios installation accessible on your webserver http://localhost/nagios3/ with the username nagiosadmin and whatever password you had selected during the install process.

Config files which you can alter can be found in /etc/nagios3/conf.d/ and to start and stop nagios, you can make use of the service command:

sudo service nagios3 stop
sudo service nagios3 start


Related Link: http://www.nagios.org

How to Check if Your Ubuntu Server is a 32 bit or 64 bit Installation Software & Sites 13 APR 2012

Because you don’t want to waste time and bandwidth by accidentally downloading the wrong bit version of your operating system, it is pretty useful to know whether or not your server is currently using a 32 bit or 64 bit version.

To check this via the terminal, run:

uname -m

Its output can be interpreted as follows:

x86_64 = 64 bit
i686 = 32 bit
i386 = 32 bit


Ubuntu Server: How to Mount a USB flashdrive from a Terminal Tips, Tricks and Tutorials 15 APR 2011

By default, an Ubuntu server installation does not auto mount USB flashdrives that are plugged into it. Thankfully though, doing this yourself is not particularly difficult.

Plug your USB thumb drive into the machine and run:

sudo fdisk -l

This displays a list of devices currently detected. Locate your device in the list – it will probably be something like /dev/sdb1

Now we need to create a mount point for the device. It makes sense to create it in the /media folder, so run:

sudo mkdir /media/external

In the above example we have decided to name the mount point external. You can name it anything you want, but avoid using spaces for multiple words – rather join multiple words with an underscore _ character.

Now to actually mount the drive. If the device is FAT16 or FAT32 (which most USB drives are), run:

sudo mount -t vfat /dev/sdb1 /media/external -o uid=1000,gid=100,utf8,dmask=027,fmask=137

If however you are plugging in a NTFS external hard drive, you would run:

sudo mount -t ntfs-3g /dev/sdb1 /media/external

Done. You can now access the contents on the USB flashdrive by accessing /media/external.

When you finish using the drive, it is critical to unmount it before removing – this will prevent corrupt data and drive failures. To unmount:

sudo umount /media/external


Ubuntu Server: How to Enable the mod_rewrite Module in Apache CodeUnit 13 APR 2011

The extremely useful mod_rewrite or rewrite module is not enabled by default on a new Apache web server install. Luckily, this turns out to be pretty simple to rectify.

Using the a2enmod function, simply run:

sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart

The first line will enable the rewrite module (which should already be available) and the next restarts the apache web server to reflect the new module addition.


Ubuntu Server: Pointing Apache’s default /var/www Folder to a Different Folder CodeUnit 11 APR 2011

If you build a Zend Framework website then you will know that it is customary practice to move most of the work out of the publicly accessible web folder and then simple feed back the results to this folder so that the visitors can see a page off your site. So all projects always contain a /public folder which technically should be the root of your Apache webserver.

If you don’t in particular want to mess around with the various rights and permissions already linked to the default /var/www folder, it turns out that there is a elegantly simple solution to your problem – simply symlink the /var/www folder to your zendproject/public folder!

rm -r /var/www
ln -sv /home/user/zendproject/public /var/www

What we are doing above is simply removing the existing /var/www folder and then creating a symlink version of it that points to our project’s public folder. So now anyone browsing our web server will be served pages out of the /public folder, without them knowing any different, keeping the rest of our Zend project out of any potential snooper’s hands!


How to Enable SSH Remote Access on a New Ubuntu Server 10.10 Install CodeUnit 08 APR 2011

Being able to administer your Ubuntu servers remotely is one of the most important aspects when running your own server environment, which makes the extremely useful OpenSSH server package an almost critical part of your system after install.

Because it is not installed by default (though the option does form part of the installer process), it is pretty simple to enable afterwards.

Once you have finished your install and logged in, update your apt-get sources by entering:

sudo apt-get update

(If you don’t do this and try installing openssh-server directly, you’ll most likely encounter a “openssh-server has no install candidate” error. Try it for yourself if you don’t believe me)

Once this has completed updating your packages, install the OpenSSH server onto your system with:

sudo apt-get install openssh-server


You may want to further configure SSH access on your system (like change the default port for added security for example), which can be done by editing the config file at /etc/ssh/sshd_config and make the required changes (disabling root logins is also always a good idea).

Note, you can check if SSH has been installed and is running by running the following PS check:

ps -aef | grep sshd


How to Change the MySQL Time Zone on an Ubuntu Server CodeUnit 18 FEB 2011

If you wish to explicitly set the time zone for your MySQL running on an Ubuntu Server, here is how to do it.

First open your global MySQL my.cnf configuration file:

sudo nano /etc/mysql/my.cnf

Scroll down the file to location the [mysqld_safe] section. Add the following line:

timezone = GMT

Obviously you would set it to what you wanted to use, be it UTC, GMT+2 or whatever. Save your changes and restart MySQL:

sudo service mysql restart


(You can check by creating a table with a timestamp column and then inserting a record with NOW() as the value. This should then show up correctly when queried.)