CHAPTER 5:- Linux Terminal, Basic Commands

CHAPTER 5:- Linux Terminal, Basic  Commands

It is extremely important to know about the Linux terminal and commands. Not in great 
detail, but this primary knowledge will help you immensely in the future. The more you 
delve deep into the world of ethical hacking, the more you will start feeling that you need 
to know more about the Linux system. This book will not take you that far. But a very basic 
knowledge is necessary so that you can understand what is going on around you.
It might seem repetitive, but I would like it to be cemented in your mind that 
without knowing Linux properly you can’t go deep into the mysterious world of ethical 
hacking. So you must know basic commands first. These commands will tell you about 
the computer itself. It will tell you the location of file system—where you are on your 
computer. By these commands you can change the permission of a file system, copy, or 
permanently remove a file. You can add a new user to your system. You can have a listing 
of files that are currently in the directory where you are. This listing includes the hidden 
files. In a nutshell, you can at least do the basic operations through your keyboard without 
using your mouse pointer. That is great from the perspective of a beginner, I presume.
To begin with, let us first start Kali Linux. In the following image you will see a full 
screen representation of Kali. I am going to explain a few things first, so that as a beginner 
you will learn what you need to know first about Kali

The image above is showing the full screen view of Kali Linux. On the left panel on 
the top, there is the browser, “Iceweasel.” Next follows the command line tool. We need 
that tool pretty often in the coming lessons. The command line tool or terminal basically 
deals with all types of keyboard inputs. The good programmers hardly use a mouse 
pointer. They are more comfortable with this terminal and keying. The file system follows 
it. If you click on it, it will open up a window just like any Windows NT version. You will 
see various directories and folders like “Home,” “Downloads,” “Pictures,” etcetera.
Let us start with the command tool by opening it. You can make it look bigger. Just 
use your “control” and “shift” keys with the “+” sign.
In the following image you will see a few starting commands that we usually type to 
know what kind of files we have in some directories or folders.
What does the image show?

It shows that I have typed “ls” first. What does that “ls” command mean? It stands for 
listing. I tell Kali to show the listing of files and folders that you have and, in a fraction of 
second, it shows me all it has.
Next I have used the “cd” command. What does that mean?
This “cd” command stands for “change directory.” You see in the image that I have 
changed the directory to “home” and issue the “ls” command again to see what it has. It 
has one folder called “sanjib” and a file. The folder “sanjib” means the “root” or system 
itself has a user called “sanjib”. Now as a root or administrator, I have created that user so 
that at the beginning I can log in as “sanjib”. You can create several users in a Linux system 
so that from various machines they can log into their files and folders. But the users will 
never have the root privilege. They can’t penetrate into the administrator’s space but the 
root or administrator can always see what the users are doing. As a root, an administrator 
can create or delete any user.
From this place you can guess what is happening. We change the directory and look 
at what “sanjib” has in its directory “Downloads.”
Next we learn about the “pwd” command. It states your position. As a root, if you are 
at “Home” directory and issue a “pwd” command, it has output like this:
root@kali:/home# pwd

It says you are at “/home” directory. This “pwd” command is important when 
you have to control a large complicated system. Often you might forget where you are 
working. Usually if you want to go back to the previous directory, you need to type this:
root@kali:/# cd /home/sanjib/
root@kali:/home/sanjib# cd ..
It means you first go to “sanjib” directory and then come back with a “cd” command 
having two dots.
Next we learn about the “cp” command. This command stands for copy. You can 
copy a file from one destination to the other. We have seen that in our “home” directory 
we have a file, “” Let us copy this file to the “Documents” 
directory of user “sanjib”.
root@kali:/home# cp -v /home/sanjib/Documents/
'' -> '/home/sanjib/Documents/'
Now we would like to go to the “sanjib” documents folder and see whether the file 
has been properly copied or not.
root@kali:/home# cd sanjib/Documents/
root@kali:/home/sanjib/Documents# ls
I have changed the directory to “sanjib/Documents” and issue the “ls” command to 
see the listing. It shows the file. So it is working properly.
You can learn about any command very easily. You just need to add a “—help” 
command like this: “cp –help.” It spits out everything about that command and it is very 
verbose. It tells you about any command in full detail.
Another very important command is “mv”. With this command, you can move any 
file from one folder to another folder. This command is more or less like a “cp” command. 
But there is a major difference. This command completely moves the file from one place 
to the other. Another important command is “cat.” You can read any text file with the help 
of this command.

I have a folder called “Writing” and have some documents over there. Now with the 
help of this command we can read any text file. Remember it is true only for a text file. For 
an experiment, I wanted to read a file with extension “.odt” and the next image shows you 
how it looked on the terminal.

In this part I want to show another trick that is often being used in Linux. Suppose 
you want to write a text file very quickly. You can use “nano”. It comes with every Linux 
distribution. Just type “nano” on your terminal and it will open up a text editor on the 
terminal itself. The next image shows you how it happens.

Now you can safely read this new file, “novel.txt”, with your “cat” command. All you 
need to do is issue a command on your terminal like this:
cat novel.txt 
It will read your file on the terminal itself.
Now it might be a good idea to edit this file. You can edit it on the terminal using 
“nano”. In that case, you need to write on your terminal this command:
nano novel.txt
This will tell “nano” to open the file. The rest is the same. You can edit any portion 
and, with the “control” and “o” key, you can save it again. Then you can exit the file with 
“control” and “x”

VI,” “VIM,” or “NANO” are extremely popular.
Now we are going to learn a very important Linux command called “grep”. This 
command does some sort of searching inside a file and it does it in a very interesting 
manner. Let us first see what we have in our root directory.
We issue a command like this on our terminal and see the output.
hagudu@hagudu-H81M-S1:~$ cd /etc/apt
hagudu@hagudu-H81M-S1:/etc/apt$ ls
apt.conf.d sources.list trusted.gpg trusted.
preferences.d sources.list.d trustdb.gpg trusted.gpg~
As you can see, we have changed the directory to “/etc/apt” and see the listing. We 
find many files there and presently we are interested in the “sources.list” file. We can use 
the “cat” command to read the file but we have something different in mind.
We would like to search some particular word and want to separate them and see 
them in segregation. The command “grep” along with another command, “|” (pipe), will 
help us in doing so.

We actually tell the terminal to display the content of “sources.list” first and then 
pipe that term to our searching process. Let us see how it works.
If we simply write a command like “cat sources.list”, it will display a long listing of 
the sources of this Linux system. You can write and see them. But we are interested about 
searching the word “src” and want to see how many times that word has been used in the 
So the final command and the output are like this:
hagudu@hagudu-H81M-S1:/etc/apt$ cat sources.list | grep src
deb-src trusty main restricted
deb-src trusty-updates main restricted
deb-src trusty universe
deb-src trusty-updates universe
deb-src trusty multiverse
deb-src trusty-updates multiverse
deb-src trusty-backports main 
restricted universe multiverse
deb-src trusty-security main restricted
deb-src trusty-security universe
deb-src trusty-security multiverse
# deb-src trusty partner
deb-src trusty main
# deb-src trusty universe
It is interesting to note that we first issued a command like this: cat sources.list | 
grep src
And the long output that follows that command has all the statements that have “src” 
in it.
We can even filter the source file more distinctly. We can narrow down our searches 
more and tell the terminal to find the word “src” only with small letters by writing down 
this command:
cat sources.list | grep –i src
In the future, we will use this “grep” command extensively to scan a network with a 
particular word.
Another important command is “echo”. This command literally echoes everything 
you write on your terminal. You can also do something more with this command. You can 
even change a text file with this command.
Previously we have written a text file “novel.txt” and saved it on our home directory. 
Now we are going to overwrite that file with this “echo” command.
hagudu@hagudu-H81M-S1:~$ echo "I DON'T LIKE THIS NOVEL ANYMORE SO I CHANGE 
IT" > novel.txt
hagudu@hagudu-H81M-S1:~$ cat novel.txt

We have first echoed some text on our terminal, then we used “>” (greater than sign) 
to put that text into the file “novel.txt”. In the next command, we have again used the “cat” 
command to read the file “novel.txt” and found that the file has been changed.
Now we will learn how to make directories in Linux. There is a very useful command: 
“mkdir”. It plainly means “make directory.” Let us make a directory named after this 
project: “Ethical Hacking.” You may guess that the command is extremely simple: mkdir 
Ethical Hacking
No, it is not. In this case, if you write that way, Linux terminal understands something 
else. It comprehends that you want to create two separate directories. One is “Ethical” 
and the other is “Hacking.” It has already created two directories in that way. So let us 
remove them first and next we will create a directory with more meaningful meaning.
To remove a directory, you must have “root” privilege. It means you are an 
administrator or super user of the system. In Ubuntu, if we want to be a “root” or “super 
user,” we issue the command “sudo” first. In Kali Linux it is different: “su”. But in both 
cases once you write that command, the system will ask for the password through the 
terminal. Let us see how it works.
We first issue the command and in the next step we check with the “ls” command to 
see whether those directories exist anymore.
hagudu@hagudu-H81M-S1:~$ sudo rm -rf Ethical/ Hacking/
[sudo] password for hagudu: 
hagudu@hagudu-H81M-S1:~$ ls
It worked—two directories have been removed successfully. Let us try to understand 
it more. We already know that the “rm” command stands for the word “remove” But what 
about the “-rf” command that follows it? The command “-rf” means “do it recursively 
with force.” Generally this “-rf” command is used to remove directories. You have to 
be very careful about using this command because in Linux, once you have used this 
command, the file or directory is deleted permanently. It is next to impossible to retrieve 
them. It is wise to be very careful about using it.
Hopefully you have also noticed that we have started our command line with “sudo”. 
And you write “sudo”, it asks for the password. In this case, you always give the password 
that you usually type down to log into the system.
Let us again make the directory properly and this time we name it “Ethical-Hacking”, 
so that the system will no longer interpret it as two separate directories.
hagudu@hagudu-H81M-S1:~$ mkdir Ethical-Hacking
hagudu@hagudu-H81M-S1:~$ cd Ethical-Hacking/
hagudu@hagudu-H81M-S1:~/Ethical-Hacking$ ls
hagudu@hagudu-H81M-S1:~/Ethical-Hacking$ touch file1 file2
hagudu@hagudu-H81M-S1:~/Ethical-Hacking$ ls
file1 file2

First we have made directory “Ethical-Hacking”. Then we use “cd” to go inside it 
and, with the help of “ls”, we checked that the directory is empty. Afterwards we issue the 
“touch” command to create two files: “file1” and “file2”. Again we issue the “ls” command 
to check that two files have been created successfully.
In Ethical Hacking, anonymity is a very big deal. In the next chapter we learn it in 
great detail. Before that you need to understand that, in the process of being anonymous, 
it is good to be any user rather than the root user. As the root or super user, you first add 
a user in your virtual Kali Linux. Set a password. Shut down Kali. Reboot and log in as the 
new user. It is a good practice.
Now how could you add a user? Let us open our virtual Kali and as the root user 
we’d use the “adduser” command on the terminal. Suppose our new user has a name like 
“xman”. In that case, the command will be very simple: adduser xman.
Once you have issued this command, Kali asks for the password and other details. 
Give a strong password of at least eight characters with alphanumeric characters. Now 
shut down your machine and log in as “xman”. For other details, it is not mandatory that 
you need to give your real identity. You can fill them with any data.
As the root or super user you can add as many users as you wish. You can delete 
them any time. You can restrict their activities from any angle. As an administrator you 
can add a user who will not be able to log in after six months. You can create groups and 
set a rule so that entry is restricted. Some users can enter into that group. Some can’t.
Primarily you need to add one user, “xman”, and log in the system as the new one. 
A user is not permitted to access or tamper any file of the root or super user. But as super 
user you can always change the file permission. It is a very important concept from every 
angle. On the Internet, the concept of file permission is extremely important.
Any file has three types of permissions related to it. It can be only “read only.” The 
meaning is clear. You can’t write on it or execute it. It can be “write only.” Another state of 
file is “executable mode.” If it is executable, you can perform any action by running it. You 
can write a simple Python program. This program will take inputs from users and give 
outputs. After writing a Python file you can make it executable.
Let us see how it happens. Let us open our Kali Linux terminal and, with the help of 
the “ls” command, we see what we have there presently.
sanjib@kali:~$ cd Documents/
sanjib@kali:~/Documents$ ls
sanjib@kali:~/Documents$ ls -la
total 7048
drwxr-xr-x 2 sanjib sanjib 4096 May 29 10:30 .
drwxr-xr-x 18 sanjib sanjib 4096 Jun 3 09:59 ..
-r-xr-xr-x 1 root root 7208397 May 29 10:30
First we go to the “Documents” folder and issue the “ls” command. That shows only 
one file: “”. Our next command is “ls –la”. It means: we want a 
listing of all files with all details. You can see the difference above. The output is in red. It 
shows two hidden files with the previously seen file. And it also shows the owners of files 
and it also shows the permissions. Let us consider this line minutely..

-r-xr-xr-x 1 root root 7208397 May 29 10:30
It tells us that the owner of this file is “root”. And the starting line is also very 
important. It handles file permissions.
What does this mean? It has three distinct parts. The first part is “r-x”. The second 
and third parts are also the same: “r-x”. The first part is for the owner of the file or current 
user. The second part is for “group.” And the final or third part is for the super user who 
is viewing this file. I have already created another user, “sanjib”, and have logged in as 
“sanjib”. That is why you see this kind of output: sanjib@kali:~/Documents$ ls -la
Now to make this concept more clear we will create a user named “xman”. And we 
will log in as “xman” and see what we have in our Documents folder.
To create a new user, you need to log in as a root or super user. Let us assume we 
have logged in as “root”. The commands and the output are given below.
root@kali:~# adduser xman
Adding user `xman' ...
Adding new group `xman' (1002) ...
Adding new user `xman' (1001) with group `xman' ...
Creating home directory `/home/xman' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for xman
Enter the new value, or press ENTER for the default
 Full Name []: xman anonymous
 Room Number []: 123
 Work Phone []: 321
 Home Phone []: 213
 Other []: anon
Is the information correct? [Y/n] y
Congratulation! You have just successfully created a new user called 'xman'. 
You notice that it had asked for the password and told you to retype the 
UNIX password again. 
Let us log out as “root” and log in as “xman”. Let us also go to the “Documents” folder 
of “xman” and see what we have.
xman@kali:~$ cd Documents/
xman@kali:~/Documents$ ls
xman@kali:~/Documents$ ls -la
total 8
drwxr-xr-x 2 xman xman 4096 Jun 3 10:33 .
drwxr-xr-x 14 xman xman 4096 Jun 3 10:33 ..

Everything goes as expected. Only one thing is missing. This new user does not have 
this line: -r-xr-xr-x 1 root root 7208397 May 29 10:30
Maybe we had moved that executable file from any root folder to the “Documents” 
folder of user “sanjib” before.
Now we already know how to create a file using “nano” text editor. So we can move 
on and have a very small Python file. Presumably you don’t know Python, so I keep it very 
simple just to show how we can change file permissions.
print("TYpe your name.")
inputs = input(">>>>>>")
outputs = inputs
def main():
if __name__ == '__main__':
Inside “nano” editor we write a simple program that will take input and give output. 
Save the file as “” and exit “nano,” and let us issue “ls –la” to see what it shows.
xman@kali:~/Documents$ ls -la
total 12
drwxr-xr-x 2 xman xman 4096 Jun 3 10:50 .
drwxr-xr-x 15 xman xman 4096 Jun 3 10:42 ..
-rw-r--r-- 1 xman xman 86 Jun 3 10:44
As you see, the file says everything. It says that now “Documents” folder has one 
new file, “”, and it has been created at 10:44. The owner is “xman” and it has file 
permissions like this: rw-r--r--
Now you know what this means. It means: the user “xman” can read and write this 
file but he can’t “execute” this file.
xman@kali:~/Documents$ chmod +x 
xman@kali:~/Documents$ ls -la
total 12
drwxr-xr-x 2 xman xman 4096 Jun 3 10:50 .
drwxr-xr-x 15 xman xman 4096 Jun 3 10:42 ..
-rwxr-xr-x 1 xman xman 86 Jun 3 10:44
Look how we have used the “chmod” command to change the file permission to 
executable. Once you have changed the file permission to executable, it changes the color 
to green. And also look at the file permission: rwxr-xr-x

The first part I mark as red so that you can understand the difference between them. 
The first part of the permission says “x” has been added since we used the “xman@kali:~/
Documents$ chmod +x” command.
Let us execute the file and see how it takes the input and gives the output.
xman@kali:~/Documents$ ./ 
Type your name.
When you run the file, it asks to type your name and gently spits back the output.

You have learned a few basic Linux commands in this chapter. Now at least you have an 
idea how a Linux system is working and how you can use your terminal or command line 
to operate your system.
In the learning process of ethical hacking, you find it extremely useful. In the future, 
you need to learn few more Linux commands. Your knowledge of Linux or any other 
operating system must be commendable. As you progress, I hope, your “appetite comes 
with eating.”
We have discussed enough rudiments to stomp our feet on the ground of ethical 
hacking. Now it is time to move forward. We are ready to take the first important step into 
the world of ethical hacking by learning a very useful programming language: Python 3.
We have discussed Python 3 in a manner so that you need no programming 
background. It’s been elaborately discussed so that you might reach the intermediate 
stage and write your own program in Python 3. As you progress in the vast universe of 
ethical hacking, you’ll find the importance of learning Python

What's Your Reaction?