Run root Commands in Linux (Ubuntu) Without Password

Terminal Windows

Terminal Windows

Recently a student of mine asked me about running sudo commands in a shell script without prompting for the password. I thought finding the solution would be as easy as typing the question in Google and reading the first result returned. But that was not the case.

The following steps will help you achieve the desired output:

  1. Create a new script file (replace create_dir.sh with your desired script name):
    vim ~/create_dir.sh

    The script will be created in the user’s home directory

  2. Add some commands that only a root or sudo user can execute like creating a folder at the root directory level:
    mkdir /abc

    Note:Don’t add sudo to these commands. Save and exit (using :wq!)

  3. Assign execute permissions to it using:
    sudo chmod u+x create_dir.sh
  4. Make changes so that this script doesn’t require a password.
    1. Open the sudoers file:
      sudo vim /etc/sudoers
    2. Add the following line at the end:
      ahmad ALL=(root) NOPASSWD: /home/ahmad/create_dir.sh

      Replace ahmad with whatever your username is. Also make sure this is the last line. Save and exit.

  5. Now when running the command add sudo before it like:
    sudo ./create_dir.sh

    This will run the commands inside the script file without asking for a password.

Note: Only the script file mentioned in the sudoers file is exempted from asking a password. The rest will behave as usual.

Smartly download stuff from the Command Line using wget

Terminal Windows

Terminal Windows

Normally you would be downloading files from the internet using your browser and its own download manager would be the right choice. However, what if you need to download files on the server which has command-line (terminal) only or you want to download a long list of files but one at a time or you want to limit the maximum download speed so that it doesn’t eat up all your limit. These are just a few of the cases in which wget command comes in handy.

Installing wget

For Linux, the fun part is that it is already installed on every Linux machine. You can verify this running the command:

wget --version

For Windows, there is a download available here. For simplicity, select the option labelled “Complete package, except sources”. Once downloaded, install it. The installation path on Windows XP is “C:\Program Files\GnuWin32\bin” but you can add it to the PATH environment variable to make it easily accessible.

For MAC OS, it doesn’t come installed but getting it is easy. Before installing wget, you need to get HomeBrew, the package manager for MAC OS. Go to http://brew.sh/ and install it by simply running the command mentioned there.

Once HomeBrew is installed, run the following command to install wget:

brew install wget

Once it is installed and ready, we can get started

Downloading a Single File

wget http://dummyurl.com/file.zip

This will start downloading the file to the current folder in which the Terminal prompt is. Besides downloading, it will display the progress in percentage, the download speed, total bytes downloaded and remaining time.

Downloading a Single File to a Different Location

The file downloaded in above command will be saved in the current working directory of the Terminal and with the name file.zip as it was its original name.

To save it with a different name:

wget -O new_name.zip http://dummyurl.com/file.zip

To save it in a different location, give the path also (which can be relative of absolute):

wget -O /home/user1/new_name.zip http://dummyurl.com/file.zip

Resume Download for a File

If your file was partially downloaded, you can resume it by using the -c switch, which is for continue.

wget -c http://dummyurl.com/file.zip

To Limit the Max Download Rate/Speed

wget --limit-rate=50K http://dummyurl.com/file.zip

This will not download at speed more than 50KB per second.

Download Files in the Background

If there are numerous task and you want that wget keeps on downloading them in the background:

wget -b http://dummyurl.com/file.zip

Download Multiple Files from a List of URLs

The wget command can take input URLs from a text file with a URL on every line and it will pipeline them for downloading:

wget -i list_of_urls.txt

These are only some of the scenarios I had to come across. I will inshAllah be updating the list of commands as the need arises.

Change Display/Screen Resolution using Linux Command

Display Application in Ubuntu

Display Application in Ubuntu

To change the Screen Resolution in Ubuntu, simply type Display in the Unity Launcher and run the application. The snapshot of the Display application is taken from Ubuntu 12.10 and is also the same in previous versions also. Similar application is available in other Linux distributions.

But there is a handy command that can do the same thing even faster or can help you create a script (depends how creative and productive you plan to be). The command is simple:

xrandr

This will display all the possible resolution profiles available for the current system (more can also be added and you can find help on http://www.x.org/archive/X11R7.5/doc/man/man1/xrandr.1.html).

To change to a known resolution:

xrandr -s 1024x768

And you have it! 🙂

File Synchronization with rsync

Microsoft SyncToy is a very helpful tool for 1-way or 2-way file synchonization. But it hasn’t been updated since 2009 and of course its not for Linux 🙂

rsync

Its a very good command based tool which can get the 1-way file synchonization done neatly (the logo does look unprofessional but this tool isn’t).

Installing rsync on Ubuntu

It is by default installed on Ubuntu (tested on both 12.04 and 12.10 distributions) but incase it isn’t, then run the following command:

sudo apt-get install rsync

Structure of rsync command

Following is the structure of the simple yet powerful rsync command:

rsync [options] [source-folder] [destination-folder]

Possible options

  • -a or –archive can be used for archive which means timestamps and ownerships will be retained.
  • -z or –compress can be used for compress mode which means bandwidth can be saved but CPU power will be consumed
  • -v or –verbose can be used to display information when rync is transfering, otherwise it does so silently.
  • -n or –dry-run can be used to display what will happen if this rync command is executed. No changes will take place
  • –progress does what the name says. It will display the %age, transfer rate etc.
  • –size-only will compare files based on their size instead of hashes. This way less CPU power is will consumed.
  • –delete will delete all the files from the destination folder that are not present in the source folder.

Source and Destination Folders

The trailing slash / at the end of the destination is important.

Rotate & Flip Video Files in Ubuntu (Linux)

Rotate Video Files

Rotate Video Files

The first thing that might pop-up in the reader’s mind is that why would anyone ever want to rotate a video? Well this need arose when I recorded videos using iPad (which always auto rotate the screen according to the user orientation). But when I transferred those to my PC, I realized that some needed to be rotated 90 degrees and some 180 or vertically flipped.

Ubuntu always has the answers to my questions. In a previous post in which I shared the commands on how to record screencasts in Ubuntu, I mentioned about avconv tool and how to install it with codecs. The same nice tool can be used to flip and rotate videos also.

To vertically flip a video:

avconv -i input_video.mov -c:v libx264 -c:a copy -vf "vflip" output_video.mov

To horizontally flip a video:

avconv -i input_video.mov -c:v libx264 -c:a copy -vf "hflip" output_video.mov

To rotate it 90 degree clock-wise:

avconv -i input_video.mov -c:v libx264 -c:a copy -vf "transpose=1" output_video.mov

The copy parameter means to keep the same audio encoding.

For further reading visit the official documentation: http://libav.org/libavfilter.html

Image editing using ImageMagick

To install the package:

sudo apt-get install imagemagick

To resize pictures:

convert source.jpg -scale 200x200 destination.jpg

Another option can be:

convert source.jpg -scale 200 destination.jpg

(This will scale the image to 200 width keeping the aspect ratio)

To know the file details, any of the following commands will work:

file source.jpg
identify source.jpg
identify -verbose source.jpg
convert source.jpg -print "Size: %wx%h" /dev/null

Automount Windows partitions in Ubuntu (Linux)

Ubuntu, Fedora and other flavors of Linux provide a simple way of just clicking on the available partitions and it will mount them for you as soon as you click them.

But what if you need to automount them so that whenever you boot the system, they are already mounted. Follow the below instructions:

Get a list of the UUIDs of the drives

sudo blkid

In my case I have C: and D: drives that I like to mount to Ubuntu.

Open the fstab file for automounting

sudo vim /etc/fstab

Add the following lines (Change in your case):

UUID=	/media/Windows	ntfs	default	 0	0
UUID=	/media/Data	ntfs	default	 0	0

Create the folder for mount point

sudo mkdir /media/Windows
sudo mkdir /media/Data

Finally, reload the new settings:

sudo mount -a

Download Youtube videos using Ubuntu (Linux) command

There are numerous Addons and Extensions for Web Browsers to download and even convert the downloaded video to your desired format. However, if you are thinking of making a script or a program to download it without the browser, this post is what you need.

Run the following command to install the program:

sudo apt-get install youtube-dl

To download any video, open it in the browser, copy the URL and paste it in the following command:

youtube-dl -o [filename].flv [Copied URL]

If you didn’t provide any path with the filename, it will download it to your home directory.

Fixing Download Errors

In case you get the following error message:

ERROR: unable to download video

Try the following to fix it:

sudo -s
youtube-dl -U
youtube-dl -t [Copied URL]

Install ImageMagick on CentOS 5.5

ImageMagick is an open source image editing tool that can work on a huge variety of formats. It essentially is a command line tool with APIs for numerous programming languages including .NET, PHP, Java, Python etc.

Download the package:

wget http://www.imagemagick.org/download/linux/CentOS/x86_64/ImageMagick-6.7.0-10.x86_64.rpm

Install all the missing dependencies:

yum install libICE libtiff libSM libXext libXt libtool-ltdl fontconfig

Install the ImageMagick Package:

rpm -ivh ImageMagick-6.7.0-10.x86_64.rpm

And its installed!

Creating Linux Command Shell (Bash) Aliases

Alias means a second name for an existing thing. Aliases for command means a shorter command for an actual lengthy command. This will help in remembering it. For example, the command to untar/unzip a compressed file is:

tar xzvf [filename]

This is ofcourse not easy to remember and this is where aliases comes into play.

In the home directory of every user, there is a hidden file named .bashrc which can be used to define aliases for that user.