Linux Fundamentals III

Automation, package management & service/application logging

·

7 min read

Linux Fundamentals III

As I continue my cybersecurity journey I wanted to publish my notes in order to provide helpful write-ups for other learners on the same journey, with the added benefit of explaining the concepts in a beginner friendly way. I intend to break things down bit by bit and provide further clarification for learners.

The third TryHackMe module is Linux Fundamentals III. This section helps us learn how to use terminal text editors, operate general utilities such as downloading and serving contents using a python webserver and provides a look into processes. It also highlights maintaining & automating your system by the use of crontabs, package management, and review logs.

TERMINAL TEXT EDITORS:

Throughout the series so far, we have only stored text in files using a combination of the echo command and the pipe operators (> and >>). This isn't an efficient way to handle data when you're working with files with multiple lines and the sorts!

Introducing terminal text editors

There are a few options that you can use, all with a variety of friendliness and utility. This task is going to introduce you to nano but also show you an alternative named VIM (which TryHackMe has a room dedicated to!)

Nano

It is easy to get started with Nano! To create or edit a file using nano, we simply use nano filename -- replacing "filename" with the name of the file you wish to edit.

Introducing Nano

tryhackme@linux3:/tmp# nano myfile

GNU nano 4.8 myfile

G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos M-U Undo M-A Mark Text

^X Exit ^R Read File ^\ Replace ^U Paste Text ^T To Spell ^_ Go To Line M-E Redo M-6 Copy Text

Once we press enter to execute the command, nanowill launch! Where we can just begin to start entering or modifying our text. You can navigate each line using the "up" and "down" arrow keys or start a new line using the "Enter" key on your keyboard.

Using Nano to write text

tryhackme@linux3:/tmp# nano myfile

GNU nano 4.8 myfile Modified

Hello TryHackMe

I can write things into "myfile"

Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos M-U Undo M-A Mark Text

^X Exit ^R Read File ^\ Replace ^U Paste Text ^T To Spell ^_ Go To Line M-E Redo M-6 Copy Text

Nano has a few features that are easy to remember & covers the most general things you would want out of a text editor, including:

  • Searching for text

  • Copying and Pasting

  • Jumping to a line number

  • Finding out what line number you are on

You can use these features of nano by pressing the "Ctrl" key (which is represented as an ^ on Linux) and a corresponding letter. For example, to exit, we would want to press "Ctrl" and "X" to exit Nano.

VIM

VIM is a much more advanced text editor. Whilst you're not expected to know all advanced features, it's helpful to mention it for powering up your Linuxskills.

Some of VIM's benefits, albeit taking a much longer time to become familiar with, includes:

  • Customisable - you can modify the keyboard shortcuts to be of your choosing

  • Syntax Highlighting - this is useful if you are writing or maintaining code, making it a popular choice for software developers

  • VIM works on all terminals where nano may not be installed

  • There are a lot of resources such as cheatsheets, tutorials, and the sorts available to you use.

TryHackMe has a room showcasing VIM if you wish to learn more about this editor!

GENERAL/ USEFUL UTILITIES:

Downloading Files

A pretty fundamental feature of computing is the ability to transfer files. For example, you may want to download a program, a script, or even a picture. Thankfully for us, there are multiple ways in which we can retrieve these files.

We're going to cover the use of wget. This command allows us to download files from the web via HTTP-- as if you were accessing the file in your browser. We simply need to provide the address of the resource that we wish to download. For example, if I wanted to download a file named "myfile.txt" onto my machine, assuming I knew the web address it -- it would look something like this:

wget https://assets.tryhackme.com/additional/linux-fundamentals/part3/myfile.txt

Transferring Files From Your Host - SCP (SSH)

Secure copy, or SCP, is just that -- a means of securely copying files. Unlike the regular cp command, this command allows you to transfer files between two computers using the SSHprotocol to provide both authentication and encryption.

Working on a model of SOURCE and DESTINATION, SCP allows you to:

  • Copy files & directories from your current system to a remote system

  • Copy files & directories from a remote system to your current system

*Provided that we know usernames and passwords for a user on your current system and a user on the remote system. For example, let's copy an example file from our machine to a remote machine, which I have neatly laid out in the table below:

VariableValue
The IP address of the remote system192.168.1.30
User on the remote systemubuntu
Name of the file on the local systemimportant.txt
Name that we wish to store the file as on the remote systemtransferred.txt

With this information, let's craft our scpcommand (remembering that the format of SCP is just SOURCE and DESTINATION)

scp important.txt ubuntu@192.168.1.30:/home/ubuntu/transferred.txt

And now let's reverse this and layout the syntax for using scpto copy a file from a remote computer that we're not logged into

VariableValue
IP address of the remote system192.168.1.30
User on the remote systemubuntu
Name of the file on the remote systemdocuments.txt
Name that we wish to store the file as on our systemnotes.txt

The command will now look like the following:

scp ubuntu@192.168.1.30:/home/ubuntu/documents.txt notes.txt

Serving Files From Your Host - WEB

Ubuntu machines come pre-packaged with python3. Python helpfully provides a lightweight and easy-to-use module called "HTTPServer". This module turns your computer into a quick and easy web server that you can use to serve your own files, where they can then be downloaded by another computing using commands such as curland wget.

Python3's "HTTPServer" will serve the files in the directory that you run the command, but this can be changed by providing options that can be found in the manual pages. Simply, all we need to do is run python3 -m http.serverto start the module! In the screenshot below, we are serving from a directory called "webserver", which has a single named "file".

Using Python to start a web server

tryhackme@linux3:/tmp# python3 -m http.server

Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

Now, let's use wget to download the file using the computer's IP address and the name of the file. One flaw with this module is that you have no way of indexing, so you must know the exact name and location of the file that you wish to use.

This is why I prefer to use Updog. What's Updog? A more advanced yet lightweight webserver. But for now, let's stick to using Python's "HTTP Server".

Downloading a file from our webserver using wget

tryhackme@linux3:/tmp# wget http://127.0.0.1:8000/file

2021-05-04 14:26:16 http://127.0.0.1:8000/file

Connecting to http://127.0.0.1:8000... connected.

HTTP request sent, awaiting response... 200 OK

Length: 51095 (50K) [text]

Saving to: ‘file’

file 100%[=================================================>] 49.90K --.-KB/s in 0.04s

2021-05-04 14:26:16 (1.31 MB/s) - ‘file’ saved [51095/51095]

In the screenshot above, we can see that wget has successfully downloaded the file named "file" to our machine. This request is logged by SimpleHTTPServer much as any web server would, which I have captured in the screenshot below.

Using Python to start a web server

tryhackme@linux3:/tmp# python3 -m http.server

Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

127.0.0.1 - - [04/May/2021/14:26:09] "GET /file HTTP/1.1" 200 -

Continue your learning in some other TryHackMe rooms that are dedicated to Linux tools or utilities:

As always, if you see anything I've missed or have suggestions to add, feel free to drop me a line or let me know in the comments. Happy learning!

-Mary

Sources: TryHackMe Pre-security Pathway: Linux Fundamentals 1-3