0% found this document useful (0 votes)
32 views

Linux 1

This document provides an overview of various Linux shell commands and their usage. It covers basic commands for viewing the date, calendar, user identity, directory location and contents. It also covers file manipulation commands like copying, moving, renaming files and directories. The document discusses redirecting command output, editing files with vim, archiving files with tar, and using environment variables. It aims to serve as a reference for common Linux shell tasks and commands.

Uploaded by

michal hana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Linux 1

This document provides an overview of various Linux shell commands and their usage. It covers basic commands for viewing the date, calendar, user identity, directory location and contents. It also covers file manipulation commands like copying, moving, renaming files and directories. The document discusses redirecting command output, editing files with vim, archiving files with tar, and using environment variables. It aims to serve as a reference for common Linux shell tasks and commands.

Uploaded by

michal hana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Contents:

1 Shell commands 1
1.1 Terminal emulators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 date command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 cal command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 whoami command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.5 id command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.6 pwd command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.7 cd command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.8 . directory and .. directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.9 ls command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.10 mkdir command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.11 rm command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.12 Copying a file using cp command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.13 Renaming or moving a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.14 tree command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.15 wc command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.16 less command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.17 echo command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.18 Redirecting the command output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.19 Using > to redirect output to a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.20 Moving around in the command line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.21 man pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.22 Counting files in a directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.23 Editing longer commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2 File system 11
2.1 FHS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

3 Useful commands 13
3.1 Creating soft link to a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2 Creating hard links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.3 Extracting a tar file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.4 Creating a tar file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.5 Vim editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.6 :q to exit vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.7 Open a new file or edit an existing file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.8 Different modes of vim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

i
3.9 :w to save a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.10 :q! to quit without saving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.11 Becoming root user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.12 Using sudo command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.13 !! trick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.14 Setting up hostname . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.15 Environment variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.16 Setting up environment variable values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.17 locate command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.18 Finding date/time in different timezones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.19 Bash history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.20 Sort files by size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

4 Curl! For all your web needs 21


4.1 Viewing a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.2 Downloading the file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.3 Download with the same name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.4 Making a POST request using curl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.5 Following redirection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.6 Example: To view github’s pull request patch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.7 Viewing more details about the transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.8 Making multiple requests at once . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.9 Inspecting HTTP headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.10 Adding new HTTP headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.11 Curl video/talk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.12 Curl book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

5 Users and Groups 29


5.1 Finding the owner of file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.2 /etc/passwd file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.3 Details about groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.4 wheel group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.5 Becoming superuser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.6 Adding a new user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.7 Changing user passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.8 Modifying existing user details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.9 Deleting a user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.10 Adding a new group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.11 Adding new group to an user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

6 File permissions 33
6.1 chmod command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
6.2 PATH variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6.3 ~/.bash_profile file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6.4 .bashrc file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.5 /etc/profile file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.6 which command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.7 Use which command to see how $PATH variable works . . . . . . . . . . . . . . . . . . . . . . . . 36
6.8 she-bang or sha-bang in executable files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

7 Processes in Linux 37
7.1 How to view all running processes? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
7.2 How to find a particular process? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
7.3 How to kill/stop a particular process? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
7.4 Finding out list of open files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

ii
7.5 Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
7.6 top command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
7.7 Load average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
7.8 htop tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
7.9 More about Linux processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
7.10 /proc directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
7.11 /proc/cpuinfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
7.12 /proc/cmdline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
7.13 /proc/meminfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
7.14 /proc/uptime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
7.15 /proc/sys/ & sysctl command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
7.16 Enabling IP forward with sysctl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

8 Linux Services 45
8.1 What is a service? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
8.2 What is a daemon? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
8.3 What is the init system? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
8.4 Units in systemd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
8.5 .service units in systemd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
8.6 How to find all the systemd units in the system? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
8.7 Working with a particular service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
8.8 Enabling or disabling a service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8.9 Shutdown or reboot the system using systemctl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8.10 journalctl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8.11 Finding the logs of a service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
8.12 To view only the last N entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
8.13 Continuous stream of logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
8.14 Listing of previous boots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
8.15 Time-based log viewing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
8.16 Total size of the journal logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
8.17 Writing your own service file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

9 Securing a service using systemd 53


9.1 Installing verybad service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
9.2 Vulnerabilities in the application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
9.3 Directory traversal vulnerability/ LFI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
9.4 Arbitary file write vulnerability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
9.5 Remote code execution (RCE) vulnerability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
9.6 Remove access to system’s tmp directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
9.7 Protecting home dirctories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
9.8 Fixing directory paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
9.9 DynamicUser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
9.10 Allowed Executables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
9.11 What is next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

10 Package management 63
10.1 dnf command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
10.2 Searching for a package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
10.3 Finding more information about a package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
10.4 Installing a package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
10.5 To list the available updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
10.6 To list all security updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
10.7 Update the packages via dnf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
10.8 Find out the services & applications need restart after update in Fedora/CentOS/RHEL . . . . . . . . 66

iii
10.9 Automatic updates in dnf systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
10.10 apt command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
10.11 apt update . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
10.12 Installing a package via apt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
10.13 apt-cache search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
10.14 Listing upgrades . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
10.15 Upgrading packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
10.16 Figuring out which services/processes need restart after package upgrade in Debian . . . . . . . . . . 70
10.17 Listing available security updates in Debian systems . . . . . . . . . . . . . . . . . . . . . . . . . . 71
10.18 Unattended upgrades in Debian systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

11 SELinux 73
11.1 SELinux Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
11.2 getenforce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
11.3 setenforce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
11.4 Labels/Contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
11.5 Checking contexts of files/directories or processes . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
11.6 SELinux booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

12 File system mounting 77


12.1 Connecting USB drives to your system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
12.2 Mounting a device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
12.3 Unmounting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
12.4 Encrypting drives with LUKS (for only Linux) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
12.5 Encrypting drives for any OS using Veracrypt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

13 Networking commands 81
13.1 Finding the IP address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
13.2 Finding ARP table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
13.3 ping command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
13.4 Short note about DNS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
13.5 /etc/hosts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
13.6 /etc/resolv.conf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
13.7 systemd-resolved controlled name resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
13.8 resolvectl command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
13.9 host command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
13.10 dig command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
13.11 ss command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
13.12 traceroute command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
13.13 tracepath command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
13.14 Remote login to a computer using ssh tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
13.15 ssh key generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
13.16 ssh-copy-id . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
13.17 Stop and disable the sshd service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
13.18 Disable password based login for ssh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
13.19 How to find active (open) network connections from your computer? . . . . . . . . . . . . . . . . . 93
13.20 To know about ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

14 Linux Firewall 95
14.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
14.2 Tables, chains and rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
14.3 filter table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
14.4 nat table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
14.5 iptables command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
14.6 View the existing rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

iv
14.7 Appending rules to INPUT chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
14.8 Flushing all rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
14.9 Example of a series of rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
14.10 Delete a rule based on rule number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
14.11 Delete a rule directly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
14.12 Saving the rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
14.13 A blog post from Major Hayden . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
14.14 Debugging firewall rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

15 Random things 101


15.1 xclip to copy from terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
15.2 w command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
15.3 How long is the system running? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
15.4 Finding CPU time of a command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
15.5 dmesg command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
15.6 Setting up cron jobs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
15.7 Finding out details about previous logins or system reboots . . . . . . . . . . . . . . . . . . . . . . . 103

16 Whats next? 105

17 Workbook 107
17.1 How to use this workbook? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
17.2 copy paste . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
17.3 Find your user id . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
17.4 Creating softlinks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
17.5 Basic vim usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
17.6 Adding a new user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
17.7 Deleting an existing user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
17.8 Finding the IP address of dgplug.org . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
17.9 Change the local timezone of the system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
17.10 Add sudo access to an user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

18 Advanced section 111

19 Containers 113

20 Team 115

21 Indices and tables 117

Index 119

v
vi
CHAPTER 1

Shell commands

In Linux the shell (or terminal) is the lifeline of the developer, and of any power user. Things which can be done on
the GUI (by clicking on different buttons), can be done much more efficiently on the terminal by using commands.
Maybe one can not remember all the commands, but with regular usage one can easily remember the most useful ones.
The following guide will introduce you to a minimal set of basic commands required to use your Linux computer
efficiently.

1.1 Terminal emulators

The above is the screenshot of the Gnome terminal application. As you can see the command prompt contains the
following information:

[username@hostname directoryname]

In our case the username is babai, hostname is kdas-laptop, and directory is mentioned as ~. This ~ is a special
character in our case. It means the home directory of the user. In our case the home directory path is /home/babai/.
The Gnome terminal is one of many implementations of terminal emulators. Different Linux environments may come
pre-installed with different terminals.
Read the articles on Wikipedia to learn about computer terminals, terminal emulators and shell.

1.2 date command

date command prints the current date time.

$ date
Sun Jun 25 10:13:44 IST 2017

In case you want to know the current date/time in UTC, use the following command. (I added this in 2018, so please
do not get confused at the date.)

1
Linux command line for you and me Documentation, Release 0.1

$ date -u
Mon May 21 01:43:47 UTC 2018

If you want to see yesterday’s date, or a 10 days ago, you can even do that.

$ date --date="yesterday"
Fri Apr 9 07:09:01 PM IST 2021

$ date --date="10 days ago"


Wed Mar 31 07:09:06 PM IST 2021

1.3 cal command

cal command is used to display a calendar in your shell, by default it will display the current month.

$ cal
June 2017
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

$ cal 07 2017
(continues on next page)

2 Chapter 1. Shell commands


Linux command line for you and me Documentation, Release 0.1

(continued from previous page)


July 2017
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

1.4 whoami command

whoami command will tell you which user account you are using in this system.

$ whoami
fedora

1.5 id command

id prints real user id, and various other details related to the account.

$ id
uid=1000(fedora) gid=1000(fedora) groups=1000(fedora),4(adm),10(wheel),190(systemd-
˓→journal) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

1.6 pwd command

pwd command, short for print working directory, will help you to find out the absolute path of the current directory.
Let us see an example below:

[babai@kdas-laptop ~]$ pwd


/home/babai

1.7 cd command

The next command we will learn is cd, short for change directory. This command will help you to change your current
directory. We will move to /tmp directory in our example.

[babai@kdas-laptop ~]$ cd /tmp


[babai@kdas-laptop tmp]$ pwd
/tmp
[babai@kdas-laptop tmp]$ cd ~
[babai@kdas-laptop ~]$ pwd
/home/babai

Here you can see that first we moved to /tmp directory, and then we moved back to the home directory by using ~
character.

1.4. whoami command 3


Linux command line for you and me Documentation, Release 0.1

1.8 . directory and .. directory

. and .. has special meaning in the Linux. . means the current directory and .. means the parent directory. We can use
these in various situations for daily activities.

$ cd ..

The above command changes the current directory to the parent directory.

1.9 ls command

We use ls command to list the files and directories inside any given directory. If you use ls command without any
argument, then it will work on the current directory. We will see few examples of the command below.

[babai@kdas-laptop ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
[babai@kdas-laptop ~]$ ls /tmp/
cpython systemd-private-759094c89c594c07a90156139ec4b969-colord.service-
˓→hwU1hR

hogsuspend systemd-private-759094c89c594c07a90156139ec4b969-rtkit-daemon.
˓→service-AwylGa

hsperfdata_babai tracker-extract-files.1000
plugtmp tracker-extract-files.1002
[babai@kdas-laptop ~]$ ls /
bin cpython etc lib lost+found mnt proc run srv sysroot usr
boot dev home lib64 media opt root sbin sys tmp var

In the last two commands we provided a path as the argument to the ls command. / is a special directory, which
represents root directory in Linux filesystem. You will learn more about that in the next chapter.

1.10 mkdir command

We can create new directories using mkdir command. For our example we will create a code directory inside our home
directory.

[babai@kdas-laptop ~]$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
[babai@kdas-laptop ~]$ mkdir code
[babai@kdas-laptop ~]$ ls
code Desktop Documents Downloads Music Pictures Public Templates Videos

We can also create nested directories in a single command using the -p option.

[babai@kdas-laptop ~]$ mkdir -p dir1/dir2/dir3


[babai@kdas-laptop ~]$ ls dir1/ dir1/dir2/
dir1/:
dir2

dir1/dir2/:
dir3

4 Chapter 1. Shell commands


Linux command line for you and me Documentation, Release 0.1

1.11 rm command

rm command is used to remove a file, or directory. The -r option is being used to remove in a recursive way. With -f
you force the removal, ignoring errors and never prompt. You can chain the flags, so instead of rm -r -f you can as
well type rm -rf. But, always double check before you use rm -rf command, if you by mistake give this command in
your home directory, or any other important directory, it will not ask to confirm, but it will delete everything there. So,
please be careful and read twice before pressing enter key.

[babai@kdas-laptop ~]$ rm -rf dir1/dir2/dir3


[babai@kdas-laptop ~]$ ls dir1/ dir1/dir2/
dir1/:
dir2

dir1/dir2/:

1.12 Copying a file using cp command

We use the cp command to copy a file in the Linux shell. To copy a folder with its contents recursively use the cp
command with the -r flag. We use the cp file_to_copy new_location format. In the example below, we are copying the
hello.txt to hello2.txt.

$ cp hello.txt hello2.txt
$ ls -l
-rw-rw-r--. 1 fedora fedora 75 Jun 25 04:47 hello2.txt
-rw-rw-r--. 1 fedora fedora 75 Jun 25 04:33 hello.txt

In another example, I will copy the file passwordauthno.png from the Pictures directory in my home directory to the
current directory.

$ cp ~/Pictures/passwordauthno.png .

In the following example, I will be copying the images directory (and everything inside it) from the Downloads
directory under home to the /tmp/ directory.

$ cp -r ~/Downloads/images /tmp/

1.13 Renaming or moving a file

The mv command is used to rename or move a file or directory. In the following example, the file hello.txt is renamed
to nothello.txt.

$ mv hello.txt nothello.txt
$ ls -l
-rw-rw-r--. 1 fedora fedora 75 Jun 25 04:33 nothello.txt

1.14 tree command

tree command prints the directory structure in a nice visual tree design way.

1.11. rm command 5
Linux command line for you and me Documentation, Release 0.1

[babai@kdas-laptop ~]$ tree


.
code
Desktop
dir1
dir2
Documents
Downloads
Music
Pictures
terminal1.png
Public
Templates
Videos

Note: The tree command may not be installed on your system by default, you can install it later after reading the
package management chapter.

1.15 wc command

wc, short for word count, is an useful command which can help us to count newlines, words and bytes of a file.

$ cat hello.txt
HI that is a file.
This is the second line.
And we also have a third line.
$ wc -l hello.txt
3 hello.txt
$ wc -w hello.txt
17 hello.txt

The -l flag finds the number of lines in a file, -w counts the number of words in the file.

1.16 less command

less command helps us to view the content of any file, it allows both forward and backward movement. It does not
require to read the full file, so it helps to read faster any big text file.

$ less ~/.bash_history

In this example, we are reading the file ~/.bash_history.

1.17 echo command

echo command echoes any given string to the display.

$ echo "Hello"
Hello

6 Chapter 1. Shell commands


Linux command line for you and me Documentation, Release 0.1

1.18 Redirecting the command output

In Linux shells, we can redirect the command output to a file, or as input to another command. The pipe operator | is
the most common way to do so. Using this we can now count the number of directories in the root (/ ) directory very
easily.
$ ls /
bin boot dev etc home lib lib64 lost+found media mnt opt proc root run
˓→sbin srv sys tmp usr var
$ ls / | wc -w
20

The | is known as pipe. To know more about this, watch this video.

1.19 Using > to redirect output to a file

We can use > to redirect the output of one command to a file, if the file exists this will remove the old content and only
keep the input. We can use >> to append to a file, means it will keep all the old content, and it will add the new input
to the end of the file.
$ ls / > details.txt
$ cat details.txt
bin
boot
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
$ ls /usr/ > details.txt
$ cat details.txt
bin
games
include
lib
lib64
libexec
local
sbin
share
src
tmp
(continues on next page)

1.18. Redirecting the command output 7


Linux command line for you and me Documentation, Release 0.1

(continued from previous page)


$ ls -l /tmp/ >> details.txt
$ cat details.txt
bin
games
include
lib
lib64
libexec
local
sbin
share
src
tmp
total 776
-rwxrwxr-x. 1 fedora fedora 34 Jun 24 07:56 helol.py
-rw-------. 1 fedora fedora 784756 Jun 23 10:49 tmp3lDEho

1.20 Moving around in the command line

There are key shortcuts available in Bash which will help you to move around faster. They are by the way very similar
to the standard emacs keybindings, a number of key combinations that you will discover in many places and therefore
are very handy to memorize and internalize. The following table is a good starting point.

Key combination Action


Ctrl + A Move to the beginning of the line
Ctrl + E Move to the end of the line
Alt + B Move to the previous word
Alt + F Move to the next word
Ctrl + U Cuts to the beginning of the line
Ctrl + K Cuts to the end of the line
Ctrl + W Cuts the previous word
Ctrl + P Browse previously entered commands
Ctrl + R Reverse search for previously entered commands
Ctrl + Y Pastes the text in buffer

1.21 man pages

man shows the system’s manual pages. This is the command we use to view the help document (manual page) for
any command. The man pages are organized based on sections, and if the same command is found in many different
sections, only the first one is shown.
The general syntax is man section command. Example man 7 signal.
You can know about different sections below. Press q to quit the program.

1 Executable programs or shell commands


2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
4 Special files (usually found in /dev)
5 File formats and conventions eg /etc/passwd
(continues on next page)

8 Chapter 1. Shell commands


Linux command line for you and me Documentation, Release 0.1

(continued from previous page)


6 Games
7 Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)
8 System administration commands (usually only for root)
9 Kernel routines [Non standard]

1.22 Counting files in a directory

Normally ls commands shows all the files and directories in multiple column. But if you pipe the output to any another
command, then it prints one name in a line. We can combine that with wc -l to count the number of files in a directory.

ls | wc -l
73

1.23 Editing longer commands

If you are typing a long command or something multi-line, then you can type Ctrl-x-e, press Control button, and then
x and then e key. This will open up a temporary editor using the $EDITOR.

1.22. Counting files in a directory 9


Linux command line for you and me Documentation, Release 0.1

10 Chapter 1. Shell commands


CHAPTER 2

File system

Now you know a few really basic, Linux commands. Before we can learn anything else, we should look into how files
and directories are structured inside a Linux system.

2.1 FHS

$ ls /
bin boot dev etc home lib lib64 lost+found mc media mnt opt output proc
˓→root run sbin srv sys tmp usr var

/ is the root directory of your file system. It’s under this directory, that all the other files and directories reside. There’s a
Filesystem Hierarchy Standard(FHS), which talks about these different directories, and what kinds of files are located
in which directory.

11
Linux command line for you and me Documentation, Release 0.1

12 Chapter 2. File system

You might also like