0% found this document useful (0 votes)
33 views54 pages

Linux Handout

Linux handout for cyber security training

Uploaded by

tiwary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views54 pages

Linux Handout

Linux handout for cyber security training

Uploaded by

tiwary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

Linux and Basic Tools

Overview and Usage

Hartwig Anzt
– based on material from Adrian Stein, Thomas Lindae

School of Computation, Information and Technology


Technical University of Munich

October 6, 2024
Linux

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 1/46
Linux
Linux is a generic name for a family of open-source Unix-like operating systems.

■ Based on the Linux kernel (initially by Linus Torvalds in 1991)


■ Large-scale free open source project
■ Linux distribution includes, kernel, system software, libraries
■ Popular distributions: Debian, Fedora Linux, Arch Linux, Rocky Linux, and Ubuntu
■ Commercial distributions: Red Hat Enterprise Linux, SUSE Linux Enterprise
■ Originally developed for x86 architecture

We use Linux on personal computers, edge devices (watch, cellphone. . . ), embedded systems (microwave, car. . . ), servers,
and high performance computing (HPC) systems.

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 2/46
Linux Distributions
The distributions differ in

■ Package Manager and Installer


■ Architecture (x86-64 for AMD and Intel, Apple, ARM. . . )
■ Hardware Support (i.e. drivers for non-standard hardware)
■ Release Cycle
■ Documentation
■ Support
■ License
Overall, Ubuntu is probably a good choice for personal computers, for HPC systems, one typically relies on Rocky Linux, Red
Hat Enterprise Linux, etc.

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 3/46
The File System

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 3/46
File Systems
The file system organizes the data on the physical drives (HDD/SSD/USB, etc). There exist different formats to organize the
data, and not all formats are compatible with all operating systems.
■ Linux-typical:
□ ext4
□ Btrfs

■ Windows-typical:
□ FAT32
□ exFAT
□ NTFS

■ MacOS-typical:
□ APFS
□ HFS+

FAT32 is supported also by MacOS and Linux, ext4 requires additional drivers on Windows and MacOS.

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 4/46
Directories and file names
File systems typically support organizing files into directories, also called folders, which segregate files into groups.

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 5/46
Directories and file names
File systems typically support organizing files into directories, also called folders, which segregate files into groups.

A file name, or filename, identifies a file for an application or user. For this, a file name has to be unique in a folder.

The path is the global address of a file.

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 6/46
Navigating and organizing the file system

■ ls show content of current folder

■ ls -l show content of current folder with details

■ cd .. go up one folder

■ cd foo go into folder “foo”

■ pwd show path to current folder

■ mkdir foo create folder “foo”

■ cp (-r) foo bar copy “foo” to “bar” (-r copies all content)

■ mv foo bar rename “foo” to “bar”

■ rm (-r) bar remove “bar”

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 7/46
Terminal use

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 7/46
The Terminal
The terminal is an interface for the user to interact with the computer. (In the past, before GUIs were supported, it was the only
form of interaction).

The terminal

■ accepts commands from the user


■ displays output from the computer
■ allows for full control of all computer applications

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 8/46
What are the advantages of the terminal?

■ Interaction is efficient and precise


■ Allows for automation (scripts)
■ Flexibility in the usage of programs and interaction between
■ Resource efficient:
□ Remote connection requires little bandwidth
□ Remote connection via terminal usually possible
□ Terminal use is very similar across operating systems

■ Servers usually do not support GUI

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 9/46
Command Line Arguments

Command Arguments and Options


command [OPTION...] arguments

■ command —The command to be executed

■ [OPTION...] —Options for the command

□ Long-options (e.g. --version ) and Short-options (e.g. -v )


□ Some options take additional arguments
□ Options without arguments are called flags
□ Useful flags: --help (or -h ) and --version (or -v )

■ arguments —Additional arguments (e.g. files, paths, etc.)

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 10/46
Essential Commands

xkcd 1168

You do not need to know all commands by heart, but it is useful!

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 11/46
Basic Commands | man
Stands for “manual”.

Opens the usage description for a program.

■ man <command> (e.g., man man )

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 12/46
Basic Commands | ls
Stands for “list”.

Lists the content of a folder.

Usage:

■ ls

■ ls path/to/folder

■ ls -a —Shows also hidden files (folders/files that start with . )

■ ls -l —Lists details (type, size, access rights, date. . . )

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 13/46
Essential Commands | cd
Stands for “change directory”.

Change of the current working directory.

Usage:

■ cd path/to/folder

■ cd .. —Change to the parent directory

■ cd ~ —Change to the Home directory

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 14/46
Basic Commands | pwd
Stands for “print working directory”.

Shows the current working directory.

Usage:

■ pwd

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 15/46
Basic Commands | cat
Stands for “concatenate files and print”.

Displays files.

Usage:

■ cat test.txt —Prints the content of test.txt

■ cat one.txt two.txt —Prints the content of one.txt and two.txt

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 16/46
Basic Commands | echo
Gibt den Angegebenen Text wieder aus.

Usage:

■ echo "some text"

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 17/46
Basic Commands | mkdir
Stands for “make directory”.

Creates folder.

Usage:

■ mkdir folder —creates folder folder in current directory

■ mkdir f1 f2 f3 —creates folders f1 , f2 and f3 in current directory

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 18/46
Basic Commands | rm
Stands for “remove”.

Removes files or folders

Usage:

■ rm test.txt

■ rm -d empty-folder

■ rm -r nonempty-folder —Folder with content have to be deleted recursively

■ rm -f test.txt —Forces the deletion

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 19/46
Basic Commands | mv
Stands for “move”.

Change a file location or renames a folder.

Usage:

■ mv path/to/file new/path/ —Moves file from path/to/ to new/path/

■ mv path/to/file new/path/elif —Changes the location and the filename from file to elif

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 20/46
Basic Commands | cp
Stands for “copy”.

Copies files or folders.

Usage:

■ cp path/to/file other-path/to/copy —Copies file from path/to/ to other-path/to/ and stores the copy as
copy

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 21/46
Basic Commands | sudo
Stands for “su-do” (“substitute user do”, or “superuser do”).

Call a command as a different user (usually the superuser’s).

sudo is needed if the user rights of the current user are not sufficient:

■ sudo dnf install terminator —the installation of a new package (“terminator”) requires the rights of a superuser

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 22/46
Control Operators
&&
■ Combine two commands – the second command is only executed if the first was successful.
■ For example: cd foo && ls

||
■ Combine two commands – the second command is only executed if the first was not successful.
■ For example: cd foo || echo "Folder does not exist"

;
■ Combine two commands.
■ For example: cd folder; ls

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 23/46
Control Operators
&
■ The precious command is executed in the background
■ For example: sleep 10 &

> and >>

■ Redirects the output, > overwrites previous content.

■ For example: echo "HelloWorld!" > greeting.txt or echo "HelloWorld!" >> greeting.txt

| (called “Pipe”)
■ The output of the precious command is the input of the next command
■ For example: ls | sort

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 24/46
Useful Tricks

■ Recall previous command: upward arrow


■ Complete path: tab
■ Delete current input: Ctrl + u
■ Cancel current input: Ctrl + c
■ Copy terminal input: Ctrl + Shift + c
■ Copy terminal input: Ctrl + Shift + v
■ Remove recent command + output: Ctrl + l

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 25/46
Access Control

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 25/46
Access Control
Controlling the access and privileges of users is important!

■ Data can be private, sensitive, export control. . .


■ Often, many users interact with the same file system

Linux uses Access-Control-List (ACL )

■ ACL is operating from the object size : Every object exposes different rights for different groups
■ Rights given to Owner, Group and Other
■ Rights include:
□ Read
□ Write
□ eXecute

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 26/46
Access Control
To change the permissions of a file, one uses the chmod command, with the following syntax:

■ chmod [references][operator][modes] filename The references are shorthand ( u , g , or o ) for each class. The
operator determines whether to add (+), remove (-) or explicitly set (=) the particular permissions. The modes are read
(r) , write (w) , or execute (x) .

■ For example, to add the execute permission for the user to file1 :
chmod u+x file1

■ To remove the write permission for others for file2 :


chmod o-w file2
■ You can combine multiple references and modes to set the desired access all at once. For example, to explicitly make
file3 readable and executable to everyone:
chmod ugo=rx file3

■ The all (a) mode is the same as ugo , allowing the previous command to be expressed as:
chmod a=rx file3

For more information on changing permissions, run man chmod

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 27/46
Environment Variables

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 27/46
Environment Variables
Environment Variables enable passing configurable variables to processes via the environment.

Beispiele für Environment Variables:

■ HOSTTYPE—Architecture of the computer (e.g. x86-64)


■ HOME—Path to /home/<username>
■ PWD—Path to current working directory”
■ PATH—Path to executables
■ TEMP/TMP—Path to temporary data

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 28/46
SSH

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 28/46
SSH
SSH stands for Secure Shell:

■ Enables a secure connection between servers (e.g. Internet)


■ Enables secure remote server administration
■ Identities, passwords, files are encrypted

We can connect to a remote server this way:

ssh <username>@lxhalle.in.tum.de

: Connection can be closed with exit .


In the first connection, the fingerprint Fingerprints need to be verified. (Is it the right server?)

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 29/46
SSH Keys
Instead of verification with a password, we can also use a set of ssh keys. Generation:

■ ssh-keygen -t rsa -b 4096 for an RSA (Rivest-Shamir-Adleman) key pair

■ ssh-keygen -t ed25519 for an ECC (Elliptic Curve Cryptography) key pair

A key pair consists of:

■ a public key (e.g. ~/.shh/id_ed25519.pub )

■ a private key (e.g. ~/.ssh/id_ed25519 )

: It is an asymmetric encryption strategy!

■ The public key can be given to anyone. I.e. stored on the server we want to access. (Rechnerhalle, LRZ, GitHub, etc.)
■ The private key has to be kept private! Never share it!

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 30/46
Code Editing

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 30/46
Editors

xkcd 1823

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 31/46
Text Editors and IDE’s
Text editors are lightweight tools for editing text or coding:

■ nano
■ vi, vim, neovim
■ Sublime Text
■ Visual Studio Code
■ VSCodium

IDE Stands for Integrated Development Environment. IDE’s add tools that ease the software development:

■ IntelliJ
■ Eclipse
■ Visual Studio

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 32/46
Terminal Editors
Terminal editors line nano, vi, vim are pre-installed on almost every system.

nano:

■ Easy to use
■ In the manual of nano, ^ refers to Ctrl

■ nano is quit with ^X (Ctrl+X )

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 33/46
Terminal Editors
vim:

■ Operates with “Modes” (e.g. INSERT-MODE and NORMAL-MODE)


■ vim starts in NORMAL-MODE, which allows the commands:
□ :q —“Quit” (quits vim)
□ :w —“Write” (writes the edited text)
□ :wq —“Write and Quit”
□ ! forces the command
□ with i the editor changes to INSERT-MODE

■ The INSERT-MODE allows to


□ insert text
□ change to NORMAL-MODE with ESC

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 34/46
Useful Commands

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 34/46
Useful Commands | grep
Stands for “global regular expression search and print”.

With grep we can search for patterns.

Usage:

■ General use: grep "RegEx" Datei-Muster

■ grep "test" *.txt —Returns the rows of the .txt files that contain test

■ cat *.txt | grep "test" —Often used in combination with pipes |

■ Useful options are -c (for “count”) and -i for “ignore-case”

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 35/46
Useful Commands | wc
Stands for “word count”.

Returns the number of rows, words, and bytes of a file.

Usage:

■ wc test.txt

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 36/46
Useful Commands | ping
Sends an ICMP ECHO_REQUEST to the IP address or URL.

Useful to check the reachability of a website or server.

Usage:

■ ping zulip.in.tum.de

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 37/46
Useful Commands | scp
Stands for “OpenSSH secure file copy”

Copies files over the network between servers

Usage:

■ scp path/to/local/file <username>@<remote-URL>:path/to/remote/copy

■ e.g. scp test.txt <username>@lxhalle.in.tum.de:~/

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 38/46
Useful Commands | htop
“Task manager” of Linux.

Usage:

■ htop

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 39/46
Useful Commands | who
Shows who is logged in on a server.

Usage:

■ who

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 40/46
Useful Commands | more
The content of a file is shown page-by-page.

Usage:

■ cat huge-file | more

■ more huge-file

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 41/46
Useful Commands | less
less similar to more, but more powerful.
■ less does not have to process the whole file : faster
■ allows for scrolling the file row by row

Usage:

■ cat huge-file | less

■ less huge-file

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 42/46
Useful Commands | alias
Creates a alias command, a redirect.

Example:

■ alias wisdom="fortune | cowsay" —allows to call the combined command in one command: wisdom instead of
fortune | cowsay

Only valid for the current session : A persistent alias can be created in the user environment (e.g. ~/.bashrc or ~/.zshrc )

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 43/46
Useful Commands | history
Displays the history of the used commands.

Usage:

■ history

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 44/46
Useful Commands | tree
Displays the file system below the current path.

Usage:

■ tree

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 45/46
Useful Commands | pushd and popd
Stands for “push directory” and “pop directory”.

Simplifies to copy file paths.

Usage:

1. pushd /bin —stores pwd and switches to /bin

2. popd —switches pwd to the previous path

Hartwig Anzt – based on material from Adrian Stein, Thomas Lindae Linux and Basic Tools October 6, 2024 Page 46/46

You might also like