Linux fundamentals — Summary:

Mukilan Baskaran
InfoSec Write-ups
Published in
8 min readAug 20, 2022

--

Locate :

When it’s not locating the temp.txt then go and update the DB.

The command for updating the DB is

sudo updatedb

Then start typing locate temp.txt it will locate the file.

Man:

The manual command is used to describe the other commands, like how it’s working and how to use it.

man ls

Which:

Check whether the software is installed or not.

It will look into what directory particular software is located.

which python3

Whatis:

The command is meant to describe what the particular is about.

whatis ls

Alias:

Before explaining what is this command about I will like to give an example of it.

alias temp=’ls -a’

History:

It is used to list out what is the previous command type in the terminal.

History

note: The history command does not work while you close the currently active terminal.

Some more Linux commands:

0 - Stdin (Input Stream)

1 - Stdout (Output Stream)

2 - Stderr (Error Stream)

Where 0 denotes input that is pointed towards the file descriptor 0. 1 denotes output is pointed to the file descriptor 1. 2 meant for error is pointed to the file descriptor 2.

Standard error descriptor example:

cat log.txt

Mahesh 2 > cat log.txt

Then checkout log.txt using cat

It will show output like

Mahesh command not found

note: We are familiar that number 2 is a standard error descriptor it would show

command not found error

Mahesh 2 >> log.txt

The above “>>” show displays the content without overriding already existing contents.

example: while you view cat log.txt this results in

Mahesh: command not found

Mahesh: command not found

without overriding the contents.

rm:

rm is used to remove the file or folder(empty).

rm log.txt

To remove the folder which is not empty.

rm -rf foldername

Echo:

The echo command is used to print the contents in the file.

echo “hello” > log.txt 2>&1

It will print hello in the log.txt without any error (2 - is the error stream and 1 is the output stream).

Find:

This command shows a list of directories with a permission denied statement list.

find / -type f -name log.txt

/ - Signifies root directory

f - Search for file

name - Name of the file

While typing the above command it would list out all permission denied statements and it’s tough to find the particular file we need.

So we use /dev/null for concatenation of the find command of the log.txt

less:

The less command displays content one screen at a time.

ls -l | less

cut:

In UNIX, the cut command is used to cut out sections of files and write the results to standard output. It can be used to cut sections of a line based on byte position, character, and field. In log.txt having content “hi”.

cat log.txt | cut -c 1 (“|” pipeline used to perform more than one operations simulataneously)

output is h (display the first letter of the string)

cat log.txt | cut -c 2

output is i (display the second letter of the string)

cat log.txt | cut -c 3

It will display the empty character because there is no character after i

cat log.txt | cut -c 1–2

output: hi (total number of strings is from the range of 1 to 2)

ls -l | cut -d “ “ -f 1

This will list out the permission present for a particular file

output:

total

— rw — r — — r — —

— rw — r — — r — —

Display the first column of ls -l and followed by space which means it prints the first field only

ls -l | cut -d “ ” -f 2

Likewise, it will display the second fields of a long listing of directories.

ls -l | cut -d “ “ -f 3

It will display the second field of long listings of directories.

Mahesh

Mahesh

NL: Numbering lines

Before using nl just concatenate the contents without overriding the contents.

echo “hello world” >> log.txt

echo “ironman” >> log.txt

echo “superman” >> log.txt

echo “spiderman” >> log.txt

nl log.txt

output is

  1. hi
  2. hello world
  3. ironman
  4. superman
  5. spiderman

head:

Head display the contents from the first line

head -n 3 log.txt

output:

hi

hello world

ironman

tail -n 2 log.txt

output:

superman

spiderman

Displays the last two contents of the file.

file permission:

ls -l it is used to display the long listing of a file.

output:

— rw —r — — r — — 1 Mahesh Mahesh 59 Aug 09:23 log.txt

The first dash denotes the text file type of “r” reading and “w” writes permission for the user to have given( — rw — ).

“r” followed by double dashes specifies read permission only given for groups (r — — ).

Final read-only permission is given to others (r — — ).

If drwxr — xr — x specifies directory type read write execute permission is given (“d” directory).

To change the permission, we need to use the chmod command.

Sort:

sort log.txt

Contents get sorted in ascending order.

output:

hi

hello world

ironman

superman

spiderman

sort -r log.txt

It helps to sort the contents in reverse order.

output:

spiderman

superman

ironman

hello world

hi

unique:

It checks when 2 lines are identical and limited to one line.

sort log.txt | unique

WC:

word count (wc)

It will calculate the number of words in the file.

wc log.txt

It would print

5 5 59 — Five lines , Five Words and 59 characters of bytes.

wc -l log.txt — “l” specifies number of lines present in the text file.

wc -w log.txt — “w” specifies number of words present in the text file.

wc -c log.txt — “c” specifies number of characters present in the text file.

Grep command:

Grep command matches the pattern given and prints only the line that matches the pattern.

cat log.txt | grep “hello”

hello world will be the output.

cat log.txt | grep -v “hello”

it will print all except the word hello or world including hello.

chmod:

Changes the permission of the text file.

4 - read

2 - write

1 - execute

for example:

chmod 777 log.txt

When you give this command it gives full permission to users, groups, and others with all read-write, and execute permission.

chmod 766 log.txt

It gives users all permissions and groups, others only have read-write permission.

— rwxrw — rw —

If you created a new file that file has default permission.

— rw — r — — r — — new.txt

umask:

If you give

umask 777

This permission was added to the new.txt

Special permissions:

SUID,SGID,Sticky bits

If you want to execute the binaries it asks for sudo permission the most highly privileged permission.

To run the binary every time sudo permission is required to avoid this special permission required.

chmod u+s temp

After that permission would be changed into

-rwSr — —r — — (S comes in user level permission which setuid)

To enable execute permission we need to chmod

chmod +x temp

permission becomes

-rwSr-xr-x

To make setgid

touch temp2

chmod g+x temp2

Then the permission be like

-rw-r-Sr — —

Sticky bit:

The purpose of a sticky bit is useful when sharing resources in the network of your computer.

chmod a+x temp2

output is -rwxr-Sr-t

It will prevent any other user to delete the contents of the folder except the owner or root user. It’s a safe and secure way to host your directories in the network.

setuid binaries -> establishing the setuid binaries are called SUID binaries.

These binaries are very prone to privilege escalation.

One way to find suid binaries is:

find / -perm -u=s (This is to find userbit is equal to set).

find / -perm -u=s -type f 2 >/dev/null

Linux file Structures:

/ -> indicate root

bin -> we have all binaries in bin directories.

dev -> you find the list of all device files.

lib64 ->contains libraries of 64-bit shared

mnt -> if any other partition or any Pendrive inserted you will be mounted at mnt point.

root -> root folder contains root files

snap -> if you wanted to install any packages snap is used.

tmp -> temp folder contains temporary files.

Boot -> contains a file required for booting Linux.

etc -> contains configuration files of applications and users.

opt -> additional software added to the opt.

cdrom -> mount cd or DVD ROM and have folder contains for many other users.

sbin -> system binaries which give specific information about the system.

var -> we have backup files

sub-directories present in var are

  • > cache (cache is local cache)
  • > lib contains library data
  • > you can install one software at a time
  • > snap contains snap files
  • > temp contains temporary files.

How to add users to the Linux system: (user management)

sudo adduser Mahesh

How to delete users present in the Linux system:

sudo deluser Mahesh

It would delete the Mahesh user

cat etc/passwd - It is used to view the configuration of the passed file.

cat etc/shadow - It is used to view unencrypted passwords.

env:

env is environment variables that tell all environmental command variables.

One environment among the environmental variable is PATH.

If you type “ls” it would check “ls” binaries available in the path environmental variable.

If you type “Mahesh” as a command in the terminal it shows the “Mahesh” command not found.

To access what is in the environmental variable use

echo $HOME -> /home/Mahesh

echo $USER -> Mahesh

export:

The export command is used to assign a value of the command to a variable.

export $ip = google.com

export PATH = $PATH:/home/Mahesh/Desktop

Software Management in Linux:

To install any software (or) tools in Linux use the command

“sudo apt install software_name”

Before installing any software we need to update the system

“sudo apt update”

while updating the system it must be saved on repositories. Each software package is also stored in repositories.

To check whether the application or software is installed or not.

“apt-cache search chrome”

To install Debian files without error there is a command for that:

“sudo apt install gdebi”

“sudo gdebi example_amd64.deb”

To remove any software installed:

“sudo apt remove”

Cron jobs:

If you want to run a task on a particular day a particular time or a particular month automatically schedule jobs by cron jobs.

“cronjob -l”

m h dom mon dow - specifies minute, hour, day of the month, day of the week.

export EDITOR = /bin/nano

crontab -e (It’s used to edit the crontab contents)

Then we can add

  • *** echo “hi” >> ~/Desktop/temp.txt
  • Once you typed the content in the crontab editor it would execute the command.
  • **** echo “hi” >> ~/Desktop/temp.txt
  • 30 2 **** echo “hello” >> ~/Desktop/temp.txt

From Infosec Writeups: A lot is coming up in the Infosec every day that it’s hard to keep up with. Join our weekly newsletter to get all the latest Infosec trends in the form of 5 articles, 4 Threads, 3 videos, 2 Github Repos and tools, and 1 job alert for FREE!

--

--