Day 010 #FromZeroToHacker – Linux Fundamentals: Part 2

We will learn how to log in to a Linux machine using SSH, how to advance your commands and file system interaction, and more!

This is the second of a 3-part from our #FromZeroToHacker Linux fundamentals, so let’s crank it up!

Table of contents
Introduction
What I have learnt today?
Stats
Resources

Introduction to Linux Fundamentals

While this lesson may be a bit dense, we will cover a wide range of Linux topics:

  • How to connect to a Linux machine remotely via SSH
  • Learning about new Linux terminal commands, along with flags and switches
  • Know where to find more information about any Linux terminal command
  • Introduction to file permissions and how to switch to other users
  • Important root directories on Linux installs and what we can find on each one

What I have learnt today?

Accessing Your Linux Machine Using SSH (Deploy)

When I was using TryHackMe, sometimes I had to solve some questions by connecting to a Linux machine in-browser without any trouble, as it was pre-configurated. Now, it is time to connect to TryHackMe Linux virtual machine from our computer.

The protocol we use, SSH or Secure SHell, is the same that the in-browser function uses, and is a common way to connect to a remote device, interacting with its command line.

What is SSH & how does it work?

Secure shell or SSH, is a protocol between devices using encryption. By using cryptography, any input sent (human-readable) is encrypted for traveling over a network, and once it arrives at the device, the data is unencrypted:

SSH connection between two devies

The gist of it is that SSH allows us to remotely connect and execute commands on another device remotely, and any data sent is encrypted.

Using SSH to log in to your Linux machine

The syntax to use SSH is pretty simple: We just need to provide the IP address of the remote machine and the correct credentials (username, and an optional password).

For example, if our user is admin and the IP is 10.10.49.127, we can try to connect it with the command ssh admin@10.10.49.127. After that, and if everything went ok, the machine will ask for a password. Giving the proper password will finally grant us access to the machine:

SSH connection in a Linux terminal

Introduction to Flags and Switches

In yesterday’s Linux Fundamental Part 1 lesson, we started using commands on the terminal, but we can provide arguments with a hyphen and a keyword called flag or switch.

If we don’t provide any, as we did, Linux uses the command as if we were providing the default valor. For example, ls listed the contents of the current directory but, as we didn’t tell Linux to show us them, hidden files were….well, hidden. With flags, we can fix this.

For example, while using ls command, we can give it the -a (short for -all) argument, listing more files and directories, displaying now the hidden ones (the ones with a dot as the first character in their names).

ls linux terminal command

Almost every command has loads of flags like this, and while you can learn them by heart, I recommend you to be lazy as me and just use the --help argument. This option lists all the possible options that the command accepts, with a description, sometimes an example about how to use it, and more information about the command:

help linux terminal command

While this provides us with practical info about our command, we can learn even more. The man (from manual) page.

The Man(ual) Page

The manual pages are a great source of information, that is accessible from our machine and online.

To access this documentation on our device, we can use the man command, providing the command we want to access the documentation for. For example, ls it would be man ls.

Filesystem Interaction Continued

We used basic commands to interact with the filesystem of a Linux machine (ls, find, cd…). Let’s learn more commandos and start manipulating files and folders with them:

  • touch: Creates a file
  • mkdir: Creates a directory
  • cp: Copies a file or folder
  • mv: Moves a file or folder
  • rm: Removes a file or folder
  • file: Determines the type of a file

Creating Files and Folders (touch, mkdir)

Creating files and folders is an easy task on Linux. First, we specify the command (touch or mkdir), then the name of the new file or folder. Creating a new file, creates an empty file with no type, while creating a folder creates also an empty folder. Let’s give it a try:

touch and mkdir linux terminal commands

Removing Files and Folders (rm)

We can delete any created file or folder with just rm command. When removing a directory, we need to provide the -R flag:

rm linux terminal command

Copying and Moving Files and Folders (cp, mv)

Copying and moving files is easy on Linux. Using cp with two arguments (existing filename and new filename), we can create a duplicate of a file. Of course, also copies the entire contents of the existing file to the new one.

cp and mv linux terminal commands

Moving a file also takes the same two arguments. However, instead of creating a copy, it moves the file from one folder to another, or renames the file if a new route isn’t provided:

cp and mv linux terminal commands

Determining File Type

Linux doesn’t force you to give an extension to a file (something that determines its type) as isn’t necessary. But we have a tool to learn the type of the file: file.

It just takes one argument, the name of the file:

file linux terminal command

Permissions 101

Not every user has access to all the files and folders, and we should keep it this way. We shouldn’t grant admin permissions to a normal user, giving them the capacity to delete all the files, right?

We can use the -l flag on the ls command, we can see the permissions of each file and folder:

Checking permissions on a Linux terminal

I know, I know. It may be a bit intimidating. The first column, the one with the hyphens and apparently random letters is the permissions. A hyphen at the start means that it is a file, while a d means that it is a directory. Then, there are 3 sets of 3 characters. The first set applies to the owner of the file, the second one to the user group that owns the file, and the third to “others”.

Then, we have R, W, or X: Read, Write or eXecute. If there is a letter, it means that that set has permission for that action, whereas if there is a hyphen, means that that set of user or users, has not.

For example, in the first file, important, we can see that user2 has permission to read and write it (rw-), the user group that owns the file has the same permission, while others (the third set) can only read the file (r–, no W nor X).

Yes, the creator of the file can read it and write it, but not execute it despite creating the file. This is good, as we as admins can stop people of creating and using attacks within our systems, thanks to the granular approach to permissions that Linux has.

Switching Between Users

Well, we want to manipulate that file, so let’s switch users to user2. To do so, we have the command su (Substitutive User) following with the username: su user2.

To go back to your user, just use su YOUR_USERNAME or just exit.

Common directories

Every Linux distro is different, but they have a few common directories that we should learn to understand how a device works.

/etc

This root directory is one of the most important root directories on any system, as it is a commonplace location to store system files used by your OS.

Important files such as sudoers.d, a list of users and groups that have permission to run sudo. Something an attacker may be interested in.

As interested as he may be in the passwd and shadow files, where the passwords of all users are encrypted.

/var

Var, short for variable data, is one of the main root folders where is stored data frequently accessed and written by services or apps running the system. Log files from services are written here (check /var/log for that).

/root

Unlike the /home directory, /root is the home directory of the root system user. And therefore, hardly accessible.

/tmp

This is a unique root directory. Short for temporary, the /tmp directory is volatile and used to store data that is only needed to be accessed once or twice and, when the computer is restarted, the contents of this folder are cleared out.

There is one caveat: ANY user can write to this folder by default. So, if we have access to a machine, it is a good place to store things like our scripts.

Stats

From 229.009th to 220.076th. Let’s go!

Here is also the Skill Matrix:

Skill matrix

Resources

Path: Pre Security

Linux Fundamentals

TryHackMe: Linux fundamentals part 2

Other resources

Linux man pages