Home –  command line
Tag Archives: command line

How to make password visible in linux terminal

Whenever you need to execute a command with root privileges / with sudo – you have to enter password. One gets used to not see the password as he types but it can make few people uncomfortable while entering long and complex passwords. If one messes up one character, the whole password needs to be re-entered because we are just.. not sure. This stops now.

Today lets see a tweak that will make password asterisks visible as one types.

We are going to add “pwfeedback” in sudoers file and we will be good to go.

Launch terminal using Ctrl + Alt + T.

Run “sudo visudo” and enter password when prompted (This is the last time you won’t get to see the password asterisks while typing).

sudo visudo

sudo visudo

Note: By default, the file is opened with nano editor. The reason behind this is while saving the sudoers file, if the syntax is wrong, user will not be able to run any command using sudo or root privileges. With nano editor, syntax is checked upon saving. So it is always recommended to edit sudoers file via nano.

You will see sudoers file like one below. Using arrow keys, navigate to the end of line:

Defaults env_reset

2

We need to append this line with “,pwfeedback”. Result should look like this:

Defaults env_reset,pwfeedback

3

Save with Ctrl + X and then typing y when prompted. You will be prompted to enter a filename, press “Enter” to accept default.

4

Now close and re-launch command prompt to see if this worked. You’re welcome ;)
This is how it looks on my terminal:

5

Thank us by sharing this post.

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.

How to view contents of a file in Linux

view file in linux

You can use any of the method given below to view contents of a file in Linux. Let us say the file name is “filename”. Then you can view its content…

1. using cat command

chankey@linuxstall:$ cat filename

or

chankey@linuxstall:$ cat < filename

2. using head command

chankey@linuxstall:$ head filename

3. using tail command

chankey@linuxstall:$ tail filename

4. using pg command

chankey@linuxstall:$ pg filename

5. using more command

chankey@linuxstall:$ more filename

6. using paste command

chankey@linuxstall:$ paste filename

7. using grep command

chankey@linuxstall:$ grep '.*' filename

8. using cut command

chankey@linuxstall:$ cut -c 1- filename

9. using while loop

chankey@linuxstall:$ while read line
>do
>echo $line
>done < filename

10. using xargs command

chankey@linuxstall:$ xargs -L1 < filename

11. using sed

chankey@linuxstall:$ sed '' filename

or

chankey@linuxstall:$ sed -n 'p' filename

or

chankey@linuxstall:$ sed -n '1, $p' filename

12. using awk

chankey@linuxstall:$ awk '1' filename

or

chankey@linuxstall:$ awk '{print;}' filename

or

chankey@linuxstall:$ awk '{print $0;}' filename

13. using perl

chankey@linuxstall:$ perl -pne '' filename

or

chankey@linuxstall:$ perl -ne 'print;' filename

or

chankey@linuxstall:$ perl -ne 'print $_;' filename

If you know some more ways then share them in comments.

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.

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