Linux Terminal Basics.

Whether you like it or not in IT, you will stumble across Linux and you will need to use it. How are you going to troubleshoot, pentest, develop, and deploy on Linux while being scared of the terminal? This article covers the absolute basics of operating and moving around the terminal, to conquer your fear of it.


Shell Prompt

A shell prompt is the text that appears on your screen when you open a terminal or command-line interface (CLI). It's typically displayed at the beginning of the line, followed by a symbol indicating where you can type commands.

Example:

simsin@mint-vm:~$

We can see what information it displays with just a few quick commands.

First part is our username, we can prove it with whoami command:

simsin@mint-vm:~$ whoami
simsin

Second part is our hostname:

simsin@mint-vm:~$ hostname
mint-vm

And the third part, the '~' character symbolizes our home directory, in case of other directories, eather the full path or the path relative to our home directory will be displayed.
We can print working directory like this:

simsin@mint-vm:~$ pwd
/home/simsin

Clearing the terminal

Terminal can easly get cluttered by the commands we used, we can eather use the clear command to clear it or click CTRL + L on the keyboard.


Moving around

First before we move around we need to know what folders/files are in the directory we are in, we can do that using the ls command:

simsin@mint-vm:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos

We can see different directories, but thats not all we can see even more with the -a flag

simsin@mint-vm:~$ ls -a
. .asdf .cert .gitconfig .mozilla .ssh .themes .zcompdump Downloads Videos
.. .bash_logout .config .gnupg .nuget .pki .steam .var .zprofile .icons
...SNIP...

We can se a lot of files and directories starting with '.' - on linux this means that a given file/dir is hidden.

There are also two directories that are just dots, the: '.' and '..', single dot represents the current directory and two represent the parent dir.

We can go to that directory using the cd command:

simsin@mint-vm:~$ cd ..
simsin@mint-vm:/home$ ls
simsin
simsin@mint-vm:/home$ cd ~
simsin@mint-vm:~$ pwd
/home/simsin

Working with files and directories

To create a directory we can use mkdir command

simsin@mint-vm:~$ mkdir test
simsin@mint-vm:~$ cd test
simsin@mint-vm:~/test$ pwd
/home/simsin/test

To create a file we can use touch:

simsin@mint-vm:~/test$ touch test.txt
simsin@mint-vm:~/test$ ls
test.txt

To write something in the file we can redirect output from echo to it, redirecting output in linux is a deeper topic but thats outside of the scope of this article.
To view the files' contents we will use cat:

simsin@mint-vm:~/test$ echo "something" > test.txt
simsin@mint-vm:~/test$ cat test.txt
something

Nano - the terminal text editor

Nano is a text editor for Unix-like computing systems or operating environments using a command line interface. It lets us easily edit files inside of the terminal, unlike vim it doesnt require knowledge of multiple shortcuts, and is much easier to exit.
We can edit our file with nano typing:

simsin@mint-vm:~/test$ nano test.txt

After that you will se something like that:

To save the file we can eather use CTRL + S or CTRL + O after that we will use CTRL + X to exit out of nano. As you probably noticed the '^' sign ath the bottom in shortcuts represents our CTRL key.

Again we can cat the file to see its contents:

simsin@mint-vm:~/test$ cat test.txt
something
text written in nano :3

To remove the file we can use the rm command:

simsin@mint-vm:~/test$ rm test.txt
simsin@mint-vm:~/test$ ls
simsin@mint-vm:~/test$

We can see that the ls command didnt give us any output sience we deleted the file and the directory is empty.

We can follow the same proces to delete a directory using the rmdir command:

simsin@mint-vm:~/test$ cd ..
simsin@mint-vm:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos test
simsin@mint-vm:~$ rmdir test
simsin@mint-vm:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos

Installing and using applications

For installing, updating our apps/libraries we use Advanced package tool, or APT. It's a free-software user interface that works with core libraries to handle the installation and removal of software on Debian and Debian-based Linux distributions. There are many different package managers, but as a beginer, i expect you to be on a debian based linux distribution.

To install an app we will use apt install [package] that we want:

simsin@mint-vm:~$ apt install nmap
Error: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
Error: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?

Elevating privileges

As you see in the example above when we try to run apt we encounter a "Permission denied" error, the reason is that by default our user does have neceseary privileges to do it. We can elevate them by using sudo - short for "superuser do"

simsin@mint-vm:~$ sudo apt install nmap
[sudo] password for simsin:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Suggested packages:
...SNIP...
Unpacking nmap (7.94+git20230807.3be01efb1+dfsg-3build2) ...
Setting up nmap (7.94+git20230807.3be01efb1+dfsg-3build2) ...
Processing triggers for man-db (2.12.0-4build2) ...
simsin@mint-vm:~$

Getting help

There are two different ways of getting help about the commands in linux, help messages and man pages.

To use see the help message we can use a -h or --help flag

simsin@mint-vm:~$ nmap -h
Nmap 7.94SVN ( https://nmap.org )
Usage: nmap [Scan Type(s)] [Options] {target specification}
TARGET SPECIFICATION:
Can pass hostnames, IP addresses, networks, etc.
...SNIP...

We can also pull up a man page which usually contains much more info about the tool, but some tools do not have their man pages.

We can do that by using man [tool]

Last tip

If your command hangs or something you executed is behaving in an unwanted way, you can use CTRL + C to stop it.