Linux Fundamentals - Part 1 2021

 
1320362_1b73_6.jpg
 

The link for this lab is located here: https://tryhackme.com/room/linuxfundamentalspart1


This room covers topics such as an introduction to Linux, running your first commands, searching, interacting with the filesystem and shell operators.


26.jpg

Task 2 - A Bit of Background on Linux

Linux is considerably much more lightweight and there is a good chance you have used Linux in some form or another every day. Linux powers things such as:

  • Websites that you visit

  • Car entertainment/control panels

  • Point of Sale (PoS) systems such as checkout tills and registers in shops

  • Critical infrastructures such as traffic light controllers or industrial sensors

The name of “Linux” is actually an umbrella term for multiple OS’s that are based on UNIX. Thanks to Unix being open-source, variants of Linux comes in all shapes and sizes. For example, Ubuntu & Debian are some of the more commonplace distributions of Linux because it is so extensible. 

As an example, you can run Ubuntu as a server or as a full desktop. As a side note, Ubuntu Server can actually even run on systems with only 512MB of RAM.

Questions

Q1: What year was the first release of a Linux operating system? A: 1991

1.jpg

Task 4 - Running Your First Few Commands

A large selling point of using OS’s such as Ubuntu is how lightweight they can be. This does have its disadvantages - often there is no GUI unless one has been installed. A large part of interacting with these systems is done through the terminal.

The terminal is purely text-based and is intimidating at first. In the terminal, we need to be able to do basic functions like navigate to files, output their contents and make files. The commands to do so are self-explanatory. Two of the first commands are:

  • echo - outputs any text we provide

  • whoami - finds out what user we are currently logged in as

echo#.png
whoami.png

Questions

Q1: If we wanted to output the text “TryHackMe”, what would our command be? A: echo TryHackMe

echo command

Q2: What is the username of who you are logged in as on your deployed Linux machine? A: tryhackme

whoami command


10.jpg

Task 5 - Interacting with the File System

Being able to navigate the machine that you are logged into without relying on a desktop environment is pretty important. Some of the following commands help with this:

  • ls - listing

  • cd - change directory

  • cat - concatenates

  • pwd - print working directory

Before we can do anything such as finding out the contents of any files or folders, we need to know what exists in the first place. This can be done using the “ls” command.

ls.png

In the screenshot, there are 4 directories. You can list the contents of a directory without having to navigate to it by using ls and the name of the directory i.e. “ls Pictures

We can use the cd command to change to that directory. If you wanted to open the Pictures directory, use the command “cd Pictures”.

If you want to see the contents of text files or any other files, use a command called “cat”. Cat is short for “concatenating” and is a great way to output the contents of files. In the screenshot, you can see the combined use of “ls” to list files and the cat command to view the contents of a text file.

cat.png

As you profess through the Linux machine, the name of the directory you are currently in will be listed at your terminal. It is easy to lose track of where we are on the filesystem exactly - this is where the “pwd” command comes in handy.

To find out where we are, we can simply type “pwd” which prints the full path to the current directory.

Questions

Q1: On the Linux machine, how many folders are there? A: 4

folders

Q2: Which directory contains a file? A: folder4

folder4

Q3: What is the contents of this file? A: Hello World

hello world

Q4: Use the cd command to navigate to this file and find out the new current working directory. What is the path? A: /home/tryhackme/folder4

working directory

9.jpg

Task 6 - Searching for Files

One of the redeeming features of Linux is how efficient you can be with it. However, you can only be as efficient as you are familiar with it. 

One fantastic way to show off how efficient you can be with systems is using a set of commands to quickly search for files across the entire system that our user has access to. This is where Linux starts to become a bit more intimidating to approach.

The find command is fantastic in the sense that it can be used both very simply or rather complex depending what you need to do. Directories can contain even more directories within themselves. It becomes a headache when we are having to go through every single one to look for files - the find command is useful for this.

If we remember the filename, we can simply use “find -name passwords.txt” where the command will look through EVERY folder in the current directory and below for that specific file.

find.png

However, what if we don’t know the filename? Or if we want to search for every file that has an extension such as “.txt”. 

We can simply use what is known as a wildcard (*) to search for anything that has .txt at the end. In our case, we want to find every .txt file that is in the current directory. The command “find -name *.txt” looks for every text file.

text.png

Another great utility that is a great one to learn about is the use of grep. The grep command allows us to search the contents of files for specific values that we are looking for.

Using a command like “cat” is not recommended if the file has hundreds of lines of content. Say we wanted to search a log file to see things that a certain user/IP address visited - looking through all the lines would take ages.

Instead, we can use grep to search the entire contents of the file for any entries that match the value we are searching for. In the screenshot, we grep for a certain IP address from an access log file.

log.png

Questions

Q1: Use grep on access.log to find the flag that has a prefix of THM. What is the flag? A: THM{ACCESS}

Access flag

11.jpg

Task 7 - An Introduction to Shell Operators

Linux operators are a fantastic way to power up your knowledge of working with Linux. There are a few important operators that are worth noting:

  • & - allows you to run commands in the background

  • && - allows you to combine multiple commands together in one line of your terminal

  • > - can take output from a command and direct it elsewhere

  • >> - same function as above but appends the output rather than replacing

The & operator allows us to execute commands in the background. For example, if we want to copy a large file, we can background it because it will take a long time. 

The && operator is used to make a list of commands to run - eg. “command1 && command2”. It is worth noting that “command2” will ONLY run if “command1” was successful.

The > operator is known as an output redirection. This means that we can take the output from a command we run and send it to somewhere else. Say we wanted to create a file named “welcome” with the message “hey” - you can run “echo hey > welcome” where we want the file created with the contents “hey”:

red.png

The >> operator is also an output redirector like in the previous operator but instead of overwriting any content within a file, it instead appends the output at the end of an existing file.

The command “echo hello >> welcome” will append hello to the end of the welcome file.

app.png

Questions

Q1: If we wanted to run a command in the background, what operator would we want to use? A: &

Q2: If I wanted to replace the contents of a file named passwords with the word "password123", what would my command be? A: echo password1234 > passwords

> operator

Q3: Now if I wanted to add "tryhackme" to this file named "passwords" but also keep "password123", what would my command be? A: echo tryhackme >> passwords

appending

Previous
Previous

Linux Fundamentals - Part 2 2021