Jump to content

Kali Linux - Basic Terminal Uses


Cyb3rShot
 Share

Recommended Posts

As a Penetration tester we use a lot of commands on our daily basis. In our previous articles we have used a lot of commands. But here we will learn basic uses of terminal and some basic commands that will help a lot on our penetration tester journey.

Basics of Kali Linux Terminal

First of all we need top open our terminal window from our Kali Linux desktop. We also can use CTRL+ALT+T key combination to open the terminal window directly from our keyboard. Kali Linux default terminal window looks like following screenshot:

kali linux terminal
Kali Linux default terminal

Let's learn some basics of terminal. We can work on text based things using terminal window. We can write commands, then press Enter ⤶ key to run/execute the command. Sometime things are messed up then we need to clear the terminal using clear command or CTRL+L to clear the terminal. To open a new terminal window from our current terminal session CTRL+SHIFT+T.

To complete the command or the filename on terminal we can press the TAB key. If there are some files starting with same name then whenever we press TAB key it will display all the options in place. We should open our terminal window and practice these things while reading this article.

For an example we have two files with same name at starting test.sh and test.txt on our home directory. When we press the TAB key then we can see that we got the both options, as we can see in the following screenshot:

tab key to complete the command

If we run a command and then we need to stop it's execution we need to press CTRL+C key combination. To close the terminal window we can press CTRL+D key combo or use exit command.

We can also shut down and restart our system using terminal window. To shut down our system we need to use poweroff and for restart we need to use reboot command with root privilege.

To check our recently used commands on terminal we can use history command, and to use any command used before (reverse command search) we can use CRTL+R and then type the part of the command then terminal will suggest the command. As we can see in the following screenshot:

reverse command search
CTRL+R, then we just type his and it suggest history

Not only in Kali Linux, Linux in general we need to understand there are lots of redirections in terminal window. For an example we have to write our file list (ls) output on a text (txt) file e need to run following command:

ls> ls-list.txt

We can see the output in the following command:

ls list file

Using the above command we save the output of ls command on a text file and provide the text file a name (ls-list.txt), and we redirected the output by using a > (grater than) character.

We also do the opposite by redirecting (printing using cat) the text file contents into the terminal window by using the < (less than) character.

cat < ls-list.txt
printing on terminal using less than

There is another redirection we need to know is the command pipe. In short, we can combine the output of each command and use it on next command using | character.

command 1 | command 2 | command 3

For an example we need to read a file then short the results and finally use grep command to filter out some text strings. Here we are going to extract files starting with 'test.'. So we need to use following commands combining with |

cat ls-list.txt | sort | grep test

We can see in the output in the following screenshot:

combining commands

Basic Kali Linux Commands

Now, let's drive into Kali Linux usage and explore some basic Kali Linux (Linux, in general) commands.

Man Pages

Most of the executable programs on the Linux command line contains a formal piece of documentation is called manual pages or man pages. A special program called man is used to view these pages. Man pages generally have a name, a synopsis, a description of the command's purpose, and the corresponding options, parameters, or switches. Let's look at the man page for the ls (list) command:

man ls

This will show us the manual of ls command, as we can see in the following screenshot:

Exploring the manuals for the ls command using man

To know more about a command we can search a keyword. For example, we need to learn about the file format of /etc/passwd file. We can apply following command to learn more about this:

man passwd

The above command will show information about passwd command as we can see in the following screenshot:

 

manulas for passwd command

Also we can use -k flag with man to do a keyword search.

 

man -k passwd

We can see the output on the following screenshot:

manual options for passwd command

We also can filter out the search by using regular expression.

man -k '^passwd$'

In the above command, the regular expression is enclosed by a caret (^) and dollar sign ($), to match the entire line and avoid sub-string matches. The output shows in the following screenshot:

filtering man page search results

We can now look at the exact passwd manual page (5) we are interested in by referencing the appropriate section:

man 5 passwd
man options of passwd command

Man pages are usually the quickest way to learn more about a Linux command. So we need to take some time and explore the man pages.

Apropos

By using apropos command we can see a list of all topics in the man pages. Although this is a bit raw, it's often helpful for finding a specific command based on the description. For an example , we want to partition a hard drive but can't remember the name of the command. We can figure this out with an apropos search for "partition".

apropos partition

We can see the commands list with description in the following screenshot:

apropos

Check that apropos have similar output like man -k, in fact both are the same.

List

The ls command prints a basic file listing on the directory to the terminal window. We can modify the output results with various flags. Like -a flag is used to display all files (including hidden files) and the -1 option displays each file on a single line, which is very useful for automatic scripts.

ls command

Change Directories

Linux does not use Windows-style drive letters (C:\). Here, all files, folders, and devices are baby of the root directory, represented by the / character (see our Kali Linux file system article). In our terminal can use the cd command followed by a path to change to the specified directory. The pwd command will print our current directory (which is helpful if we get lost inside files) and running cd will return to the home directory (/home/username). To understand this we need to check the following screenshot and practice it by our own.

changing directories

To return back from a directory to it's parent/previous directory we can use cd .. command.

Creating Directories

We can use mkdir command followed by the name of our new directory to create a new directory. Directory names can be contains space in middle, but when we are using command line interface it will be easier to work with directory names using underscores or hyphens instead.

To create a new file we can use touch command followed by the name of our new file. Example of mkdir and touch command is shown in the following screenshot:

 

creating directories
We also can create multiple directories at a same time using -p flag. -p is capable to create directories inside parent directory. Suppose we need to add 2 directories inside our newly created (above example) directory (which is /home/kali/new_folder/baby-new-folder). We can do it from our home by using -p as shown in the following command:

 

 

mkdir -p /home/kali/new_folder/baby-new-folder/{testing,info,exploit}

We can see the output in following screenshot:

 

Creating diirectories advanced way

 

Searching for Files

 

There are three most common Linux commands for searching files on terminal, those are which, locate and find. Utilities of these commands are similar but work and output of these utilities are different.

Which

which command searches between the directories that are defined in the $PATH environment variable for a given file name. This variable contains a listing of all the directories that Kali Linux searches when a command is applied without its path. If a match is found, which returns the full path of the file as shown below:

which command

Locate

The locate command is the quickest way to find the locations of files and directories in Kali Linux. To do the search on a much shorter search time, locate searches a built-in database named locate.db rather than checking the entire hard disk. This database is automatically updated on a regular basis by the cron scheduler. To manually update the locate.db database, we can use the sudo updatedb command.

locate command

Find

The find command is the most complex and flexible tool in these three. Understanding it's syntax sometimes very hard, but it is very powerful than a normal search. In the following screenshot we did the most basic search using find command, where we start our search from root directory (/) and look for the filename starts with sbd.

find command

Where which and locate command searches files by using their names, find can search files by it's name, type, size, time, permissions etc. find is an complex yet very powerful search tool. We can know more about it here.

In our this part we just covered the basics terminal uses and some basic Linux commands. We will about more commands on our upcoming parts. Hope this article was enjoyable and informative.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...