Home –  Tips
Category Archives: Tips

How to run sudo command without password on Linux?

There comes a time when you want to execute a command with root privileges but you don’t want to enter root password again and again. May be you want to allow a user to be able to execute one particular command but can’t share root password or some command needs to be executed on boot but requires root..sounds familiar? Keep reading.

The kind of approach we will be using is very often frowned upon. Mainly because it beats the purpose of sudo i.e. allowing only privileged users to execute certain commands. It is also looked upon as security breach, but you are fine if you know what you are doing.

You can run sudo command without password on linux by modifying /etc/sudoers file which maintains – which user can execute what. Before editing this file, lets take a backup – just in case – it gets messed up.

ayushhgoyal@linuxstall:$ sudo cp /etc/sudoers /root/sudoers.bak

/etc/sudoers file can be edited in any editor of your choice but it is highly recommended to use visudo command. If there is a syntax error in sudoers file then you will be locked out of shell and we don’t want that. While visudo command checks for correct syntax and restricts user from saving a corrupt sudoers file. Enter following command to edit sudoers file:

ayushhgoyal@linuxstall:$ sudo visudo

For example, if we want user “ayush” to be able to execute, say, “visudo” without asking for password then we will have to append following line in sudoers file in the end.

 ayush ALL = NOPASSWD: /usr/sbin/visudo 

This needs to be added at the very last of sudoers file because if there are matching entries then last line will override all previous occurrences.

This is how my sudoers looks after editing:

sudoers_sudo

Now, user “ayush” won’t be prompted to enter password while executing “sudo visudo”.

You can replace your own username and command(s) you want to execute without entering root password:

 [username] ALL = NOPASSWD: [command] 

This can be extended for multiple commands by comma separating them.

Add auto-complete to “yum”

We really enjoy the auto complete feature in bash but it would be great to have it with “yum”. Debian based Linux Users already have it. Red Hat based distros do not ship with auto-complete enabled in “yum” which would be great if they do. Meanwhile here is a small trick to use mighty auto complete with “yum”, this will work with Red Hat as well as Cent OS and Fedora (all versions).

  • Change to root
 su - 
  • Install the package: bash-completion
 yum install bash-completion 

And you are good to go. You can now type “yum install “ and hit [TAB] to load suggestions just like in bash.

How to display a digital clock in Linux terminal?

 

A clock is nothing special, we have tons of it but when it comes to linux the joy of accomplishing anything on command line is heavenly. In this document we are going to see how to display a digital clock in terminal (which updates). You can see the current data/time with Linux’s default command date. We will be playing with this very command to do the intended. To retrieve only the time from date command, we can add “+%r” or “+%T”    to date command.

 

With this “data” command you can print the system time. The logic behing building a digital clock is to get continuos output.  One way to do this is to use “watch” command along with “date” command. Thus to get a continuous digital clock you can execute following code:

watch -n 1 date +%r

 

You will find following result:

 

date linuxstall

 

Now, if you want a clear and formatted output of current time then you can go for this second method. This time we are going to use “echo” command  to display the date and in addition to that, we will put it in infinite loop which will update every 1 second- giving a feel of continuous digital clock.

The code goes like this:

clear; while true; do echo -e \\b\\b\\b\\b\\b\\b\\b\\b`date +%T`\\c ; sleep 1; done

 

You will see something similar to this:

 

 

 

 

Lets break our code down:

 

“echo” is used for displaying the output of “date” command.

\\b is to delete the previous output. We have used “%T “ with date which gives 8 characters long output. That is why we have used \\b 8 times. In case you wish to use “%r”, you will have to use \\b for 11 times as it produces detailed output.

\\c is used to check “echo” from creating new line.

Your own command line password generator!

random password from command line

When you need to create a random password, stop brain storming over random characters as you have Linux command line at your disposal. It can accomplish things perfectly and, there are multiple ways to do it. Today we are creating our own command line password generator. We have assembled some ways to create a random password. We will start off with an easy one, its easy to remember:

Using md5sum

It doesn’t give you enough options but its random enough.

date +”%N” | md5sum If you want some more control, keep reading.

For rest of the following random password commands, you are allowed to modify them according to your requirement like restricting the number of characters returned.

Using /dev/urandom

It is a built-in feature. Following command return only those characters which you need to use in a password.

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32};echo;

Another way to do it but in reverse order:

tr -cd '[:alnum:]' < /dev/urandom | fold -w30 | head -n1

Using SHA

Date is hashed via SHA, passed on to base64 and 32 characters appear in output.

date +%s | sha256sum | base64 | head -c 32 ; echo

Using Strings

String command filters and returns printable straings:

strings /dev/urandom | grep -o ':alnum:' | head -n 30 | tr -d '\n'; echo

Using urandom (again!)

This one is less complicated:

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6 ; echo ;

Using dd command

Ever thought dd is capable of doing this?

dd if=/dev/urandom bs=1 count=32 2>/dev/null | base64 -w 0 | rev | cut -b 2- | rev

Using openssl’s rand

It may not be available on your system:

openssl rand -base64 32

If you feel like using some method again and again, you can save yourself the trouble of executing the command by putting it into a function and placing that function in ~/.bashrc. For an instance, if you want to create a function for SHA one here is the code:

genpass() { date +%s | sha256sum | base64 | head -c 32 ; echo } We are calling our random password generator as “genpass”.

All the commands are tested on our own system, screenshot:
random password from command line

So whats your favorite method? Post it in comments.

Unity keyboard Shortcuts

unity keyboard shortcuts

unity keyboard shortcuts

Your Unity keyboard shortcuts documentation is here! Bookmark this page for future reference or download the wallpaper containing all unity keyboard shortcuts, download link in later half of this page.

A “Super” user will make most out of his keyboard with less/no use of mouse as it slows one down. Here we have documented some keyboard shortcuts for Unity which can help you get your task done more efficiently and quickly. Unity shortcuts, well most of them, start with tapping SUPER key. Since 12.04, tapping Entershows up following screen:

unity keyboard shortcuts 12.04

Launcher Shortcuts

The launcher can be started with Super key.
Super can be used along with numbers to invoke or focus an application. Numbers run from top to bottom on dash such as top most icon corresponds to number 1. For example, to open or to set your focus on second icon from top : Super + 1
Using Shift will open the second instant of an application if one is already open.
To open the trash: Super + T
To put focus on Launcher, use Alt + F1. Henceforth Enter will open the application, arrow keys can be used to move focus like they always do.
Terminal window launches open with Ctrl + Alt + T

Dash Shortcuts

Open the dash by tapping Super key. It opens when the key is released and when it is pressed so it might feel a bit slow.
Move to the next lens: Tab (11.10+ only)
Move to the previous lens: Shift + Tab
Dash’s special mode for running commands: Alt + F2
When the dash starts, the focus is on search box. When search results are in, arrow keys can be used to do usual and Enter launches the choice. It also has its own version of “I’m feeling lucky”. When the search is still going on, tapping Enter twice will invoke the first search result.
To open application lens: Super + A
To open Files and Folders: Super + F
For music lens: Super + M(11.10+ only)

Panel Shortcuts

Open the first menu on panel: F10. Arrow keys for moving through menus.
You can get to session menu by hitting F10 followed by left arrow key.
Get out of menus with Esckey.

Window Management Shortcuts

Spread mode: Super + W (It zooms out all windows on all workspaces)
Minimize all windows: Super + D. Restore by hitting it again.
Switch between applications: Alt + Tab
Switching between an application’s windows: Alt + `. It switches only between windows related to that application.

Window placement Shortcuts

Maximize window: Ctrl + Alt + 0
Position window in bottom-left: Ctrl + Alt + 1
Position window in bottom-half: Ctrl + Alt + 2
Position window in bottom-right: Ctrl + Alt + 3
Position window in left side of screen: Ctrl + Alt + 4
Position window in middle of screen: Ctrl + Alt + 5
Position window in right side of screen: Ctrl + Alt + 6
Position window in left top: Ctrl + Alt 7
Position window in top half: Ctrl + Alt + 8
Position window in right top: Ctrl + Alt + 9

Workspace Management Shortcuts

(Expo mode) Zoom out on all windows which makes management easier: Super + s
Expo mode in current workspace only: Shift + Alt + ↑
Change workspace: Ctrl + Alt + ↑/↓/←/→
Position window on new workspace: Ctrl + Alt + Shift +↑/↓/←/→
Lock current screen: Ctrl + Alt + L

Screenshot Shortcuts

Screenshot of current screen: PrtScn
Screenshot of current window: Alt + PrtScn

Wallpaper

These unity keyboard shortcuts can be found in following wallpaper:

unity keyboard shortcuts

Above wallpaper is in English, we also have its Spanish translation:

unity keyboard shortcuts spanish

View log files in real time

log files in realtime in linux

log files in realtime in linux

Most of the log files in linux can be found in /var/log . You can go to this directory with cd command but root permission is required.

cd /var/log

To view the log files in real time:

tail -f /path/toFile.log

For example, to view the live version of yum log files execute tht following command:

tail -f /var/log/yum.log

Linux Command Line tips that every Linux user should know

Below is the collection of Linux command line tips which I’ve found useful for Linux users. To get more information about the command mentioned below just open your terminal and type man <command>.

Things a Linux user must learn

  • Learn bash: No need to refer a lengthy bash guide or something else. Just read the complete man page of bash (man bash).
  • Learn vim: You might be using Emacs or Eclipse for your work all the time but nothing can compete vim.
  • Learn ssh: Learn the basics of passwordless authentication.
  • Learn basics of bash job management: Using &, Ctrl-C, fg, bg, Ctrl-Z, jobs, kill.
  • Learn basic commands for file management: ls and ls -l, less, head, tail and tail -f, ln and ln -s (learn the differences between hard links and soft links), chown, mount, chmod, df, du (du -sk *).
  • Learn basic commands for network management: dig, ifconfig.
  • Learn how to use grep, find and sed.
  • Learn how to use aptitude or yum (depends on the distro) to find and install packages.

For daily use

  • In bash, you may use Ctrl+R to search in command history.
  • In bash, you may use Ctrl+W to delete the last word, and Ctrl+U to delete the complete line.
  • Use cd – command to go back to the previous working directory.
  • Learn how to use xargs.

$ find . -name \*.py | xargs grep some_function

$ cat hosts | xargs -I{} ssh root@{} hostnameX

  • Use pstree -p command to get see the process tree.
  • Learn various signals. For example, to suspend a process, use kill -STOP [pid]. Type man 7 signal in terminal for complete guide.
  • If you want to keep running a background process forever then you can use nohup or disown.
  • Use netstat -lntp command to see what the processes are listening. You should check about lsof also.
  • In your bash script you can use subshells to group commands.

# do something in current dir

(cd /some/other/dir; other-command)

# continue in original dir

  • Trimming of strings: ${var%suffix} and ${var#prefix}. For example if var=foo.pdf, then echo ${var%.pdf}.txt prints “foo.txt”.
  • The output of a command can be treated like a file via <(some command). For example, compare local /etc/hosts with a remote one: diff /etc/hosts <(ssh somehost cat /etc/hosts)
  • Know about “here documents” in bash.
  • Learn how to redirect both standard output and standard error via: some-command >logfile 2>&1.
  • You should know about ASCII table (with hex and decimal values). Type man ascii in terminal.
  • While working remotely via ssh, you should use screen or dtach to save your session.
  • For web deveopers use of curl and curl -I, wget etc is useful.
  • To convert HTML page to text file: lynx -dump -stdin
  • If you must handle XML, xmlstarlet is good.
  • In ssh, learn how to port tunnel with -L or -D (and occasionally -R). Also learn how to access web sites from a remote server.
  • If you were typing a command but then changed your mind, Press Alt+shift+3. It will add # at the beginning and enter it as a comment.

Data processing

  • Learn about sort and uniq.
  • Learn about cut, paste, and join.
  • Learn how to get union, intersection and difference of text files.

cat a b | sort | uniq > c # c is a union b

cat a b | sort | uniq -d > c # c is a intersect b

cat a b b | sort | uniq -u > c # c is set difference a – b

  • Summing all numbers in the second column of a text file, code given below is probably 3X faster and 3X shorter than equivalent Python.

awk ‘{ x += $2 } END { print x }’

  • Learn about strings and grep command.
  • To split files into different parts learn about split (to split by size) and csplit (to split by a pattern).

System debugging

  • To know the status of your disk, cpu or network use iostat, netstat, top (or the better htop), and (especially) dstat.
  • To know your system’s memory status use free and vmstat command.
  • Use mtr which is a network diagnostic tool.
  • To find out which process or socket is using bandwidth, try iftop or nethogs.
  • You may use ab tool which is helpful for quick checking of web server performance.
  • For more serious network debugging take use of wireshark or tshark.
  • Learn how to use strace, and that you can strace a running process (with -p). This is helpful if your program is failing, hanging, or crashing, and you don’t know why.
  • Use the ldd command to check shared libraries.
  • Learn how to connect to a running process with gdb and get its stack traces.
  • Knowledge of /proc is very helpful. Examples: /proc/cpuinfo, /proc/xxx/smaps, /proc/xxx/exe, /proc/xxx/cwd, /proc/xxx/fd/.
  • When debugging why something went wrong in the past? To know about this use the sar command. It collects, reports and saves system activity information.

PS: I think I have missed some tips because they didn’t come in my mind at the moment. If you know some good command line tips then please share them in the comment. Thank you :)

Edit: I found some useful tips from reddit users which they gave after reading this post.

1. ifconfig is deprecated, alternative of it is ip.
2. Use of aliases is also an important thing which I forgot to mention.

Cheat Sheet

Download or bookmark the cheat sheet given below. It is very useful.

linux command line cheat sheet

Noticed “rm -rf / – make computer faster” under file commands in the cheat sheet? Don’t ever do that, that will delete all of your files. Do it on your enemy’s system :P

How to install RPM package on Ubuntu?

install rpm on ubuntu

install rpm on ubuntu

Ubuntu users are used to using Synaptic for their software needs and the other user chunk has apt-get via command line. But Ubuntu users don’t have to be sad for those packages which are still distributed in RPM format. Installation of RPM packages can be checked here.

If an Ubuntu user wishes to install a RPM package, a utility is created for this purpose and is called Alien. This doesn’t guarantees that the package will work, its actually a software that converts format of the package. In order to install Alien some prerequisite software are also required including gcc and make.

To install Alien utility, following code should be executed:

sudo apt-get install alien dpkg-dev debhelper build-essential

CONVERTING RPM TO DEBIAN

For performing conversion, run the following command:

sudo alien SomePackage.rpm

To continue with the installation, dpkg tool can be used as follows.

sudo dpkg -i SomePackage.deb