Sunday, July 20, 2014

How to change window manager for GNOME

If I ask a Linux user what desktop environment he is running, most likely he can tell me the correct answer - GNOME, KDE, Xfce, LXDE, etc. But if I ask him what window manager he is running, I won't be too surprised if he can't answer me. In fact, not long ago, I did not know that myself.

The Window Manager dictates how various visual elements - windows, panes, icons, etc - look, and how users may interact with these elements. There are many window manager choices: Metacity, Mutter, Compiz, Openbox, etc.

The key is that you are not locked in to any window manager. If you don't like your current window manager, change it. This post explains how to change the window manager, specifically for the GNOME desktop environment.

Before we change it, let's find out which window manager is currently running. To do that, you need to install and run a tool named wmctrl.

$ sudo apt-get install wmctrl $ wmctrl -m Name: Metacity Class: N/A PID: N/A Window manager's "showing the desktop" mode: N/A

The above output tells us that Metacity is the current window manager.

The procedure to change the window manager is:

  1. Choose a new window manager, say Mutter.
  2. Install the new window manager.
    $ sudo apt-get install mutter
  3. Change window manager.

    If you just want to try out the window manager, then execute the following command in your desktop environment:

    $ mutter --replace &

    The window manager is switched on-the-fly. However, Mutter does not persist after logging out. When you login to X, the window manager is reverted back to Metacity.

    To make Mutter your new default, create the file ~/.gnomerc like this:

    $ cat >> ~/.gnomerc export WINDOW_MANAGER=mutter

Wednesday, July 16, 2014

Hide command from bash command line history

Linux is known as a very secure operating system. But, it is not going to save us if we voluntarily or unknowingly expose ourselves to unnecessary danger. For instance, a password-authenticated command may allow you to specify the password right on the command-line.

$ mysql -u root -pMyPassword

The command you just executed - with the mysql root password - gets recorded in the shell command history file. For bash, the history file is ~/.bash_history. This is not desirable for security.

$ tail ~/.bash_history ... mysql -u root -pMyPassword $

The most effective solution is to break the bad habit: don't enter any password on the command line. For the above example, make mysql prompt you for the password:

$ mysql -u root -p Enter password: mysql>

Failing that, you can mitigate the security risk by hiding a command from the command line history. Note: the technique below only works for the bash shell. It won't work for zsh, tcsh, etc. (If you do know the trick for these shells, please let us know through comments).

The bash trick is to enter one or more leading spaces before the actual command.

$ mysql -u root -pMyPassword

A leading blank. Just like that, and the command you enter won't be written into the command history file.

Saturday, July 12, 2014

Share the keyboard and mouse using Synergy - part 2/2

Part 1 of this 2-part series covers x2x, a nifty software tool that lets you use the keyboard and mouse of one X terminal to control another. If you want to control more than 1 other machine, or the machines are on different platforms (Linux, Mac OS X, and Windows), Synergy is the tool.

Installation

My primary machine runs Debian 7.5 - aka Wheezy. I want to use the keyboard and mouse of this machine to control my secondary machine which runs Fedora 19. I have both good news and bad news for you - regarding installation.

First, the good news. Both the Debian and Fedora releases have included Synergy in their official package repositories. Installation is as simple as:

# For Debian $ sudo apt-get install synergy # For Fedora $ sudo yum install synergy

Now, the bad news. The 2 Synergy versions from the respective repositories, 1.3.8 for Debian Wheezy and 1.4.10 for Fedora 19, are incompatible. Starting Synergy on the secondary machine generated this error message:

2014-07-09T20:33:52 WARNING: failed to connect to server: incompatible client 1.3 You're using different versions of synergy on the client and server. You should use the same version on all systems.

The solution I recommend is to download the latest stable release - 1.5.0 at the time of writing - directly from the Synergy download site.

To install:

# For Debian $ sudo dpkg -i synergy-1.5.0-r2278-Linux-x86_64.deb # For Fedora $ sudo rpm -i synergy-1.5.0-r2278-Linux-i686.rpm

Setup

First, configure Synergy on the primary machine. Then, the secondary machine. The setup procedure is as follows:

  1. Run Synergy.

    You will find the Synergy program under the Accessories menu for Debian GNOME, and Applications/Utilities for Fedora KDE.

  2. Synergy Premium or not.

    Synergy Premium is the non-free version. Not interested.

  3. Specify Server or Client.

    For the primary machine - the one with the keyboard and mouse that you want to use - select Server. Select Client for the secondary machine.

  4. Enable Encryption.

    After you click Finish, which screen to display next depends on whether you selected Client or Server.

  5. Configure Server (For primary machine only).

    The following screen is displayed for configuring the primary machine.

    Click Configure Server.

    By default, the grid contains a single node labeled with the hostname of the primary machine ('panther'). Double click the node to edit its settings.

    Optionally, enter a new Screen Name, say 'Desktop'. Then, add the hostname ('panther') as an alias.

    Next, add a new node representing the secondary machine to the grid. Drag the monitor icon, and drop it at the grid location relative to how it is physically positioned with respect to the primary machine. In my case, the secondary machine is located to the left of the primary machine.

    Edit the still unnamed secondary machine. Give it a meaningful screen name. Add the hostname as the alias.

  6. Configure Client (For secondary machine only).

    Specify the IP address of the Server (the primary machine).

  7. Click Start.

    This starts the actual Synergy Server - or Client - program using the latest configuration data.

Now, you can slide the mouse of the primary machine to the left, crossing the left edge, and into the screen of the the secondary machine. You can return to the primary machine by sliding the mouse to the right, crossing the right edge.

Auto-starting Synergy

Instead of manually launching Synergy, you can set up auto-start for Synergy like any other X application. For GNOME 3, execute the command gnome-session-properties to bring up the Startup Applications Preferences app. For KDE 4, follow this chain of menus: Applications / Settings / System Settings / System Administration / Startup and Shutdown.

Command-line interface

You can launch Synergy using the command-line interface.

For the primary machine:

  1. Prepare the Synergy configuration file (~/.synergy.conf).
    $ cat > ~/.synergy.conf section: screens Laptop: Desktop1: end section: links Desktop1: left = Laptop Laptop: right = Desktop1 end section: aliases Desktop1: panther Laptop: localhost.localdomain end
  2. Start the Synergy Server.
    $ synergys -f

    The -f option means that the program is run in the foreground. The log messages are displayed in the terminal. Very useful for troubleshooting.

For the secondary machine, execute synergyc with the IP address of the primary machine:

$ synergyc -f 192.168.1.103

Thursday, July 3, 2014

Share the keyboard and mouse on two X terminals - part 1/2

I have a desktop and a laptop on my workstation, side by side. No KVM switch. Each has its own display, mouse, and keyboard. I thought I could always slide over to use the other computer. Wishful thinking at its finest.

I started investigating KVM options. I like the idea of having both displays visible simultaneously. Therefore, the final solution only needs to share the mouse and the keyboard. Part 1 of this 2-part series focuses on x2x, a software KVM solution for 2 computers. Part 2 is on Synergy, a software KVM solution that supports more than 2 computers.

My primary machine is the desktop which runs Debian 7.5 (aka Wheezy). With x2x, I use the desktop's keyboard and mouse to control my secondary machine, the laptop which runs Ubuntu 12.04 (aka Precise Pangolin).

Installation

You only need to install x2x on the secondary machine, the one whose keyboard and mouse you are NOT using.

Before the install, verify that the X Server on your secondary machine supports the XTEST extension.

$ xdpyinfo -ext XTEST |grep XTEST XTEST XTEST version 2.2 opcode: 141

If the secondary machine runs on a recent Debian or Ubuntu release, the install is as easy as:

$ sudo apt-get install x2x

Unfortunately, recent Fedora and other Red Hat-based distributions do not pre-package x2x in their official repositories. This is not totally surprising because x2x is currently not being actively developed. Its most recent release was back in 2003. Part 2 of this series introduces an alternative tool named Synergy that works for Fedora.

If you are successful in installing x2x on a recent Fedora release, please share your experience by leaving a comment.

Setup

Before you run x2x, verify that X forwarding over SSH is enabled on the secondary machine. Specifically, the /etc/ssh/sshd_config file on that machine must contain the following line:

X11Forwarding yes

Restart the ssh daemon if you had to edit the sshd_config file.

$ sudo service ssh restart

For details on SSH X forwarding, please refer to my earlier post.

Running x2x

Execute a customized version of the following command on your primary machine, the one with the mouse and keyboard that you want to use.

$ ssh -X 192.168.1.111 x2x -west -to :0 peter@192.168.1.111's password:

The parameters to modify are:

  1. IP address

    This is the IP address of the secondary machine.

  2. Location of the secondary machine relative to the primary.

    My secondary machine is placed to the left of my primary, hence -west. When the mouse crosses the left edge of the primary display, the mouse pointer slides over to the secondary display. From there, when the mouse crosses the right edge of the secondary display, the mouse pointer slides back to the primary. Depending on the relative machine location, you can specify instead -north, -south, or -east.

Now, the two X displays are virtually integrated. Move the mouse of your primary display to the left, crossing the left edge. Notice that the mouse pointer lands in the secondary display. Any keystrokes you enter with the keyboard of the primary display appear inside a window on the secondary.

x2x is not currently under active development. But if your task is to share the keyboard and mouse between 2 Debian-based systems, x2x is a simple and effective solution. If you have more than 2 computers to share, or the computers are non-Debian, or even non-Linux (Mac OS X, Windows), consider using Synergy which is explained in the next post.

Wednesday, June 25, 2014

How to resize a picture using command-line tools

Occasionally, I need to resize an image. For instance, Google+ refuses to use my favorite portrait as my profile picture. The reason is that the portrait is smaller than the minimum requirement of 250 x 250 pixels.

To scale the image, I can opt for a GUI tool such as GIMP or Pinta. This post explores the scaling of pictures using command-line tools.

We will use tools such as convert and identify from the ImageMagick package. To install ImageMagick on Debian:

$ sudo apt-get update $ sudo apt-get install imagemagick

identify

To know the current dimension of the image, run the identify command:

$ identify portrait1.jpg portrait1.jpg JPEG 320x237 320x237+0+0 8-bit DirectClass 20.5KB 0.000u 0:00.000

The result includes the image width and height in the number of pixels (320x237). From that, we know that the image's height is below the minimum requirement.

convert

To scale the height dimension to 250 pixels:

$ convert -resize x250 -strip portrait1.jpg myProfile.jpg $ identify myProfile.jpg myProfile.jpg JPEG 338x250 338x250+0+0 8-bit DirectClass 35.2KB 0.000u 0:00.000

Note that the width is scaled up proportionally (to 338 pixels). To manually scale the width using convert, specify -resize 338x on the command line.

$ convert -resize 338x -strip portrait1.jpg myProfile1.jpg $ identify myProfile1.jpg myProfile1.jpg JPEG 338x250 338x250+0+0 8-bit DirectClass 35.2KB 0.000u 0:00.000

You can also use convert to dress up the picture, for instance, add a 3-pixel gray frame.

$ convert -resize x250 -strip -frame 3x3 -mattecolor "#999999" portrait1.jpg framedProfile.jpg

If optimizing file size is a major concern, you should investigate the use of the -quality option.

In general, image quality and file compression have an inverse relationship: higher image quality means lower file compression. The quality value ranges from 1 (lowest quality and highest compression) to 100 (highest quality but lowest compression).

$ convert -resize x250 -quality 95 -strip portrait1.jpg myProfile95.jpg $ $ ls -al myProfile*.jpg -rw-r--r-- 1 peter peter 19267 May 31 16:11 myProfile95.jpg -rw-r--r-- 1 peter peter 35160 May 31 15:31 myProfile.jpg

Specifying a quality of 95 (as compared to the default) resulted in a 45% reduction of the output file size. Some experimentation with quality values is needed to achieve the balance of minimal file size and acceptable image quality.