Linux: what is partition UUID and how to use it

If you have worked with Linux disks, you have seen device names like /dev/sda1. They work fine until you add or remove a disk — then the names can shift, breaking your mount points and scripts. UUIDs solve this because they are permanent and tied to the filesystem itself.

Instead of writing /dev/sda1 in fstab, use the UUID:

UUID=65b14742-9610-4289-8ea4-af8edcc5d8da /media/data ext4 defaults 0 2

There are a few ways to find the UUID of a partition:

sudo vol_id -u /dev/sda2
sudo blkid
ls -l /dev/disk/by-uuid/

On older systems vol_id was the standard tool, while blkid is the modern equivalent available on virtually all current distributions. You can also see the PARTUUID with blkid, which identifies the partition table entry rather than the filesystem — this is useful for booting with UEFI.

If UUIDs feel unwieldy, you can label a filesystem and use the label instead:

sudo e2label /dev/sda1 mydata
LABEL=mydata /media/data ext4 defaults 0 2

Labels are easier to read but not guaranteed to be unique. UUIDs are the safer choice for production systems.

Hope it works!

Launch remote GUI locally trough SSH

There are times when it is easier to change a setting through a GUI rather than the command line. You can launch a remote GUI application on your local machine through SSH.

Both machines need to be Linux with a desktop environment installed. I have tested this with two Ubuntu 10.04 systems.

Basic X11 forwarding

Adding the -X parameter to SSH enables X11 forwarding:

ssh -X user@remote-server

Once connected, any GUI application you launch from the terminal displays on your local machine:

gedit /etc/remote-config-file.conf

Trusted X11 forwarding (-Y)

Some applications need more X11 permissions than -X provides. Use -Y for trusted forwarding:

ssh -Y user@remote-server

Making it permanent with ~/.ssh/config

Host my-server
    HostName 192.168.1.100
    User myuser
    ForwardX11 yes

Enjoy!

Load LVM partition manually

Sometimes you have to load a logical volume that is not part of the main filesystem. Other times you might have to mount a volume from a live CD, perhaps to recover data from a system that is no longer booting.

In both cases it needs to be done manually.

Install the LVM tools

If lvm2 is not already installed:

sudo apt-get install lvm2

Scan for and activate volumes

If you can see the disk with fdisk but LVM does not recognise the volumes, the kernel may not have scanned for them yet. Start by scanning for physical volumes and volume groups:

sudo pvscan
sudo vgscan
sudo lvscan

pvscan detects LVM physical volumes, vgscan finds the volume groups, and lvscan lists the logical volumes within them.

Activate and mount

If the volume group is inactive (common after booting from a live CD or moving a disk to another machine), activate it:

sudo vgchange -ay

This activates all available volume groups. Now mount the logical volume:

sudo mount /dev/VolumeGroupName/LogicalVolumeName /mnt

Replace VolumeGroupName and LogicalVolumeName with the names shown by lvscan. If you are not sure what the volume is called, check /dev/mapper/ for device-mapper entries.

Troubleshooting

If lvscan shows your volumes but vgchange reports “Volume group not found”, check the output of vgscan carefully. Sometimes the volume group name contains a hyphen (e.g. my-server) which can confuse the command — try using the full path from /dev/mapper/ instead:

sudo mount /dev/mapper/my--server-myvol /mnt

Note the double hyphen — LVM represents the hyphen in the volume group name as — in the mapper path.

Enjoy!

IDE Netbeans 7.0 is out: PHP, Java SE/EE and C/C++

Netbeans 7 is out. I have been using it for PHP, at work as well as at home, for almost two years and I have to say it has become by far my favourite PHP IDE.

The Netbeans IDE started in 1996 for Java. PHP support came with version 6.5 in late 2008. Since then, every release has improved significantly.

What I like about it is that it just works out of the box — the PHP code completion, the debugger integration with Xdebug, the SVN/Git support built right in. You don’t need to piece together half a dozen plugins to get a productive environment.

One of my favourite features is the ability to search across files with Alt+Shift+O. It’s one of those things you don’t appreciate until you try going back to an IDE that doesn’t have it.

Curious about it? Download the version you need from the Netbeans site and give it a try.

Enjoy it!

Linux: How to forward system emails

By default, most Linux systems deliver system emails — cron output, monitoring alerts, security notices — to the local root mailbox. If you never check it, those messages pile up and you might miss something important.

The simplest way to deal with this is to forward them to an external address. The most common method is the /etc/aliases file. Add a line like:

root: you@example.com

Then run newaliases to rebuild the alias database. Local mail will be forwarded to the external address via your MTA.

For per-user forwarding, create a .forward file in the user’s home directory with your email address in it. This doesn’t need an alias rebuild and works even if you don’t have sudo access.

Enjoy!

Mediawiki: disable account creation

When you install MediaWiki, new user registration is enabled by default. Anyone can create an account and start editing. That’s fine for a public wiki, but not always what you want — especially for a private or company wiki where you want to control who has access.

There’s no toggle for this in the settings UI, but it’s a single line in the config file.

Disable registration

Add this line to your LocalSettings.php file:

$wgGroupPermissions['*']['createaccount'] = false;

This tells MediaWiki to deny the createaccount permission to the * group (which means all users, including unauthenticated visitors).

Allow only certain users to create accounts

If you want only logged-in users to be able to create new accounts (for example, so existing users can invite others):

$wgGroupPermissions['*']['createaccount'] = false;
$wgGroupPermissions['user']['createaccount'] = true;

Or if you want only sysops (admins) to create accounts:

$wgGroupPermissions['*']['createaccount'] = false;
$wgGroupPermissions['sysop']['createaccount'] = true;

After making the change, clear the MediaWiki cache if needed (php maintenance/runJobs.php) and the login/register page will immediately reflect the new permissions.

Enjoy!

How to configure sendmail as a SMART HOST

Sometimes I need to test the email functionality of a PHP website on my local machine. The best and easiest way I found so far is using sendmail as a smart host.

A smart host is a piece of software that routes email to an intermediate mail server rather than directly to the recipient’s server. The only thing you need is the SMTP address of your internet service provider. For example, my ISP is Virgin Media and their SMTP server is smtp.virgin.net.

First of all you have to install sendmail. On Ubuntu:

sudo apt-get install sendmail

Then open your configuration file:

sudo vi /etc/mail/sendmail.mc

and append or modify the following line to define your ISP SMTP server:

define(`SMART_HOST',`smtp.virgin.net')

Remove the “dnl” word at the end if there is one. Now login as root and regenerate the sendmail config file:

m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

and restart sendmail:

service sendmail restart

Now your local machine should be able to send emails. The easiest way to test is from the PHP interactive shell:

php -a
mail('myemail@domain.com','TEST','WORKING?!?');

Enjoy… :-)

How to install “as many Operating Systems as you want” in one machine

Do you often have the need, or want the pleasure, to try a new operating system?
Do you prefer using a “real machine” rather than creating a virtual one (like VMware or Virtualbox for instance)?
Have you got a spare machine for this purpose?
Ok, if you responded “Yes” to all the above, please read on…

Some theory
A hard disk partition can be:
– primary;
– extended;
– logical.

Primary partition
A hard disk can be divided in no more than four primary partitions. Microsoft Windows requires to be installed in a primary partition in order to boot. In some older versions it even requires to be in the first partition as Wikipedia says: “In DOS and earlier versions of Microsoft Windows systems, the system partition was required to be the first partition. Windows operating systems must be located in a primary partition. Other operating systems do not have this peculiar requirement.”

Extended partition
Instead of creating four primary partitions one can be “sacrificed” to be an extended one. A hard disk may contain only one extended partition which can be subdivided into multiple logical partitions.
The only purpose of an extended partition is to contain logical partitions.

Logical partition
These can be used as normal partitions except for the fact that Windows cannot boot from them. Linux can… :-) Nice penguin!

Practice
So, this is the “secret”. You can use logical partitions for installing all Linux flavours that you like so much and can’t live without (SWAP and other OSs) and keep the primary partitions for the lovely Windows.
To start partitioning your hard drive you can use gParted which is the best tool I’ve ever found for this purpose (or any other partitioning tool you like). gParted is included in Ubuntu but I’m quite sure it can be installed in other distros as well.

Now you should be ready to have a look at my experiment:
GParted - multi Operating Systems
As you can see I created an extended partition in the first one (“/dev/sda1”) and I kept a primary partition for Windows Xp (“/dev/sda2”).
Inside the extended partition I’ve created six logical partitions for some linux flavours all functioning, plus a SWAP (“/dev/sda5”) partition and a DATA (“/dev/sda12”) partition used to share files among the OSs. I’ve choosen “fat32” since some Linux distributions don’t support NTFS out of the box.

After all of this, my little hard disk still has 9 GB free. What other operating system should I install? :-)
All operating systems are managed by a boot loader (Grub2) which gives me the possibility to choose which OS to start. Grub2 is shipped with many recent distributions like Ubuntu Lucid for example.

In the near future I might write a post on how to recover a lost grub which makes the computer unbootable.
Enjoy the freedom to try all the operating systems you are curious about.

How to ssh to a server without password

If you connect to a remote server regularly, typing your password every time gets old fast. Key-based authentication is the standard way around this — it is more secure and more convenient once set up.

Generate a key pair

On your local machine, run:

ssh-keygen -t rsa

This creates a 2048-bit RSA key pair in ~/.ssh/. The public key is saved as id_rsa.pub, the private key as id_rsa. Keep the private key safe — anyone who has it can log into any server that trusts the matching public key.

Copy the public key to the server

Copy the public key to the remote server and append it to the authorized keys file:

scp ~/.ssh/id_rsa.pub my_username@mySshServer:~/.ssh/id_rsa_local.pub
cd ~/.ssh/
cat id_rsa_local.pub >> authorized_keys2

Make sure the permissions are correct on the server (~/.ssh should be 700, authorized_keys2 should be 600). If the permissions are too loose, SSH will ignore the key and fall back to password authentication.

Connect using sftp

You can also connect using SFTP with the same key pair:

sftp://my_username@mySshServer/home/my_username

Using ssh-agent to avoid typing your passphrase

If you set a passphrase on your key (recommended), you will be prompted for it the first time you use the key each session. You can avoid this by adding the key to ssh-agent:

eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa

Enter your passphrase once, and ssh-agent will remember it for the rest of your session. On most desktop Linux distributions, ssh-agent starts automatically when you log in.

Troubleshooting

If SSH still asks for a password, check these in order:

  • Server file permissions: ~/.ssh must be 700, authorized_keys must be 600
  • You appended the correct public key (compare with cat ~/.ssh/id_rsa.pub on your local machine)
  • The server’s /etc/ssh/sshd_config has PubkeyAuthentication set to yes
  • Check /var/log/auth.log on the server for specific error messages

Once it works, you can SSH into the server without being prompted for a password.

Enjoy!

How to create a simple WordPress Child Theme

A child theme lets you customise an existing WordPress theme without losing your changes when the parent theme updates. Every time the parent theme is updated, any files you edited directly get overwritten. A child theme avoids this.

Start by creating a folder and a style.css file that declares the parent theme:

/*
Theme Name:     My Twenty Ten Child
Theme URI:      http://www.opensourceisbetter.com/
Description:    Child theme for the Twenty Ten theme
Author:         Andrea
Template:       twentyten
*/

The Template line must match the folder name of the parent theme exactly. Activate the child theme from Appearance > Themes.

For the styles to work, create a functions.php file to enqueue the parent styles:

function my_theme_enqueue_styles() {
    wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css');
}
add_action('wp_enqueue_scripts', 'my_theme_enqueue_styles');

Now you can edit the child theme files freely. Add custom CSS to style.css, override template files by copying them from the parent theme, and add functionality through functions.php — all without touching the parent theme.

That is it!