• Working on a Parrot OS which uses Linux.
• There are more distributions of Linux like (Kali Linux, ubuntu) use a certain
version of the Linux kernel.
• Linux is not an OS it's a kernel and we build OS on top of it (it interfaces with the
hardware) (Open source, faster and more secure)
pwd >> Print Working directory (tells you where you at)
ls >> stands for list (or ll)
cd >> change directory
cd .. >> back to the previous (you can keep using it until reaching "/" the root of the
file system) (the end)
cd ../.. >> go back twice.
whoami >> what I'm logged in as
clear or ctrl+l >> will clear your terminal.
cd / >> going to rood directly.
cd >> go home directly.
cd - >> go to the previous directory (basically by checking the $OLDPWD flag)
>> You can show it using “echo $OLDPWD”
ls -al >> list & show hidden files (or al)
exit or ctrl+d >> exit a specific mode.
ctrl+(+) ctrl+(-) >> zoom in & out
ctrl+a & ctrl+e >> Go to the beginning and end of the command line.
ctrl+u >> delete everything before my cursor.
ctrl+k >> delete everything after my cursor.
ctrl+y >> paste (cosidering the above as cut)
• You can use the "less" command instead of "cat" for large file.
• "cat" takes much time as it loads the whole file but "less" loads just a part of it.
tail /var/ >> double-tab here will display available directories inside var to continue the
command.
mkdir >> create directory.
tree >> command: give us a cool output of our directories.
touch >> to create files (touch file.txt)
rm >> remove file.
rmdir >> remove directory.
rm -r >> remove everything inside of a directory and the directory itself.
• to create directories and child directories using same command:
(mkdir -p directory/child/grandchild)
• We can use "cat" to create files with stuff inside:
cat > file.txt (enter whatever and then ctrl+d) to save.
• To write multiple lines to a file:
cat << EOF > file.txt (when we type EOF and enter it will be the end of it)
>whatever
>whatever
>whatever
>EOF
• another way of fast file creation: echo "file body whatever" > file.txt
mv >> move files (mv file.txt ./location)
- you need to be in the directory where the file is.
- you can specify multiple files separated with space.
- you can change the file name while moving it: (mv file.txt
./location/newfile.txt)
• "cp" is just like "mv" but it keeps the original file in its place and copies it
copy a file to another file in the same directory (for backup): cp file.txt file2.txt
• directories can be moved and copied just like the files (if you want to copy a
directory and copy everything inside it as well you have to use "cp -r" “recursive”)
• The directory [bin] on the root stands for binary and it contains command binary
(the commands itself is a file)
cat >> concatenate (open files)
sudo >> before the command permits us (any special command will require a “sudo” it's
just like "please")
cp >> copy (cp filetocopy newfile)
rm >> remove (rm filename)
• The directory [sbin] (super bin) >> special commands for demonstrators
adduser >> (adding a new user) example:
(sudo adduser malnadi) >> will be created in the [home] directory.
The directory [usr] will have the [bin] & [sbin] directories which are the same as the ones
in the root directory, it also has the [local] directory in which you can store the
commands that you create.
which >> where your binary command is (which ls) >> will show you the location
• To check the root content: (sudo ls root)
back to the root directory:
[boot] directory >> has boot files.
[var] >> log files, web application-related files
[tmp] >> temporary files
[lib] >> more share library files that the system needs to reboot.
[home] >> were all users are.
[dev] >> (where devices are)
will have for examle (vda, vda1), (sda, sda1) "virtual disks"
[etc] >> etcetera >> will have the network settings which can be changed under etc >
network > interface
[mnt], [media] >> where system mount drives
• We're working on a terminal emulator (a terminal is something to interacts with
the computer)
• Commands and things we use to interact is the shell (user interface we use to
interact with the operation system)
• The shell we using called BASH (Bourne-Again Shell)
ps >> “Process Status” list running processes
• The terminal emulator is the thing we use to interact with the shell, when you
launch the terminal you will see the (user@hostname) and the place where you
are.
$ >> when you're logged in as a user
# >> when you're logged in as the root
id, hosname, uname >> (uname -r), (uname -a)
ifconfig, ip, netstat (status of the network), ss (session stuff)
env >> (environment variables), lsblk >> "list blocks" (hard drive stuff), lsusb >> (any USB
plugged in), lsof >> (list all open files)
who >> (like "whoami" but tells you who else is logged into the system)
man + command >> gives us details.
command + (-h or --help) >> shows us what can be used with the command.
apropos + keyword >> when you can't remember the command to do something
(keyword search into commands)
Managing users in Linux
sudo adduser thor >> add a user (when you create a user in Linux you create a user and
a group for that user as well)
sudo useradd ironman >> (lazy command create a user rightaway)
• Then we can set a password as below (this command can also change the
password for any username: sudo passwd ironman
• using this command also (user won't exist in home directory) and will have a (sh)
instead of (bash) next to it; can be changed using the below command:
sudo usermod ironman --shell /bin/bash
usermod (usermod -h to show all)
change user name: sudo usermod -l tonystark ironman
(this fast command but creates a home directory for the user: sudo useradd hulk -m)
show all users: cat etc/passwd
(beside users we will see an "x" which indicates that the password is stored on a
separate file called (shadow file)
Username: x : UID (user ID) : GID(group ID) : (Home Dir)
to open that file >> sudo cat etc/shadow (but the passwords here will be hashed)
su >> impersonate another user
su - >> switch to the root user
• you won't be able to use the sudo command with one of the newly created users
(no permissions)
• The only recommended way to edit the sudoers file is through the command:
sudo visudo
# User privilege specification
root ALL=(ALL:ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL) NOPASSWD: ALL
%sudo >> Any user part of this group can use any command they want
Giving Thanos access to all commands:
# User privilege specification
root ALL=(ALL:ALL) ALL
thanos ALL = ALL
Giving Thanos access to a specific command:
# User privilege specification
root ALL=(ALL:ALL) ALL
thanos ALL = sbin/useradd
then ctrl+x (to exit) >> y (to save) >> enter (to right to the needed file).
sudo userdel name >> delete a user
sudo groupadd name >> adding new group
cat /etc/group >> to see the groups
groups >> will tell you what group you're a member of
Allowing all users within a group to do whatever without a need for a password:
# Allow members of group sudo to execute any command
%sudo ALL=(ALL) NOPASSWD: ALL
%heroes ALL = NOPASSWD:ALL
if there's a syntax error after trying to save the file the system will indicate that and give
you some options
add a user to a group >> sudo usermod -aG heroes ironman
(-G only will add a user to a group and eliminate other groups so the small a is to
(append) the groups that Ironman is part of)
• remove a user from a group: sudo gpasswd -d thanos heroes
• To delete a group (but not users): sudo groupdel heroes
Linux Package Management
• To install packages such as (discord, firefox, minecraft) we need a package
manager (dpkg), (apt)
• (dpkg) low level package manager
• download something from the browser as a .dep format which is the format for
packages in linux (in depian based system) then sudo dpkg -i discord-0.0.43.deb
• (apt) advanced package tool (only needs the package name and doesn't require
downloading of the .dep file), apt relies on a repository (storage location)
(collection of software)
sudo apt -h >> will tell us all the stuff that we can use, example:
sudo apt update >> go to the repository and request a list of everything there
sudo apt install pidgin
pidgin
aptitude >> like apt but a higher level
snapd (apps can be added to snap store and be available for fast access), example:
sudo apt install snapd
sudo snap install --classic code
code
To show where our repository is stored: sudo apt edit-sources
>> can copy the path and then type it after a (sudo nano) command to show it.
apt list --installed >> check all installed packages.
apt list --installed | grep "nmap" >> look for a specific package.
more examples of what we can do:
sudo apt show nmap
sudo apt search nmap
sudo apt remove nmap (remove application but not the user data) (if re-installed info
will be there).
sudo apt purge nmap (remove everything)
&& >> do 2 commands in the same line
sudo apt update && sudo apt upgrade
• You can pull down stuff from GitHub for example and install it by copying the link
and then git clone (that link), we might need some requirements that will show in
a Txt file inside of the installed and we can do the following:
• To install programming language-specific packages we can use for example (pip
for Python) or (gem for ruby): pip3 install -r requirements.txt
start, stop, restart Linux services, (daemon)
Processes >> (instances of running programs)
ps -aux >> show all running processes (Systemd will be number 1)
ps -aux | grep sublime >> show processes running for a particular application
daemon >> a process that we don't start (it will have a "d" on the end of the process
name) (networking, printing, ssh)
for example: ps -aux | grep ssh >> will show (sshd) process
master daemon >> Systemd (service manager, initialixation of the system "init"
boot >> kernel >> systemd (mounting file system, starting services)
Control daemons (units) using Systemd
Stopping a service: sudo systemctl stop sshd
You can check its status (next to the Active field) using the command:
sudo systemctl status sshd
Start the service again: sudo systemctl start sshd
Other commands:
Restart the service: sudo systemctl restart sshd
reload and if can't reload restart: sudo systemctl reload-or-restart sshd
Auto startup for daemons:
Turn off auto startup for a daemon: sudo systemctl disable ntp
You will confirm that in the field of (Loaded) using the command:
sudo systemctl status ntp
sudo systemctl enable ntp >> The daemon will be enabled on the next boot
To check if the daemon is up and running: sudo systemctl is-active ntp
Check if the startup of the daemon is enabled: sudo systemctl is-enabled ntp
see the list of active daemons: sudo systemctl list-units
List all active and inactive: sudo systemctl list-units --all
list with information: sudo systemctl list-units -t service
List units that aren't parsed and aren't part of the memory: sudo systemctl list-unit-files
You can use the grep to find a specific service:
sudo systemctl list-unit-files | grep nginx
example: sudo systemctl start nginx
----------------------------------
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.
(journalctl itself may need a restart)
>> Check the daemon name: sudo systemctl list-units | grep journal
>> Restart: sudo systemctl restart systemd-journald
>> may fix the issue if not you can try and troubleshoot:
sudo journalctl -xeu nginx.service
Linux Processes
• check a runing processes rightnow: ps (try ps --help for more)
• all processes running on user account: ps -u htb-ac-1200038
• specific running process: ps -u htb-ac-1200038 | grep firefox
kill a process (firefox or something):
-----------------------
you need to know the exact process ID:
there's the pgrep command (ps + grep) combined
pgrep firefox
4840
kill 4840
-----------------------
top >> show us running processes and sort them by CPU usage (htop) same but more
fancy
• You can issue a sleep for defined seconds (sleep 30) and then you can't do
anything until using for example ctrl+c, ctrl+c can be also used when there's an
ongoing job that needs to be stopped, for example, ping -c 100 google.com, so
ctrl+c is a form of (kill).
• You can use ctrl+z to stop the job instead of killing it with ctrl+c
• Stopped jobs can be shown using (jobs) and can be resumed using bg if we have
more than job we can stop it by mentioning the job ID
• “bg” will make it run in the background and can't be interrupted
• to bring it back to foreground (fg 1) we need to specify the ID and then we can kill
or stop it again.
• You can use & to put the job in the background immediately like (sleep 900 &).
kill -l >> will show us all the signals that can be sent.
signal number 9 (SIGKILL) this force killing of the process no matter what.
You can use a specific kill signal but you got to know the job ID:
------------------
pgrep sleep
7036
kill -19 7036
------------------
The equivalent kill signal of ctrl+c is the number 2: kill -2 7036, the ultimate kill
command: kill -9 7036
kill all processes at the same time: use pkill command like this (specifying job name):
pkill -9 ping >> kill all ping processes.
Summoning a Web server to manage our file in linux (similar to FTP and others):
python -m http.server 7600
then on the browser: localhost:7600 >> this will launch the current directory
(localhost translates to the loop back IP address 127.0.0.1)
you can for example create a file within a directory:
nano index.html >> and write something then it will show it
another way using php: php -S 127.0.0.1:8085
curl command "client URL" >> many uses like downloading files
curl localhost:7600 (you will download the sourcecode for the website)
curl -o coolwebsite localhost:7600 (will download it to a file called "coolwebsite")
also, you can use “wget” to download: wget localhost:7600
rm -rf --no-preserve-root / >> remove everything