Command-Line Walk-Through: Unveiling the Power of Linux and Kali (Part 1 of 2)

Dr. Jaber Kakar
7 min readSep 6, 2023

--

In the realm of cybersecurity, a strong foundation serves as our compass through the intricate digital landscape. Navigating through the command line is a vital skill that allows us to interact with operating systems on a deeper level. If you’re new to this terrain, don’t worry — we’re here to guide you through the essentials.

Linux Command-Line Walk-Through

Part 1: The Basics

This article marks the beginning of a two-part series dedicated to unravel the power of the command line in Linux. In this first part, we’ll lay down the fundamentals, demystify the terminology, and demonstrate the practicality of common commands. Furthermore, we’ll take a step beyond and explore the bridge to Kali Linux, a specialized operating system embraced by ethical hackers.

Join us in this expedition as we delve into the fundamental commands that will empower you to confidently traverse the digital landscape. With each command you learn, you’ll be one step closer to harnessing the full potential of the command line, not just for cybersecurity but beyond.

Why Kali Linux Matters?

Kali Linux isn’t just any operating system; it’s a Debian-based distribution purpose-built for penetration testing. Imagine having a toolbox filled with pre-installed and preconfigured penetration testing tools — that’s precisely what Kali Linux offers. Ethical hackers rely on Kali Linux to identify vulnerabilities, simulate cyberattacks, and secure systems.

Setting up a penetration testing environment from scratch can be a tedious task. Kali Linux eliminates that complexity by providing a ready-to-use environment that saves time and effort. Whether you’re a cybersecurity enthusiast or a professional in the field, Kali Linux offers a great platform for hands-on experience in a controlled environment.

Why Embrace the Terminal?

The terminal, often referred to as the command line interface, is the heartbeat of Linux systems. It’s the space where simple text commands can wield tremendous power. Using the terminal offers several advantages:

  1. Efficiency: The terminal allows you to perform tasks swiftly by entering commands directly, bypassing graphical interfaces and menu navigation.
  2. Precision: Commands let you fine-tune your actions, a critical aspect in cybersecurity, where every action matters.
  3. Customization: Commands can be combined and automated to perform complex tasks, allowing you to tailor your environment to your specific requirements.

Linux Command Line

Your Linux command line may initially look as follows:

root@kali:~#

When you enter the command line, you’re interacting with Bash, a command processor that interprets your text-based commands. This is where the true control lies. The prompt, root@kali#, signifies the superuser or root user in Linux, the entity with supreme authority over your system, in our case, Kali.

To perform operations in Linux, you’ll use commands along with relevant options. Here is an example:

root@kali:~# ls
clock.sh Documents Music Pictures Templates
Desktop Downloads nohup.out Public Videos

In this example, the command ls(without additional options) is used to list the contents of the root’s home directory. Contents in this directory are folders Documents, Desktop, etc.

Navigating the Linux File System

At the core of Linux is a hierarchical file system with every branch originating from the root (/). Learning to navigate this structure is your initial step toward mastery. Every file, directory, and resource has its place, and understanding this structure is crucial for efficient command-line navigation.

Moving Around: Use the cd command to change your directory. You can use both absolute and relative paths. Here is a collection of cd commands.

root@kali:~# cd Documents
root@kali:~/Documents# cd ..
root@kali:~# cd /etc
root@kali:/etc#
  • In the first line, we seek to navigate to a folder named Documents from the root’s home directory.
  • To move up one level, we generally make use of cd ... For example, if you were in the Documents directory and wanted to go back to the root’s home directory, you'd type cd ...
  • To jump to a specific location, like the /etc directory, make use of cd /etc with /etc being an absolute path.

Exploring Directories: The ls command lists the contents of a directory. Add options like -l for detailed information or -a to show hidden files as well. Here is an example:

root@kali:~# ls -a
. .cache Documents .local .profile Videos
.. clock.sh Downloads Music Public .viminfo
.bash_history .config .gnupg .nohup.out .ssh .wget-hsts
.bashrc Desktop .ICEauthority Pictures Templates

In the example above, we can see multiple files being located in the root directory. The entries . and .. indicate the current directory and the parent directory. Entries that are preceded by a period (.) indicate hidden files/directories.

Locating Your Spot: The pwd command displays your current working directory. It's like a GPS for your command-line journey.

root@kali:~# pwd
/root

Mastering User Privileges and Essential Commands

Privilege separation is one important security paradigm implemented in Linux. Regular users operate with limited privileges to reduce their impact on their environment. In contrast, a special user called root possesses super-user privileges. This is an administrative account without the restrictions that are imposed on normal users. By default, Kali Linux comes with only the privileged root account.

  • Adding a User: To add a new user, such as Ben, use the adduser command as shown below. This command not only adds a new user but also creates a “Ben” group, a home directory, and prompts for user information, including a password and full name.
root@kali:~# adduser ben
Adding user `ben' (1000) ...
Adding new group `ben' (1000) ...
Adding new user `ben' (1000) with group `ben' ...
Creating home directory `/home/ben' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for tim
Enter the new value, or press ENTER for the default
Full Name []: Ben Thomas
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] Y
  • Using the sudo Command: When a regular user needs to perform actions requiring root privileges, the sudo command comes to the rescue. It allows you to execute specific commands with super-user privileges, provided you enter your password.
  • Adding a User to the Sudoers File: To grant a new user like Ben the ability to run privileged commands, add them to the sudo group. This simple command (see below) empowers the user Ben to execute privileged commands when necessary.
root@kali:~# adduser ben sudo
Adding user `ben' to group `sudo' ...
Adding user ben to group sudo
Done.
  • Switching Users: The su command enables you to switch to another user account. For example, to switch from the root user to Ben, you can enter the command shown below. To revert to the root user, use the su command without specifying a username.
root@kali:~# su ben
  • Applying Commands as Non-Root User: In certain cases, running commands, such as adduser, may fail due to insufficient privileges. Applying the sudo command, as shown below, with Ben being part of the sudo group, can resolve this issue, allowing you to add a new user like Tim:
root@kali:~# su ben
ben@kali:/root$ adduser tim
adduser: Only root may add a user or group to the system.
ben@kali:/root$ sudo adduser tim

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

[sudo] password for ben:
Adding new group `tim' (1002) ...
Adding new user `tim' (1002) with group `tim' ...
Creating home directory `/home/tim' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for tim
Enter the new value, or press ENTER for the default
Full Name []: Tim Thomas
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] Y

Managing Files and Directories

Effectively handling file and directory manipulation is crucial for command-line proficiency. Key aspects include:

  • Creating New Files and Directories: To create a new file or directory, use the touch and mkdir commands, respectively. Here’s how to create a new directory, move to it, and add a new file:
root@kali:~# mkdir newdirectory
root@kali:~# cd newdirectory
root@kali:~/newdirectory# touch newfile
  • Copying, Moving, and Removing Files: You can copy files with cp, move them with mv, and remove them with rm. The syntax for these commands are, respectively for (i) cp and mv: cp/mv source destination and (ii) rm: rm file. Exercise caution, especially with the rm command and the -r (recursive) option, as it can permanently delete files and directories without sending them to the trash or recycling bin. Double-check your commands before executing them to avoid unintentional data loss. Here is an example of copying a file to the same directory:
root@kali:~/newdirectory# cp /root/newdirectory/newfile newfile2
  • Using echo to Adjust Files: As the name suggests, the echo command itself echoes what the user enters into the terminal. When combined with > or >> symbols, you can overwrite or append text in a file. Specifically, to overwrite the entire content of a file with new text, you can use echo followed by > to redirect the output to the file. Alternatively, to append text to a file use echo followed by >> instead. The following snippet shows how to use these commands in greater detail (Note: The command cat with syntax cat file allows the user to see the content of the file.):
root@kali:~/newdirectory# echo hello world
hello world
root@kali:~/newdirectory# echo hello world > newfile
root@kali:~/newdirectory# cat newfile
hello world
root@kali:~/newdirectory# echo hello world, a second time > newfile
hello world, a second time
root@kali:~/newdirectory# echo hello world, a second time >> newfile
hello world, a second time
hello world, a third time
  • Using Text Editors (e.g., nano) to Adjust Files: Text editors like nano provide advanced text manipulation capabilities compared to echo. They open a text file in an interactive environment, allowing you to view, edit, and save changes. To open newfile in thenano text editor, perform the following:
root@kali:~/newdirectory# nano newfile

Conclusion

Thank you for joining us for Part 1 of our two-part series on mastering the command line. In Part 2, we’ll dive even deeper into the command line, exploring aspects of data manipulation, network commands, and more. Stay tuned for an even more comprehensive journey into the world of the command line in our next issue.

Thanks for reading! If you want to learn more about Ethical Hacking, please subscribe to this blog. We will constantly be posting articles to help you start your cyber security journey as an ethical hacker!

--

--

Dr. Jaber Kakar

🔐 Cybersecurity Enthusiast | Ethical Hacker in the Making | Exploring the Digital Battlefield | Sharing Insights to Safeguard the Online Realm 🔐