cli Archive

  • How To disable root access via SSH

    How To disable root access via SSH

    If you are planning to deploy openssh on any server, you must secure it from hackers. There are several programs on internet which can launch a brute force attack against your ssh server.

    Full Story

  • 10 Useful Linux Commands

    10 Useful Linux Commands

    Here’s a list of 10 commands which may come handy when using the command line in Linux: Search for all files modified in the last N days containing a specific text in their name find DIR -mtime -N -name "*TEXT*" For example: find ~ -mtime...

    Full Story

  • Post to Twitter from Command-Line

    Post to Twitter from Command-Line

    Ever wondered how to tweet from your BASH prompt? No? Well, now you can even though you never thought about it!

    Full Story

  • Some wget tricks

    Some wget tricks

    The wget is a command line utility that can download files from web servers and FTP servers.
    For example , you can download the DVD image of Karmic Koala using the following command.

    $ wget http://cdimage.ubuntu.com/releases/9.10/release/ubuntu-9.10-dvd-i386.iso

    If an FTP server requires a login and password, you can enter that information on
    the wget command line in the following form.

    $ wget ftp://user:password@ftp.example.com/path/to/file

    You can use wget to download a single web page as follows:

    $ wget http://unixlab.blogspot.com

    A file named index.html will be created in your current directory

    If you open this index.html in a web browser , you will find some of the links broken especially images. . To download all the images and other elements required to render the page properly, -p option can be used.

    $ wget -p http://unixlab.blogspot.com

    This will create a folder named unixlab.blogspot.com with index.html in it.

    But if you open the resulting index.html in your browser, chances are you will still
    have all the broken links even though all the images were downloaded. That’s because
    the links need to be translated to point to your local files. So instead, do this:

    $ wget -pk http://unixlab.blogspot.com

    Sometimes an HTML file you download does not have an.html extension, but ends
    in .php or .cgi instead. . If you wget files from such a site , your browser will complain when you try to open the file. To solve the problem , you can ask wget to append .html to such files using the -E option:

    $ wget -E http://unixlab.blogspot.com

    I use the following command line to keep a usable copy of the website on my hard disk.

    $ wget -mEkK http://unixlab.blogspot.com

    Full Story

  • Enhancing terminals with byobu on Ubuntu  9.10

    Enhancing terminals with byobu on Ubuntu 9.10

    Byobu is a Japanese term for decorative screens . A Japanese byobu screen looks as below.



    You can achieve similar decorative effects on your gnome terminal or xterm , or Konsole with Byobu software available from ubuntu launchpad. Infact, the new ubuntu 9.10 ships it by default. On earlier versions of ubuntu you can install byobu from the PPA here.

    On my jaunty box , I added the following to /etc/sources.list and imported the key form the PPA and installed it via apt,

    For trying out byobu , open a terminal and type

    $ byobu

    your terminal will immediately change to the following screen.



    The bottom line on the above picture indicates the present status of your system. It displays several useful parameters. Byobu can be customised to display several other informatiin.

    Infact ,byobu is an enhancement to GNU/Screen. So, all commands applicable to GNU/ screen will work with byobu. If you are not familiar with screen look at this page for some additional information

    Most of the settings of byobu are stored in a hidden directory named .byobu under your home folder. Most ot these settings can be modified from byobu itself. For customising byubu, press F9 . You will get the following screen.


    You can change the look and feel of your terminal as shown in the screen-shots below.




    For me the status notifications on the last line is very useful For adding additional parameters Press F9 and select status notifications , you will get the following screens. Choose whatever parameter you want to display as your default status line.





    To start byobu automatically when ever you launch a terminal on Karmic Koala desktop, select
    the last option as shown below. ( Be careful as screen sessions can become background processes. I suggest you GNU/screen documentation )


    and press enter.


    Now click on edit->> Profile Preferences-> Title and Command on your gnome-terminal and tick Run command as login shell.


    Byobu will be launched automatically next time when you start a terminal.

    Full Story

  • Ubuntu Command Line Tip #1 – Aliases

    Ubuntu Command Line Tip #1 – Aliases

    This is my first Ubuntut, and for it I'd like to start off with a productivity tip. We're going to create a bunch of aliases (shortcuts) for commands we use often.

    Full Story

  • Controlling Services from  Command line

    Controlling Services from Command line

    Before learning how to start, stop, and disable services from the command line, it's important to understand Ubuntu's startup process and how Ubuntu determines which programs to run when it starts.

    Ubuntu uses System V style init scripts. The init scripts are located in a special directory named /etc/init.d. Even though there may be several init scripts present in /etc/init.d , not all of them are run at boot time. Ubuntu organizes which scripts to run for different circumstances into runlevels; most Linux systems have seven runlevels, ranging from 0 to 6.

    A some of these runlevels are set aside for special states in a Linux system:
    Runlevel 0 Halts the system.
    Runlevel 1 Sets up single-user mode.
    Runlevels 2-5 Set up different multi user modes. Although, typically, only one or two of these are used by a distribution.
    Runlevel 6 Reboots the system.

    Each runlevel has a directory that stores symlinks to certain init scripts in /etc/init.d, which are started when that runlevel is selected and stopped when it is exited. Ubuntu puts these symlinks under /etc/rc.d for example, all runlevel 2 scripts are located in /etc/rc2.d/.

    If you look in one of these runlevel directories, you'll notice that many of the symlinks to scripts in /etc/init.d have odd names that begin with an S, K, or D; then a number; and finally the name of the script. Ubuntu defaults to runlevel 2, so here is a partial sample of my /etc/rc2.d directory:

    lrwxrwxrwx 1 root root 19 2009-05-13 09:58 S01policykit -> ../init.d/policykit
    lrwxrwxrwx 1 root root 15 2009-05-13 09:58 S10acpid -> ../init.d/acpid
    lrwxrwxrwx 1 root root 14 2009-05-13 09:58 S10apmd -> ../init.d/apmd
    lrwxrwxrwx 1 root root 18 2009-05-13 09:58 S99ondemand -> ../init.d/ondemand
    lrwxrwxrwx 1 root root 18 2009-05-13 09:58 S99rc.local -> ../init.d/rc.local
    lrwxrwxrwx 1 root root 19 2009-05-13 09:58 S99rmnologin -> ../init.d/rmnologin
    lrwxrwxrwx 1 root root 24 2009-05-13 09:58 S99stop-readahead -> ../init.d/stop-readahead

    As you can see, this directory is full of symlinks that point to a script in the init.d directory.
    The letter at the beginning of each filename tells init when to execute this script. If the script begins with an S, then init starts the script when it goes through the runlevel. If the script begins with a K, then init stops (or kills) the script when it changes to a different runlevel. If the script begins with a D, then that script is disabled for the time being, and init ignores it. init runs the scripts in numerical order, so the numbers in each script let you know in which order they are to be run. This is useful to ensure that dependent services start after the service they are dependent on.
    Ubuntu 9.04 uses upstart daemon to manage init scripts. The configuration files for upstart are located in /etc/event.d/ . ( On earlier systems, the start up was managed from /etc/inittab. You will not find this config file in new ubuntu releases.). When ubuntu boots up the default runlevel is determined by a small script in /etc/event.d/rc-default.
    Next, upstart loads any system scripts from a special system runlevel directory at /etc/rcS.d. These scripts load daemons and services that are vital to the boot process. Lastly, upstart runs any startup scripts for the default runlevel in alphabetical order. Scripts in /etc/rcS.d are run in runlevels 1 through 5, so you should generally leave them alone unless you know what you are doing. If you accidentally disable a crucial service, you may have to resort to a rescue disc to undo the mistake.

    Change the Runlevel

    You can change the runlevel yourself on the command line with the init command. To switch to single-user mode from the command line, type:
    $ sudo init 1

    If you're running X11 when you issue this command, beware, since it will kill X and your desktop environment! This command runs all of the shutdown scripts for your current runlevel and then any start up scripts for single-user mode. To change back to the default multi-user runlevel for Ubuntu, type:

    $ sudo init 2

    You can also use init to halt or reboot a machine: just change to runlevel 0 and runlevel 6, respectively.

    Manually Start and Stop Services

    You can start and stop scripts manually by running the script with the start or stop argument. For example, to stop the CUPS service from the command line, type:

    $ sudo /etc/init.d/cupsys stop

    To start the service back up, type:
    $ sudo /etc/init.d/cupsys start

    Most scripts also support a restart argument that will run stop, then start for you. Most init scripts are also configured to output the list of arguments they support when you execute them without any options:
    ~$ sudo /etc/init.d/cups
    Password:
    Usage: /etc/init.d/cups {start|stop|restart|force-reload|status}

    Disable Scripts from Starting

    To disable a script, you must know your default runlevel. On Ubuntu, the default runlevel is usually set to 2, but you may want to double-check your default runlevel before you start disabling services.

    You can find out your current runlevel with

    ~$ runlevel
    N 2

    As you can see, in this example, the default runlevel is in fact 2. Now change to the directory containing all of the scripts for that runlevel (/etc/rc2.d) and find the script you want to disable. To disable a service, just rename its script by changing the S to a D. For example, to disable the cupsys service, type:

    ~$ cd /etc/rc2.d
    /etc/rc2.d$ sudo mv S50cups D50cups

    To enable it again, rename it back by changing the D to an S:
    ~$ cd /etc/rc2.d
    /etc/rc2.d$ sudo mv D50cupsys S50cups

    You'll still need to stop the service as shown earlier if you want to shut it down right away, but renaming it will control whether it's started the next time you reboot (or change runlevels).

    Alternately, you can use the update-rc.d command to manipulate the starting /stopping of init scripts.

    Full Story

  • Tip of the Day: Prevent Exiting a Shell with Ctrl+D

    Tip of the Day: Prevent Exiting a Shell with Ctrl+D

    A normal Bash shell with exit the current session when pressing ^D (Ctrl+D), but this can be avoided by editing the ~/.bashrc file and adding the following line:

    export IGNOREEOF=1

    This command will set the $IGNOREEOF variable to true, preventing a single combination of ^D to exit. In order to close the session, you can still use exit or double ^D.

    Full Story

  • Tip of the Day: Resize Images from CLI Using ImageMagick

    Tip of the Day: Resize Images from CLI Using ImageMagick

    ImageMagick is a powerful set of tools used to perform various operations on images directly from the command-line. To install it in Debian type as root:

    apt-get install imagemagick

    Or in Ubuntu:

    sudo apt-get install imagemagick

    With the user password.

    Resize images using convert
    The tool we're going to need for this is convert, which is included in the imagemagick package. Here's one easy way to do it:

    convert -sample 50%x50% input_image.png output_image.png

    This will resize the input_image.png and make it half the original image.

    Another tool which does the same thing is mogrify, with the exception that it will overwrite the original image:

    mogrify -resize 320 filename.jpg

    This will resize filename.jpg using a width of 320 and keeping the aspect ratio intact.

    Full Story