Monday, November 12, 2012

How to Lock your Ubuntu computer using a Keyboard Shortcut

Here at Virtusa, I am using the Virtusa Operating System as the operating system for my PC. It is built on top of Ubuntu Kernal and therefore whatever the command that works on Ubuntu should work on that also unless it is specifically blocked by its development team. I had been a Windows user for a long time and therefore I had the habit of locking the computer using the keyboard shortcut Windows Key + L . Ubuntu does not have this keyboard shortcut inbuilt and therefore I created a custom keyboard shortcut to get it done.

First I searched in google for the command to lock the screen using the terminal. I found the following command and tried it in the terminal and it locked the screen.



   gnome-screensaver-command -l

Next thing was to execute that command as my own custom command. I created a new file with the name lockmyscreen, wrote the following shell script and saved.


    #! /bin/bash

  echo `gnome-screensaver-command -l`

Then I added the execute permission to the shell script I wrote.



   chmod +x lockmyscreen

I executed the script and it locked my screen.



   ./lockmyscreen

Next thing was to convert the shell script to a command. For that, I moved the script to the location /usr/bin/ where the commands contain. For that I needed root access.



   sudo mv lockmyscreen /usr/bin/lockmyscreen

I provided the root password when it prompted. Then I could run the script I wrote as a command in the terminal.



   lockmyscreen

It was successfully locking my screen. Next thing was to assign a keyboard shortcut to that command. I opened System -> Preferences -> Keyboard Shortcuts as shown in the image below.


Then it opened the following window and I pressed the button Add to add a new keyboard shortcut.


I gave a name for the command and typed lockmyscreen which was the name of the script I wrote in the Command text box. Then pressed the button Apply.


Then it listed down the newly added keyboard shortcut under the Custom Shortcuts list. The shortcut was Disabled by default as I had not assigned a shortcut yet.

I clicked on the text Disabled to add new shortcut. Then the text Disabled changed as New shortcut...
 

Then I pressed the windows key and while holding it I pressed the key L also. Then it added the shortcut and so I closed the window by clicking on the Close button.
After that I could lock my screen as I used to do on windows by pressing the keyboard shortcut Windows Key + L.

Following the same approach, you can write your own commands and assign keyboard shortcuts to them to perform your daily tasks easily.
Feel free to send me your comments.

Written by : Tharindu Edirisinghe

No comments:

Post a Comment