Tuesday, February 10, 2015

How to run multiple simultaneous X Window sessions

If you share your Linux desktop machine with other users, you may occasionally need to switch from 1 X session to another. For example, you are busy writing code or blogging, and your spouse walks up to you and asks to 'quickly' check her email. You can log out so that she can login using her own account. But, the current context of your work is lost, and you need to re-establish it when you resume.

A better way is to have her run a second simultaneous X session. The steps are as follows:

  1. Switch to a virtual terminal.
    By default, six virtual text terminals are available to you (Ctrl+Alt+F1 to F6). Press Control+Alt+F1 to go to virtual terminal 1.

  2. Login as her.

  3. Execute the following command:

    $ startx -- :1

    This starts another X session using the first free graphical console. By default, 6 X consoles are available (Ctrl+Alt+F7 to F12). Your own existing X session is Ctrl+Alt+F7. The next free X console is therefore Ctrl+Alt+F8.

    To switch from her X session to yours, and vice versa, press Ctrl+Alt+F7 and Ctrl+Alt+F8 respectively.

You may use the above procedure to create up to 5 additional X sessions (Ctrl+Alt+F8 to Ctrl+Alt+F12). For each additional X session, increment the console number in both step 1 and 3. For instance, switch to virtual terminal 2 (Control+Alt+F2) to execute the command startx -- :2.

Sunday, February 1, 2015

Learning command arguments using ExplainShell.com

Suppose you come across the following command in a script. You are not quite sure what it does. Specifically, you want to know the meaning of each command argument.

$ wget --spider -r -nd -nv -H -l 1 -w 2 -o test.log http://example.com/

Traditionally, to find out what each argument does, you look up the man page for the wget command. Then, you scroll through page after page of information, and extract the description for each argument. This is a time-consuming, and eye-straining exercise.

Thanks to the Explainshell.com website. Now, you simply enter the above command on that website, and click a button.

The output clearly displays a description of each specified argument from the man page.

One caveat is that the information from Explainshell.com is sourced from the Ubuntu man pages. So, it is great for commands that you often run on Ubuntu or Debian systems. But, you won't find anything for Fedora/Centos/RedHat-specific commands such as yum.

Even with that limitation, Explainshell.com is a great learning tool for Linux command-line users. If you are interested in the inner workings of Explainshell.com, click this link.

Monday, January 26, 2015

Using gdebi to install and resolve dependencies for a local deb file

Debian comes with over 37,500 packages in its default distribution. Yet, occasionally, we still need to install a package which has not made it into the default distribution.

If your current system already has all the prerequisite packages installed, then life is good. You simply download the deb file, and install it with the dpkg command:

$ sudo apt-get update
$ sudo dpkg -i somelocal.deb

But, if you run into package dependency problems, you are on your own. The dpkg command does not resolve dependency problems for you.

For a Debian or Ubuntu system, a better way to install a local package is to use the gdebi command. The gdebi command accesses the repositories specified in your /etc/apt/sources.list file to resolve any dependencies.

First, install gdebi by running the following commands.

$ sudo apt-get update
$ sudo apt-get install gdebi

You need to be root in order to run the gdebi command to install a package.

$ sudo apt-get update
$ sudo gdebi somelocal.deb

Using the gdebi command saves you time if you run into dependency problems while installing a local deb package.

Sunday, December 21, 2014

How to create a swap file

Linux can be configured to use swap space, aka secondary disk storage, when physical memory is running low. Swap spaces can be allocated as disk partitions ('swap partitions') or as files ('swap files'). While swap partitions are generally preferred over swap files, if your system is a virtual private server (VPS) without a pre-configured swap partition, creating a swap file may be your only option. The following procedure describes how to create a swap file.

List swap spaces

Before you create a swap file, you should first check whether the system has any swap space pre-allocated. The easiest way is to run the free command.

$ free -h total used free shared buffers cached Mem: 497M 490M 6.2M 0B 14M 101M -/+ buffers/cache: 375M 121M Swap: 0B 0B 0B

The line labeled Swap above tells you that there is no swap space configured.

Alternatively, run the swapon command with the -s parameter:

$ sudo swapon -s Filename Type Size Used Priority

I prefer free because root privilege is not required to run the command.

Create swap file

Follow the steps below to create and activate a swap file.

  1. Create a new file pre-allocated with the desired file size.
    $ sudo fallocate -l 500M /var/swap.img

    The above command pre-allocates 500 megabytes for the file /var/swap.img.

  2. Secure the new file.
    $ sudo chmod 600 /var/swap.img
  3. Make a swap file.

    The following mkswap command sets up /var/swap.img as a swap file.

    $ sudo mkswap /var/swap.img Setting up swapspace version 1, size = 511996 KiB no label, UUID=a0a90414-adab-4c50-8b27-0d27f0c34448
  4. Activate the swap file.
    $ sudo swapon /var/swap.img

    After executing the above swapon command, verify that the swap file is indeed enabled.

    $ free -h total used free shared buffers cached Mem: 497M 464M 32M 0B 14M 104M -/+ buffers/cache: 346M 151M Swap: 499M 34M 465M $ sudo swapon -s Filename Type Size Used Priority /var/swap.img file 511996 35184 -1

    According to the above output, the swap file has been enabled. However, unless you complete the next step, the swap file will be disabled when you reboot the machine.

  5. Update file system table.

    Add the swap file to the file system table using the following command:

    $ sudo sh -c 'echo "/var/swap.img none swap sw 0 0" >> /etc/fstab'

Wednesday, December 3, 2014

How to change system timezone

When you initially install Linux, you specify the machine's timezone. After the install, you can manually change the timezone. The following procedure applies to Debian and Ubuntu systems.

Before you change the timezone, let's find out what timezone your system is currently in.

$ date Tue Dec 2 13:53:11 PST 2014

The above date command tells you that the system is on PST, aka Pacific Standard Time.

You can change the timezone interactively or through batch processing.

Interactive setup

The following command guides you through 2 screens to configure the timezone.

$ sudo dpkg-reconfigure tzdata

The advantage of specifying the timezone interactively is that you don't have to know the exact name of the timezone. The program will guide you to select your target timezone. But, if you want to automate the process through a shell script, please follow the batch method as explained below.

Batch setup

  1. Identify the name of the target timezone.

    Timezone data files are stored in the /usr/share/zoneinfo directory tree. Each continent has a corresponding subdirectory, e.g., /usr/share/zoneinfo/America. Each continent subdirectory contains timezone files named by cities in the continent, e.g., /usr/share/zoneinfo/America/Vancouver.

    $ ls /usr/share/zoneinfo/America

    Note the city where your system is located (or the nearest city in the same timezone). The timezone identifier is the concatenated continent and city names, e.g., America/Vancouver.

  2. Specify the timezone in /etc/timezone.
    $ sudo -s sh -c 'echo America/Vancouver > /etc/timezone'
  3. Run configure program.
    $ sudo dpkg-reconfigure -f noninteractive tzdata