# Linux Intro ## Club Resources * [Practice Problems](https://ctf.tjcsec.club) * [Codespaces Desktop](https://github.com/TJCSec/desktop) * [Shell Commands List](https://hackmd.io/@tjcsc/cmd) ## Definition List Operating System : The software behind a computer's basic functions. Some common examples are Windows or MacOS. Kernel : The "core" of the operating system. Among other functions, a kernel controls the hardware and manages how different applications interact with each other. Shell : A computer program used to interface with other programs. The most common type of shell uses a command-line interface, which requires users to type commands into a text-based computer terminal. ## What is Linux? Linux is an operating system kernel. Different **distributions** of Linux build on top of this kernel to create a full-fledged operating system (OS). Some examples of a Linux distribution are: * **Debian** - A flexible and configurable Linux distribution. Our [Codespaces Desktop](https://github.com/dianalin2/desktop) uses an objectively ugly version of Debian, but you can customize it to suit your preferences. * **Ubuntu** - The most commonly-used Linux distribution, built on top of Debian. While it is less configurable than Debian, it is much more user-friendly to install. Because of this, it is generally recommended as the first distribution that most beginners should start with. * **Kali Linux** - A Linux distribution often used by security experts. It comes pre-installed with various hacking and penetration testing tools. * **Arch Linux** - A very minimal, low-level Linux distribution. It comes pre-installed with only what is needed to run the operating system, and users are intended to only install what is necessary. If you want to install a Linux distro on a personal machine, Ubuntu works perfectly for our purposes. If you want to try something different, feel free to check out Kali. If you're up for a challenge, try to install Arch on your system — for beginners, it often takes an entire day because of how barebones it is. I personally use Arch because I came to enjoy its lightweight nature.[^1] ## The Command-Line Interface (CLI) Normally, to open Google Chrome, you click on the icon on your taskbar. However, another way you can do that is by using a terminal, which is a text-based interface for your computer. With a terminal, you can do almost anything you can do with a graphical user interface (GUI), and there is much more that you can do with a terminal that you can't do with a GUI. While terminals may look different, they all serve the same functionality so you can pick any one to use. The one that comes pre-installed with your system is likely more than usable, so feel free to stick with that one. I personally use [kitty](https://sw.kovidgoyal.net/kitty/) because it was recommended to me by a friend, and I enjoy the user experience. 🙀 When you open a terminal, a program called a shell is opened. The shell takes your commands and gives them to the OS to perform. While there are many different shells available, the most popular is [bash](https://www.gnu.org/software/bash/), which comes pre-installed in most Linux distributions. Open a terminal window, type in `ls`, and hit the Enter button. This will run the command to list all the files and directories in the folder that the command is run in. Generally, the shell is, by default, opened in the current user's home directory[^2], which hosts all the folders and files that the specific user owns. A shortened way to refer to the current user's home folder is `~`. A non-exhaustive list of common commands is written below: | Command | Description | | -------- | -------- | | `ls` | List directory contents | | `pwd` | Print the full path of the current (working) directory | | `cd` | Move to another directory | | `cat` | Print the contents of a file | | `cp` | Copy a file/folder from one location to another | | `mv` | Move a file/folder from location to another | | `mkdir` | Create a directory | | `rm` | Delete files/folders | | `clear` | Clear the terminal display | | `nano` | Open a (relatively) user-friendly command line text editor | | `echo` | Print the proceeding argument | Note that many of these commands have various options associated with them, so you might run something like: ```sh ls -la ~ ``` This command lists all the folders and files located in the user's home directory (`~`) in a long format (`l`), not ignoring hidden files (`a`). To view a full description of a command, you can use `man <command>`. At this point, I advise playing around with a terminal. Try to create a file, edit it, and copy it to your home directory. ### Useful Folders The creators of Linux acknowledged that some folders will likely be accessed more often than others, which resulted in the creation of several useful shortcuts: * `..` means the directory that the current directory is located under. * `~` means the current user's Although this one isn't a shortcut, the "root" folder can be accessed at `/`. This root folder contains every folder and file in the filesystem. The filesystem has a "tree" structure, meaning folders are stored in other folders, and files are also stored in folders. ## CLI for "Security" The CLI can also be used to run many other tools and programs. For example, you may have seen `nc challenge.tjcsec.club 31101` on one of our challenges. This is actually another CLI command, which actualy connects to a server (challenge.tjcsec.club) on a specified port[^3] (31101). A non-exhaustive list of commands that are less common to every day Linux users can be found below: | Command | Description | | -------- | -------- | | `nc` | Establish a connection to a server | | `grep` | Search for a file that has contents that matches a specific pattern of characters | | `file` | Print the file type of a file based on its contents, not the file extension | | `strings` | Print the "printable" bits of text of a file out | | `find` | Print out the files that match a certain specified criteria | The last four commands listed are often used for file forensics, the analysis of files and their contents. This is tricky to lay out in text, so you may need to try them out yourself to get the hang of them. ## I/O Redirection Notice how three of the verbs in the table above are "print." This printing doesn't have to mean "output to the console." Like the printing system on your computer, you can "print" to many different places, including files and other commands. To redirect output to a file (that may or may not already exist), use `>`: ```sh echo "ha" > file.txt ``` If, instead, you want to append to the end of the file (that may or may not exist), use `>>`: ```sh echo "haha" > file.txt ``` Additionally, you can take input from a file using `<`: ```sh python3 -c "print(input())" < file.txt # Prints out the contents of file.txt ``` To send the output of one command to another command, you can pipe it using `|`: ```sh strings file.txt | grep flag ``` ## File Permissions In Linux, files and directories have various permissions that are specified in order to control access to said files and directories. There are three groups of people that could possibly access the file/directory: * The file owner * The file group members * All other users The file owner, as you may guess, owns the file/directory. To change the file owner, use the `chown` command: ```sh chown myuser myfile ``` The file group is a uniquely-defined group of users allowed to access the file a specific way. In Linux, they also have many other purposes in access control, but they are mostly unnecessary to know. To change group ownership, use the `chgrp` command: ```sh chgrp mygroup myfile ``` All other users that are not the owner or in the group are relegated to "other" permissions. There are three permissions for each category of users: read (`r`), write (`w`), and execute (`x`). The first two permissions are pretty self-explanatory; however, the execute permission might be a little new. The execute permission lets you run a file using the following notation in the terminal: ```sh ./myfile ``` If, for example, you have a machine-code program that you want to run on your computer, you must first ensure that your current user has execute permissions. To change any and all permissions, use the `chmod` command: ```sh chmod u+x myfile ``` The above command adds the execute permission to the owner of the file or directory. ## Administrators You may want to change the access permissions of a file because you have "other" permissions. It wouldn't make sense for just anyone to be able to do that, so how would you do that? Like other operating systems, Linux also has system administrators. If you prefix any command with `sudo` (and a space), it will run the command as the superuser, also known as the root or admin user. If you wanted to change access permissions, you could run: ```sh sudo chmod o+r myfile ``` To install new programs on a Debian-based distribution (including Ubuntu), use the `apt` command. You will need to prefix it with [sudo](#Administrators) in order for it to work properly, like so: ```sh # cowsay can be replaced with any package sudo apt install cowsay ``` ## Conclusions We went over a long list of commands in this document, and there are many more that I deem useful, but not useful enough to fully explain. Because of that, we have created a (non-exhaustive) live list of important shell commands that you can refer to at https://hackmd.io/@tjcsc/cmd. As always, if you have any questions, feel free to contact us by: - Asking for help during a club block - Creating a ticket on our [Discord server](https://tjcsec.club/discord) - DMing an officer Happy hacking! [^1]: At the time of writing this, I have eight Chrome tabs, Visual Studio Code, MATLAB, Discord, and Spotify open on my laptop. Only 7.5 GiB of memory is in use, which is a large difference from my Windows machine that used 8 GiB just to run the OS. [^2]: Folders and directories refer to the same thing — a space used to store other files and folders. [^3]: A port is a connection endpoint for a different service. Think of a computer like a house address and a port like a specific person that lives at that address.