TryHackMe - Learning Linux

Posted June 11, 2020 · 5 min read
tryhackme
linux
fundamentals

SSH

format - <user>@<ip-address>
ex: root@10.10.249.27

Essential Commands

su - Switch User

su with a username will switch users, empty means root.

su user1
# password prompt would come if neccesary

Output Redirection

  • > — outputs to a file. Ex: echo hello > file.txt would save the result of echo hello in a file called file.txt.

    Note: if there currently is a file named file.txt it would be completely erased

  • >> — Like >, but instead of re-writing the file it appends to it.

Command Chaining

  • && — executes two commands. Ex: sleep 2 && echo hello — first it would sleep for 2 seconds and after that echo hello would be executed.

  • & — makes it so that a command can be executed while another is going on. By default sleep 10 would wait for 10 seconds, and during this time no other command can be executed, however by using sleep 10 & it would be possible to execute code during the time the first command is being executed.

  • ; — just like && but it would work even if the first command is invalid.

  • | — uses the first command as an input for the second command. Assuming there is a file called text.txt with the content “Hello World”, executing the command cat text.txt | grep Hello would search for the word Hello, from the input given, in this case the concat of text.txt, and as an output would highlight that the word “Hello” is in the file text.txt.

$ - Environment Variables

This is used to set enviroment variables. Ex: echo $USER will echo the current user.

To set your own enviroment variable it is just as easy as:

export {varname}={whatchu trying to set}

File Permissions

chmod

Allows you to set permissions for a file and who can control it.

chmod <file><permission>
ValuePermission
1Execute
2Write
3Execute + Write
4Read
5Read + Execute
6Read + Write
7Read + Write + Execute

Understanding Permission Strings

-rwxrwxrwx
  • Character 0 tells file type: - for files, d for directories
  • After that, each 3 letter group shows permissions: 1st for user, 2nd for group, 3rd for all others
CharacterMeaning
rRead access
wWrite access
xExecute access
-Access denied

File Operations

CommandDescription
rm <filename>Removes file
mv <filename> <destination>Moves file
cp <source> <destination>Copies file (like mv but duplicates)
mkdir <path>Makes a directory (supports relative and absolute paths)
cd <path>Changes directory (supports relative and absolute paths)

Searching

find

Finds files in current directory (recursively). The find command is very large and overwhelming and it would be best to read man find.

  • -PERM to find files with specific permissions. Ex: find -PERM 777
  • -user to find files of a user. Ex: find / -user nafaal

grep

grep <string> <file>
# file is optional if using pipe "|"
# multiple files can be searched: grep <string> <file> <file2>

Example — finding the path of a file named helloworld:

find /* | grep helloworld

Here we are passig all the directories and using grep to find the path. Notice that we used pipe to input data from the first operation to the second one.

User Management

sudo

Allows user to do task as root (default) if user is in list of sudoers. man sudo for more details.

Adding Users/Groups

sudo adduser <name of user>
sudo addgroup <name of group>

Note: adduser and addgroup can only be run as root.

Editing users in groups:

usermod -a -G <groups seperated by commas> <user>

nano

Nano is a text editor like vim. Ex: nano <file you want to edit>

Important Paths

PathDescription
/etc/passwdStores user information - Often used to see all the users on a system
/etc/shadowHas all the passwords of these users
/tmpEvery file inside it gets deleted upon shutdown - used for temporary files
/etc/sudoersUsed to control the sudo permissions of every user on the system
/homeThe directory where all your downloads, documents etc are. Equivalent on Windows: C:\Users\<user>
/rootThe root user’s home directory. Equivalent on Windows: C:\Users\Administrator
/usrWhere all your software is installed
/bin and /sbinUsed for system critical files - DO NOT DELETE
/varThe Linux miscellaneous directory, a myriad of processes store data in /var

$PATH

Stores all the binaries you’re able to run - same as $PATH on Windows. It is worth noting that the paths in $PATH are separated by colons. Every executable file that is in any of those paths you are able to run just by typing the name of the executable instead of the full path.

Process Management

ps

ps command will display all the current processes. To list all the system processes ps -eF can be used. To stop any process, the command will be kill <PID of the service>.