TryHackMe - Learning Linux
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.txtwould 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 defaultsleep 10would wait for 10 seconds, and during this time no other command can be executed, however by usingsleep 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 commandcat text.txt | grep Hellowould 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>
| Value | Permission |
|---|---|
| 1 | Execute |
| 2 | Write |
| 3 | Execute + Write |
| 4 | Read |
| 5 | Read + Execute |
| 6 | Read + Write |
| 7 | Read + Write + Execute |
Understanding Permission Strings
-rwxrwxrwx
- Character 0 tells file type:
-for files,dfor directories - After that, each 3 letter group shows permissions: 1st for user, 2nd for group, 3rd for all others
| Character | Meaning |
|---|---|
r | Read access |
w | Write access |
x | Execute access |
- | Access denied |
File Operations
| Command | Description |
|---|---|
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.
-PERMto find files with specific permissions. Ex:find -PERM 777-userto 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
| Path | Description |
|---|---|
/etc/passwd | Stores user information - Often used to see all the users on a system |
/etc/shadow | Has all the passwords of these users |
/tmp | Every file inside it gets deleted upon shutdown - used for temporary files |
/etc/sudoers | Used to control the sudo permissions of every user on the system |
/home | The directory where all your downloads, documents etc are. Equivalent on Windows: C:\Users\<user> |
/root | The root user’s home directory. Equivalent on Windows: C:\Users\Administrator |
/usr | Where all your software is installed |
/bin and /sbin | Used for system critical files - DO NOT DELETE |
/var | The 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>.