Merge pull request #15 from codophobia/linux_basics

initial commit for linux basics module
This commit is contained in:
kalyan 2020-11-17 14:41:46 +05:30 committed by GitHub
commit ec96d831a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
93 changed files with 1248 additions and 0 deletions

View File

@ -0,0 +1,459 @@
# Command Line Basics
## What is a command ?
A command is a program that tells the operating system to perform
specific work. Programs are stored as files in linux. Therefore, a
command is also a file which is stored somewhere on the disk.
Commands may also take additional arguments as input from the user.
These arguments are called command line arguments. Knowing how to use
the commands is important and there are many ways to get help in Linux,
especially for commands. Almost every command will have some form of
documentation, most commands will have a command-line argument -h or
\--help that will display a reasonable amount of documentation. But the
most popular documentation system in Linux is called man pages - short
for manual pages.
Using \--help to show the documentation for ls command.
![](/images/linux/commands/image19.png)
## File System Organization
The linux file system has a hierarchical (or tree-like) structure with
its highest level directory called root ( denoted by / ). Directories
present inside the root directory stores file related to the system.
These directories in turn can either store system files or application
files or user related files.
![](/images/linux/commands/image17.png)
bin | The executable program of most commonly used commands reside in bin directory
sbin | This directory contains programs used for system administration.
home | This directory contains user related files and directories.
lib | This directory contains all the library files
etc | This directory contains all the system configuration files
proc | This directory contains files related to the running processes on the system
dev | This directory contains files related to devices on the system
mnt | This directory contains files related to mounted devices on the system
tmp | This directory is used to store temporary files on the system
usr | This directory is used to store application programs on the system
## Commands for Navigating the File System
There are three basic commands which are used frequently to navigate the
file system:
- ls
- pwd
- cd
We will now try to understand what each command does and how to use
these commands. You should also practice the given examples on the
online bash shell.
### pwd (print working directory)
At any given moment of time, we will be standing in a certain directory.
To get the name of the directory in which we are standing, we can use
the pwd command in linux.
![](/images/linux/commands/image2.png)
We will now use the cd command to move to a different directory and then
print the working directory.
![](/images/linux/commands/image20.png)
### cd (change directory)
The cd command can be used to change the working directory. Using the
command, you can move from one directory to another.
In the below example, we are initially in the root directory. we have
then used the cd command to change the directory.
![](/images/linux/commands/image3.png)
### ls (list files and directories)**
The ls command is used to list the contents of a directory. It will list
down all the files and folders present in the given directory.
If we just type ls in the shell, it will list all the files and
directories present in the current directory.
![](/images/linux/commands/image7.png)
We can also provide the directory name as argument to ls command. It
will then list all the files and directories inside the given directory.
![](/images/linux/commands/image4.png)
## Commands for Manipulating Files
There are four basic commands which are used frequently to manipulate
files:
- touch
- mkdir
- cp
- mv
- rm
We will now try to understand what each command does and how to use
these commands. You should also practice the given examples on the
online bash shell.
### touch (create new file)
The touch command can be used to create an empty new file.
This command is very useful for many other purposes but we will discuss
the simplest use case of creating a new file.
General syntax of using touch command
```
touch <file_name>
```
![](/images/linux/commands/image9.png)
### mkdir (create new directories)
The mkdir command is used to create directories.You can use ls command
to verify that the new directory is created.
General syntax of using mkdir command
```
mkdir <directory_name>
```
![](/images/linux/commands/image11.png)
### rm (delete files and directories)
The rm command can be used to delete files and directories. It is very
important to note that this command permanently deletes the files and
directories. It's almost impossible to recover these files and
directories once you have executed rm command on them successfully. Do
run this command with care.
General syntax of using rm command:
```
rm <file_name>
```
Let's try to understand the rm command with an example. We will try to
delete the file and directory we created using touch and mkdir command
respectively.
![](/images/linux/commands/image18.png)
### cp (copy files and directories)
The cp command is used to copy files and directories from one location
to another. Do note that the cp command doesn't do any change to the
original files or directories. The original files or directories and
their copy both co-exist after running cp command successfully.
General syntax of using cp command:
```
cp <source_path> <destination_path>
```
We are currently in the '/home/runner' directory. We will use the mkdir
command to create a new directory named "test_directory". We will now
try to copy the "\_test_runner.py" file to the directory we created just
now.
![](/images/linux/commands/image23.png)
Do note that nothing happened to the original "\_test_runner.py" file.
It's still there in the current directory. A new copy of it got created
inside the "test_directory".
![](/images/linux/commands/image14.png)
We can also use the cp command to copy the whole directory from one
location to another. Let's try to understand this with an example.
![](/images/linux/commands/image12.png)
We again used the mkdir command to create a new directory called
"another_directory". We then used the cp command along with an
additional argument '-r' to copy the "test_directory".
**mv (move files and directories)**
The mv command can either be used to move files or directories from one
location to another or it can be used to rename files or directories. Do
note that moving files and copying them are very different. When you
move the files or directories, the original copy is lost.
General syntax of using mv command:
```
mv <source_path> <destination_path>
```
In this example, we will use the mv command to move the
"\_test_runner.py" file to "test_directory". In this case, this file
already exists in "test_directory". The mv command will just replace it.
**Do note that the original file doesn't exist in the current directory
after mv command ran successfully.**
![](/images/linux/commands/image26.png)
We can also use the mv command to move a directory from one location to
another. In this case, we do not need to use the '-r' flag that we did
while using the cp command. Do note that the original directory will not
exist if we use mv command.
One of the important uses of the mv command is to rename files and
directories. Let's see how we can use this command for renaming.
We have first changed our location to "test_directory". We then use the
mv command to rename the ""\_test_runner.py" file to "test.py".
![](/images/linux/commands/image29.png)
## Commands for Viewing Files
There are three basic commands which are used frequently to view the
files:
- cat
- head
- tail
We will now try to understand what each command does and how to use
these commands. You should also practice the given examples on the
online bash shell.
We will create a new file called "numbers.txt" and insert numbers from 1
to 100 in this file. Each number will be in a separate line.
![](/images/linux/commands/image21.png)
Do not worry about the above command now. It's an advanced command which
is used to generate numbers. We have then used a redirection operator to
push these numbers to the file. We will be discussing I/O redirection in the
later sections.
### cat
The most simplest use of cat command is to print the contents of the file on
your output screen. This command is very useful and can be used for many
other purposes. We will study about other use cases later.
![](/images/linux/commands/image1.png)
You can try to run the above command and you will see numbers being
printed from 1 to 100 on your screen. You will need to scroll up to view
all the numbers.
### head
The head command displays the first 10 lines of the file by default. We
can include additional arguments to display as many lines as we want
from the top.
In this example, we are only able to see the first 10 lines from the
file when we use the head command.
![](/images/linux/commands/image15.png)
By default, head command will only display the first 10 lines. If we
want to specify the number of lines we want to see from start, use the
'-n' argument to provide the input.
![](/images/linux/commands/image16.png)
### tail
The tail command displays the last 10 lines of the file by default. We
can include additional arguments to display as many lines as we want
from the end of the file.
![](/images/linux/commands/image22.png)
By default, the tail command will only display the last 10 lines. If we
want to specify the number of lines we want to see from the end, use '-n'
argument to provide the input.
![](/images/linux/commands/image10.png)
In this example, we are only able to see the last 5 lines from the file
when we use the tail command with explicit -n option.
## Echo Command in Linux
The echo command is one of the simplest commands that is used in the
shell. This command is equivalent to what we have <print> in other
programming languages.
The echo command prints the given input string on the screen.
![](/images/linux/commands/image24.png)
## Text Processing Commands
In the previous section, we learned how to view the content of a file.
In many cases, we will be interested in performing the below operations:
- Print only the lines which contain a particular word(s)
- Replace a particular word with another word in a file
- Sort the lines in a particular order
There are three basic commands which are used frequently to process
texts:
- grep
- sed
- sort
We will now try to understand what each command does and how to use
these commands. You should also practice the given examples on the
online bash shell.
We will create a new file called "numbers.txt" and insert numbers from 1
to 10 in this file. Each number will be in a separate line.
![](/images/linux/commands/image8.png)
### grep
The grep command in its simplest form can be used to search particular
words in a text file. It will display all the lines in a file that
contains a particular input. The word we want to search is provided as
an input to the grep command.
General syntax of using grep command:
```
grep <word_to_search> <file_name>
```
In this example, we are trying to search for a string "1" in this file.
The grep command outputs the lines where it found this string.
![](/images/linux/commands/image5.png)
### sed
The sed command in its simplest form can be used to replace a text in a
file.
General syntax of using the sed command for replacement:
```
sed 's/<text_to_replace>/<replacement_text>/' <file_name>
```
Let's try to replace each occurrence of "1" in the file with "3" using
sed command.
![](/images/linux/commands/image31.png)
The content of the file will not change in the above
example. To do so, we have to use an extra argument '-i' so that the
changes are reflected back in the file.
### sort
The sort command can be used to sort the input provided to it as an
argument. By default, it will sort in increasing order.
Let's first see the content of the file before trying to sort it.
![](/images/linux/commands/image27.png)
Now, we will try to sort the file using the sort command. The sort
command sorts the content in lexicographical order.
![](/images/linux/commands/image32.png)
The content of the file will not change in the above
example.
## I/O Redirection
Each open file gets assigned a file descriptor. A file descriptor is an
unique identifier for open files in the system. There are always three
default files open, stdin (the keyboard), stdout (the screen), and
stderr (error messages output to the screen). These files can be
redirected.
Everything is a file in linux -
[https://unix.stackexchange.com/questions/225537/everything-is-a-file](https://unix.stackexchange.com/questions/225537/everything-is-a-file)
Till now, we have displayed all the output on the screen which is the
standard output. We can use some special operators to redirect the
output of the command to files or even to the input of other commands.
I/O redirection is a very powerful feature.
In the below example, we have used the '>' operator to redirect the
output of ls command to output.txt file.
![](/images/linux/commands/image30.png)
In the below example, we have redirected the output from echo command to
a file.
![](/images/linux/commands/image13.png)
We can also redirect the output of a command as an input to another
command. This is possible with the help of pipes.
In the below example, we have passed the output of cat command as an
input to grep command using pipe(\|) operator.
![](/images/linux/commands/image6.png)
In the below example, we have passed the output of sort command as an
input to uniq command using pipe(\|) operator. The uniq command only
prints the unique numbers from the input.
![](/images/linux/commands/image28.png)
I/O redirection -
[https://tldp.org/LDP/abs/html/io-redirection.html](https://tldp.org/LDP/abs/html/io-redirection.html)
## Applications in SRE Role
- As a SRE, you will be required to perform some general tasks on these linux servers. You will also be using the command line when you are troubleshooting issues.
- Moving from one location to another in the filesystem will require the help of ls, pwd and cd commands
- You may need to search some specific information in the log files. Grep command would be very useful here. I/O redirection will become handy if you want to store the output in a file or pass it as an input to another command.
- Tail command is very useful to view the latest data in the log file.
## Useful courses and tutorials
- [Edx linuxcourse](https://courses.edx.org/courses/course-v1:LinuxFoundationX+LFS101x+1T2020/course/) -
This video course can be very helpful in developing the basics of linux command line. This course is provided
in both free and paidmodes by edX. If you take the free course, you will not be able to access the assignments.
- [https://linuxcommand.org/lc3_learning_the_shell.php](https://linuxcommand.org/lc3_learning_the_shell.php)

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 134 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 251 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 161 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 301 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 306 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 375 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 332 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 101 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 373 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 295 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 192 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 242 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 245 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 295 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 243 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 186 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 217 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 179 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 268 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 95 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 288 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 302 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 628 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 233 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 131 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 189 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 189 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 187 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 246 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 353 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 147 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 200 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

View File

@ -0,0 +1,167 @@
# Introduction
## Pre - Reads
- Experience of working on any operating systems like Windows, Linux or Mac
- Basics of operating system
## What to expect from this training
This course is divided into three parts. In the first part, we will cover the
fundamentals of linux operating systems. We will talk about linux architecture,
linux distributions and uses of linux operating systems. We will also talk about
difference between GUI and CLI.
In the second part, we will study about some of the basic commands that are used
in linux. We will focus on commands used for navigating file system, commands used
for manipulating files, commands used for viewing files, I/O redirection etc.
In the third part, we will study about linux system administration. In this part, we
will focus on day to day tasks performed by linux admins like managing users/groups,
managing file permissions, monitoring system performance, log files etc.
In the second and third part, we will be taking examples to understand the concepts.
## What is not covered under this training
We are not covering advanced linux commands and bash scripting in this
course. We will also not be covering linux internals.
## Training Content
The following topics has been covered in this course:
- Introduction to Linux
- What are Linux Operating Systems
- Linux Distributions
- Uses of Linux Operating Systems
- Linux Architecture
- GUI vs CLI
- Command Line Basics
- Navigating File System
- Manipulating Files
- Viewing Files
- Text Processing Commands
- I/O Redirection
- Linux system administration
- User/Groups management
- Superuser in Linux
- File Permissions
- SSH Command
- Package Management
- Process Management
- Memory Management
- Daemons and Systemd
- Logs
## What are Linux operating systems
Most of us will be familiar with the windows operating system which is
used in more than 75% of the personal computers. The windows operating systems
are based on windows NT kernel. A kernel is the most important part of
an operating system which performs important functions like process
management, memory management, filesystem management etc.
Linux operating systems are based on the Linux kernel. A linux based
operating system will consist of linux kernel, GUI/CLI, system libraries
and system utilities. The Linux kernel was independently developed and
released by Linus Torvalds. The linux kernel is free and open-source -
[https://github.com/torvalds/linux](https://github.com/torvalds/linux)
History of Linux -
[https://en.wikipedia.org/wiki/History_of_Linux](https://en.wikipedia.org/wiki/History_of_Linux)
## What are popular Linux distributions
A linux distribution(distro) is an operating system that is based on
the linux kernel and a package management system. A package management
system consists of tools that helps in installing, upgrading,
configuring and removing softwares on the operating system.
Softwares are usually adopted to a distribution and are packaged in a
distro specific format. These packages are available through a distro
specific repository. Packages are installed and managed in the operating
system by a package manager.
**List of popular Linux distributions:**
- Fedora
- Ubuntu
- Debian
- Centos
- Red Hat Enterprise Linux
- Suse
- Arch Linux
| Packaging systems | Distributions | Package manager
| ---------------------- | ------------------------------------------ | -----------------
| Debian style (.deb) | Debian, Ubuntu | APT
| Red Hat style (.rpm) | Fedora, CentOS, Red Hat Enterprise Linux | YUM
## Linux Architecture
![](/images/linux/commands/image25.png)
- The Linux kernel is monolithic in nature.
- System calls are used to interact with the linux kernel space.
- Kernel code can only be executed in the kernel mode. Non-kernel code is executed in the user mode.
- Device drivers are used to communicate with the hardware devices.
## Uses of Linux Operating Systems
Operating system based on linux kernel are widely used in:
- Personal computers
- Servers
- Mobile phones - Android is based on linux operating system
- Embedded devices - watches, televisions, traffic lights etc
- Satelites
- Network devices - routers, switches etc.
## Graphical user interface (GUI) vs Command line interface (CLI)
A user interacts with a computer with the help of user interfaces. The
user interface can be either GUI or CLI.
Graphical user interface allows a user to interact with the computer
using graphics such as icons and images. When a user clicks on an icon
to open an application on a computer, he or she is actually using the
GUI. It's easy to perform tasks using GUI.
Command line interface allows a user to interact with the computer using
commands. A user types the command in a terminal and the system helps in
executing these commands. A new user with experience on GUI may find it
difficult to interact with CLI as he/she needs to be aware of the commands
to perform a particular operation.
## Shell vs Terminal
Shell is a program that takes command or a group of commands from the
users and gives them to the operating system for processing. Shell is an
example of command line interface. Bash is one of the most popular shell
programs available on linux servers. Other popular shell programs are
zsh, ksh and tcsh.
Terminal is a program that opens a window and lets you interact with the
shell. Some popular examples of terminals are gnome-terminal, xterm,
konsole etc.
Linux users do use the terms shell, terminal, prompt, console etc.
interchangeably. In simple terms, these all refer to a way of taking
commands from the user.

View File

@ -0,0 +1,622 @@
# Linux Server Administration
In this course will try to cover some of the common tasks that a linux
server administrator performs. We will first try to understand what a
particular command does and then try to understand the commands using
examples. Do keep in mind that it's very important to practice the linux
commands on your own.
## Lab Environment Setup
- Install docker on your system - [https://docs.docker.com/engine/install/](https://docs.docker.com/engine/install/)
- We will be running all the commands on Red Hat Enterprise Linux (RHEL) 8 system.
![](/images/linux/admin/image19.png)
- We will run most of the commands used in this module in the above docker container.
## Multi-User Operating Systems
An operating system is considered as multi-user if it allows multiple people/users to use a computer and not affect each other files and preferences. Linux based operating systems are multi-user in nature as it allows multiple users to access the system at the same time. A typical computer will only have one keyboard and monitor but multiple users can log in via ssh if the computer is connected to the network. We will cover more about ssh later.
As a server administrator, we are mostly concerned with the linux servers which are physically present at a very large distance from us. We can connect to these servers with the help of remote login methods like ssh.
Since linux supports multiple users, we need to have a method which can protect the users from each other. One user should not be able to access and modify files of other users
## User/Group Management in Linux
- Each user in linux has an associated user ID called UID attached to him
- Each user also has a home directory and a login shell associated with him/her
- A group is a collection of one or more users. A group makes it easier to share permissions among a group of users.
- Each group has a group ID called GID associated with it.
### id command in linux
id command can be used to find the uid and gid associated with an user.
It also lists down the groups to which the user belongs to.
The uid and gid associated with the root user is 0.
![](/images/linux/admin/image30.png)
A good way to find out the current user in linux is to use the whoami
command.
![](/images/linux/admin/image35.png)
**"root" user or superuser is the most privileged user with**
**unrestricted access to all the resources on the system. It has UID 0**
### Important files associated with users/groups
| /etc/passwd | Stores the user name, the uid, the gid, the home directory, the login shell etc |
| -------------| ---------------------------------------------------------------------------------
| /etc/shadow | Stores the password associated with the users |
| /etc/group | Stores information about different groups on the system |
![](/images/linux/admin/image23.png)
![](/images/linux/admin/image21.png)
![](/images/linux/admin/image9.png)
If you want to understand each filed discussed in the above outputs, you can go
through below links:
- [https://tldp.org/LDP/lame/LAME/linux-admin-made-easy/shadow-file-formats.html](https://tldp.org/LDP/lame/LAME/linux-admin-made-easy/shadow-file-formats.html)
- [https://tldp.org/HOWTO/User-Authentication-HOWTO/x71.html](https://tldp.org/HOWTO/User-Authentication-HOWTO/x71.html)
## Important commands for managing users
Some of the commands which are used frequently to manage users/groups
on linux are following:
- useradd - Creates a new user
- passwd - Adds or modifies passwords for a user
- usermod - Modifies attributes of an user
- userdel - Deletes an user
### useradd
The useradd command adds a new user in linux.
We will create a new user 'shivam'. We will also verify that the user
has been created by tailing the /etc/passwd file. The uid and gid are
1000 for the newly created user. The home directory assigned to the user
is /home/shivam and the login shell assigned is /bin/bash. Do note that
the user home directory and login shell can be modified later on.
![](/images/linux/admin/image41.png)
If we do not specify any value for attributes like home directory or
login shell, default values will be assigned to the user. We can also
override these default values when creating a new user.
![](/images/linux/admin/image54.png)
### passwd
The passwd command is used to create or modify passwords for a user.
In the above examples, we have not assigned any password for users
'shivam' or 'amit' while creating them.
\"!!\" in an account entry in shadow means the account of an user has
been created, but not yet given a password.
![](/images/linux/admin/image13.png)
Let's now try to create a password for user "shivam".
![](/images/linux/admin/image55.png)
Do remember the password as we will be later using examples
where it will be useful.
Also, let's change the password for the root user now. When we switch
from a normal user to root user, it will request you for a password.
Also, when you login using root user, the password will be asked.
![](/images/linux/admin/image39.png)
### usermod
The usermod command is used to modify the attributes of an user like the
home directory or the shell.
Let's try to modify the login shell of user "amit" to "/bin/bash".
![](/images/linux/admin/image17.png)
In a similar way, you can also modify many other attributes for a user.
Try 'usermod -h' for a list of attributes you can modify.
### userdel
The userdel command is used to remove a user on linux. Once we remove a
user, all the information related to that user will be removed.
Let's try to delete the user "amit". After deleting the user, you will
not find the entry for that user in "/etc/passwd" or "/etc/shadow" file.
![](/images/linux/admin/image34.png)
## Important commands for managing groups
Commands for managing groups are quite similar to the commands used for managing users. Each command is not explained in detail here as they are quite similar. You can try running these commands on your system.
| groupadd \<group_name\> | Creates a new group |
| ------------------------ | ------------------------------- |
| groupmod \<group_name\> | Modifies attributes of a group |
| groupdel \<group_name\> | Deletes a group |
| gpasswd \<group_name\> | Modifies password for group |
![](/images/linux/admin/image52.png)
We will now try to add user "shivam" to the group we have created above.
![](/images/linux/admin/image33.png)
## Becoming a Superuser in Linux
**Before running the below commands, do make sure that you have set up a
password for user "shivam" and user "root" using the passwd command
described in the above section.**
The su command can be used to switch users in linux. Let's now try to
switch to user "shivam".
![](/images/linux/admin/image37.png)
Let's now try to open the "/etc/shadow" file.
![](/images/linux/admin/image29.png)
The operating system didn't allow the user "shivam" to read the content
of the "/etc/shadow" file. This is an important file in linux which
stores the passwords of users. This file can only be accessed by root or
users who have the superuser privileges.
**The sudo command allows a** **user to run commands with the security
privileges of the root user.** Do remember that the root user has all
the privileges on a system. We can also use su command to switch to the
root user and open the above file but doing that will require the
password of the root user. An alternative way which is preferred on most
modern operating systems is to use sudo command for becoming a
superuser. Using this way, a user has to enter his/her password and they
need to be a part of the sudo group.
**How to provide superpriveleges to other users ?**
Let's first switch to the root user using su command. Do note that using
the below command will need you to enter the password for the root user.
![](/images/linux/admin/image44.png)
In case, you forgot to set a password for the root user, type "exit" and
you will be back as the root user. Now, set up a password using the
passwd command.
**The file /etc/sudoers holds the names of users permitted to invoke
sudo**. In redhat operating systems, this file is not present by
default. We will need to install sudo.
![](/images/linux/admin/image3.png)
We will discuss the yum command in detail in later sections.
Try to open the "/etc/sudoers" file on the system. The file has a lot of
information. This file stores the rules that users must follow when
running the sudo command. For example, root is allowed to run any
commands from anywhere.
![](/images/linux/admin/image8.png)
One easy way of providing root access to users is to add them to a group
which has permissions to run all the commands. "wheel" is a group in
redhat linux with such privileges.
![](/images/linux/admin/image25.png)
Let's add the user "shivam" to this group so that it also has sudo
privileges.
![](/images/linux/admin/image48.png)
Let's now switch back to user "shivam" and try to access the
"/etc/shadow" file.
![](/images/linux/admin/image56.png)
We need to use sudo before running the command since it can only be
accessed with the sudo privileges. We have already given sudo privileges
to user “shivam” by adding him to the group “wheel”.
## File Permissions in Linux
On a linux operating system, each file and directory is assigned access
permissions for the owner of the file, the members of a group of related
users and everybody else. This is to make sure that one user is not
allowed to access the files and resources of another user.
To see the permissions of a file, we can use the ls command. Let's look
at the permissions of /etc/passwd file.
![](/images/linux/admin/image40.png)
Let's go over some of the important fields in the output that are
related to file permissions.
![](/images/linux/admin/image31.jpg)
![](/images/linux/admin/image57.png)
### Chmod command in linux
The chmod command is used to modify files and directories permissions in
linux.
The chmod command accepts permissions in as a numerical argument. We can
think of permission as a series of bits with 1 representing True or
allowed and 0 representing False or not allowed.
| Permission | rwx | Binary | Decimal |
| -------------------------| ------- | ------- | --------- |
| Read, write and execute | rwx | 111 | 7 |
| Read and write | rw- | 110 | 6 |
| Read and execute | r-x | 101 | 5 |
| Read only | r-- | 100 | 4 |
| Write and execute | -wx | 011 | 3 |
| Write only | -w- | 010 | 2 |
| Execute only | --x | 001 | 1 |
| None | --- | 000 | 0 |
We will now create a new file and check the permission of the file.
![](/images/linux/admin/image15.png)
The group owner doesn't have the permission to write to this file. Let's
give the group owner or root the permission to write to it using chmod
command.
![](/images/linux/admin/image26.png)
Chmod command can be also used to change the permissions of a directory
in the similar way.
### Chown command in linux
The chown command is used to change the owner of files or
directories in linux.
Command syntax: chown \<new_owner\> \<file_name\>
![](/images/linux/admin/image6.png)
**In case, we do not have sudo privileges, we need to use sudo
command**. Let's switch to user 'shivam' and try changing the owner. We
have also changed the owner of the file to root before running the below
command.
![](/images/linux/admin/image12.png)
Chown command can also be used to change the owner of a directory in the
similar way.
### Chgrp command in linux
The chgrp command can be used to change the group ownership of files or
directories in linux. The syntax is very similar to that of chown
command.
![](/images/linux/admin/image27.png)
Chgrp command can also be used to change the owner of a directory in the
similar way.
## SSH Command
The ssh command is used for logging into the remote systems, transfer files between systems and for executing commands on a remote machine. SSH stands for secure shell and is used to provide an encrypted secured connection between two hosts over an insecure network like the internet.
Reference:
[https://www.ssh.com/ssh/command/](https://www.ssh.com/ssh/command/)
We will now discuss passwordless authentication which is secure and most
commonly used for ssh authentication.
### Passwordless Authentication Using SSH
Using this method, we can ssh into hosts without entering the password.
This method is also useful when we want some scripts to perform
ssh-related tasks.
Passwordless authentication requires the use of a public and private key pair. As the name implies, the public key can be shared with anyone but the private key should be kept private.
Lets not get into the details of how this authentication works. You can read more about it
[here](https://www.digitalocean.com/community/tutorials/understanding-the-ssh-encryption-and-connection-process)
Steps for setting up a passwordless authentication with a remote host:
1. Generating public-private key pair
**If we already have a key pair stored in \~/.ssh directory, we will not need to generate keys again.**
Install openssh package which contains all the commands related to ssh.
![](/images/linux/admin/image49.png)
Generate a key pair using the ssh-keygen command. One can choose the
default values for all prompts.
![](/images/linux/admin/image47.png)
After running the ssh-keygen command successfully, we should see two
keys present in the \~/.ssh directory. Id_rsa is the private key and
id_rsa.pub is the public key. Do note that the private key can only be
read and modified by you.
![](/images/linux/admin/image7.png)
2. Transferring the public key to the remote host
There are multiple ways to transfer the public key to the remote server.
We will look at one of the most common ways of doing it using the
ssh-id-copy command.
![](/images/linux/admin/image11.png)
Install the openssh-clients package to use ssh-id-copy command.
![](/images/linux/admin/image46.png)
Use the ssh-id-copy command to copy your public key to the remote host.
![](/images/linux/admin/image50.png)
Now, ssh into the remote host using the password authentication.
![](/images/linux/admin/image51.png)
Our public key should be there in \~/.ssh/authorized_keys now.
![](/images/linux/admin/image4.png)
\~/.ssh/authorized_key contains a list of public keys. The users
associated with these public keys have the ssh access into the remote
host.
### How to run commands on a remote host ?
General syntax: ssh \<user\>@\<hostname/hostip\> \<command\>
![](/images/linux/admin/image14.png)
### How to transfer files from one host to another host ?
General syntax: scp \<source\> \<destination\>
![](/images/linux/admin/image32.png)
## Package Management
Package management is the process of installing and managing software on
the system. We can install the packages which we require from the linux
package distributor. Different distributors use different packaging
systems.
| Packaging systems | Distributions |
| ---------------------- | ------------------------------------------ |
| Debian style (.deb) | Debian, Ubuntu |
| Red Hat style (.rpm) | Fedora, CentOS, Red Hat Enterprise Linux |
**Popular Packaging Systems in Linux**
|Command | Description |
| ----------------------------- | --------------------------------------------------- |
| yum install \<package_name\> | Installs a package on your system |
| yum update \<package_name\> | Updates a package to it's latest available version |
| yum remove \<package_name\> | Removes a package from your system |
| yum search \<keyword\> | Searches for a particular keyword |
[DNF](https://docs.fedoraproject.org/en-US/quick-docs/dnf/) is
the successor to YUM which is now used in Fedora for installing and
managing packages. DNF may replace YUM in the future on all RPM based
linux distributions.
![](/images/linux/admin/image20.png)
We did find an exact match for the keyword httpd when we searched using
yum search command. Let's now install the httpd package.
![](/images/linux/admin/image28.png)
After httpd is installed, we will use the yum remove command to remove
httpd package.
![](/images/linux/admin/image43.png)
## Process Management
In this section, we will study about some useful commands that can be
used to monitor the processes on linux systems.
### ps (process status)
The ps command is used to know the information of a process or list of
processes.
![](/images/linux/admin/image24.png)
If you get an error "ps command not found" while running ps command, do
install **procps** package.
ps without any arguments is not very useful. Let's try to list all the
processes on the system by using the below command.
Reference:
[https://unix.stackexchange.com/questions/106847/what-does-aux-mean-in-ps-aux](https://unix.stackexchange.com/questions/106847/what-does-aux-mean-in-ps-aux)
![](/images/linux/admin/image42.png)
We can use an additional argument with ps command to list the
information about the process with a specific process ID.
![](/images/linux/admin/image2.png)
We can use grep in combination with ps command to list only specific
processes.
![](/images/linux/admin/image1.png)
### top
The top command is used to show information about linux processes
running on the system in real time. It also shows a summary of the
system information.
![](/images/linux/admin/image53.png)
For each process, top lists down the process ID, owner, priority, state,
cpu utilization, memory utilization and much more information. It also
lists down the memory utilization and cpu utilization of the system as a
whole along with system uptime and cpu load average.
## Memory Management
In this section, we will study about some useful commands that can be
used to view information about the system memory.
### free
The free command is used to display the memory usage of the system. The
command displays the total free and used space available in the RAM
along with space occupied by the caches/buffers.
![](/images/linux/admin/image22.png)
free command by default shows the memory usage in kilobytes. We can use
an additional argument to get the data in human-readable format.
![](/images/linux/admin/image5.png)
### vmstat
The vmstat command can be used to display the memory usage along with
additional information about io and cpu usage.
![](/images/linux/admin/image38.png)
## Checking Disk Space in Linux
In this section, we will study about some useful commands that can be
used to view disk space on linux.
### df (disk free)
The df command is used to display the free and available space for each
mounted file system.
![](/images/linux/admin/image36.png)
### du (disk usage)
The du command is used to display disk usage of files and directories on
the system.
![](/images/linux/admin/image10.png)
The below command can be used to display the top 5 largest directories
in the root directory.
![](/images/linux/admin/image18.png)
## Daemons
A computer program that runs as a background process is called a daemon.
Traditionally, the name of daemon processes ended with d - sshd, httpd
etc. We cannot interact with a daemon process as they run in the
background.
Services and daemons are used interchangeably most of the time.
## Systemd
Systemd is a system and service manager for Linux operating systems.
Systemd units are the building blocks of systemd. These units are
represented by unit configuration files.
The below examples shows the unit configuration files available at
/usr/lib/systemd/system which are distributed by installed RPM packages.
We are more interested in the configuration file that ends with service
as these are service units.
![](/images/linux/admin/image16.png)
### Managing System Services
Service units end with .service file extension. Systemctl command can be
used to start/stop/restart the services managed by systemd.
| Command | Description |
| ------------------------------- | -------------------------------------- |
| systemctl start name.service | Starts a service |
| systemctl stop name.service | Stops a service |
| systemctl restart name.service | Restarts a service |
| systemctl status name.service | Check the status of a service |
| systemctl reload name.service | Reload the configuration of a service |
## Logs
In this section, we will talk about some important files and directories
which can be very useful for viewing system logs and applications logs
in linux. These logs can be very useful when you are troubleshooting on
the system.
![](/images/linux/admin/image58.png)
## Applications in SRE Role
- Different users will have different permissions depending on their
roles. We will also not want everyone in the company to access our
servers for security reasons. Users permissions can be restricted
with chown, chmod and chgrp commands.
- SSH is one of the most frequently used commands for a SRE. Logging
into servers and troubleshooting along with performing basic
administration tasks will only be possible if we are able to login
into the server.
- What if we want to run an apache server or nginx on a server ? We
will first install it using the package manager. Package
management commands become important here.
- Managing services on servers is another critical responsibility of a
SRE. Systemd related commands can help in troubleshooting issues.
If a service goes down, we can start it using systemctl start
command. We can also stop a service in case it is not needed.
- Monitoring is another core responsibility of a SRE. Memory and CPU
are two important system level metrics which should be monitored.
Commands like top and free are quite helpful here.
- If a service is throwing an error, how do we find out the root cause
of the error ? We will certainly need to check logs to find out
the whole stack trace of the error. The log file will also tell us
the number of times the error has occurred along with time when it
started.
## Useful courses and tutorials
- Edx Red Hat Enterprise Linux Course - [https://courses.edx.org/courses/course-v1:RedHat+RH066x+2T2017/course/](https://courses.edx.org/courses/course-v1:RedHat+RH066x+2T2017/course/)