Diferencia entre revisiones de «Basic Administration»

De Wiki de Sistemas Operativos
Saltar a: navegación, buscar
(cat: remove <>)
(Exercise)
 
(No se muestran 8 ediciones intermedias del mismo usuario)
Línea 418: Línea 418:
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
cat file1 file2 > file3 file3
+
cat file1 file2 > file3
</syntaxhighlight>
 
 
 
* Edit files:
 
 
 
<syntaxhighlight lang="bash">
 
cat
 
 
 
hello
 
what's up
 
 
 
cat > file4
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Línea 487: Línea 476:
  
 
It will show us the count of each file and the total.
 
It will show us the count of each file and the total.
 +
 +
== find ==
 +
 +
To search for files we can use the ''find'' command.
 +
 +
For example, to search for the file ''python3'' in the whole directory tree (from the root) we can use the command:
 +
 +
<syntaxhighlight lang="bash">
 +
find / -name python3
 +
</syntaxhighlight>
 +
 +
We can restrict the search to files with ''-type f''
 +
 +
<syntaxhighlight lang="bash">
 +
find / -type f -name python3
 +
</syntaxhighlight>
 +
 +
or for directories with ''-type d''
 +
 +
== Pipe (|) ==
 +
 +
The pipeline will serve us to connect several commands at the same time, let's see
 +
some examples, which is always better understood:
 +
 +
<syntaxhighlight lang="bash">
 +
cat file2.txt | grep test
 +
grep -nr test file2.txt | wc -l
 +
</syntaxhighlight>
 +
 +
In the first example, we display the contents of file2.txt, and this output is passed to the grep command.
 +
We pass it to the grep command, which searches for lines containing test.
 +
 +
In the second example, we look for all lines containing the word test, and this output is passed to the w
 +
output is passed to the wc command, which counts the lines in the output of the grep command.
 +
grep command.
 +
 +
== Redirections ==
 +
 +
Let's look at a few concepts before we continue:
 +
 +
* Standard input: represents the data that an application needs to run, such as a data file or information entered from the terminal and is represented in the terminal as type 0.
 +
* Standard output: it is the way that the applications use to show you information, there we can see the progress or simply the messages that the application wants to give you at a certain moment and it is represented in the terminal as type 1.
 +
* Standard error: it is the way in which the programs inform you about the problems that can be found at the moment of the execution and it is represented in the terminal as type 2.
 +
 +
Redirections are used to move information from one type to another.
 +
 +
We have already seen previously the use of > and >>, and also the |, let's see some more:
 +
 +
* command < file: Takes the file input.
 +
* command 2> file: Sends the error output of command to file
 +
* command 2>&1: Send the error output to the standard output
 +
* command &> file: Send the standard and error output to file; equivalent to command > file 2>&1
 +
 +
= Step 4: Users and groups =
 +
 +
== whoami ==
 +
 +
Used to get the current user.
 +
 +
<syntaxhighlight lang="bash">
 +
whoami
 +
</syntaxhighlight>
 +
 +
Most likely we will get ubuntu.
 +
 +
 +
== who ==
 +
 +
This is to find out who is currently on the machine logged in.
 +
 +
<syntaxhighlight lang="bash">
 +
who
 +
</syntaxhighlight>
 +
 +
It's probably just us:
 +
 +
<syntaxhighlight lang="bash">
 +
ubuntu pts/0 2019-08-28 13:51 (192.168.122.1)
 +
</syntaxhighlight>
 +
 +
 +
== su and sudo ==
 +
 +
su (substitute user): used to change user. For example:
 +
 +
<syntaxhighlight lang="bash">
 +
su root
 +
</syntaxhighlight>
 +
 +
Here you will be prompted for the password of the user you want to change to.
 +
 +
sudo (super user do): not all distributions integrate it, because it's less secure than su.
 +
secure than su, this command allows us to execute a command as another user,
 +
by default, if we do not give user, it will do it with the root user. If sudo
 +
password, it asks for the user's password, since the user is the one who has permissions to use sudo or not.
 +
has permissions to use sudo or not.
 +
 +
<syntaxhighlight lang="bash">
 +
sudo ls -a /root
 +
</syntaxhighlight>
 +
 +
If we had for example a user 'practica' and we wanted to run a
 +
command on his behalf:
 +
 +
<syntaxhighlight lang="bash">
 +
sudo -u practica ls
 +
</syntaxhighlight>
 +
 +
As root I can add users so they can sudo like this:
 +
 +
<syntaxhighlight lang="bash">
 +
adduser sudo user
 +
</syntaxhighlight>
 +
 +
This makes the user ''user'' able to do sudo.
 +
 +
 +
== adduser and useradd ==
 +
 +
Both commands are used to create a new user, but they have their differences.
 +
 +
useradd is a system command, and adduser is a script written in perl that uses the useradd command.
 +
uses the useradd command. The difference at first glance is that adduser will
 +
create the /home/user directory and useradd needs the -m option to do the same thing.
 +
option to do the same. You can use whichever one you want, although useradd is more recommended,
 +
as it will work the same on all distributions.
 +
 +
Let's add a user practica with its directory in /home (note that we need super user permissions to do this).
 +
we need super user permissions to perform this task):
 +
 +
<syntaxhighlight lang="bash">
 +
sudo useradd -m practice
 +
</syntaxhighlight>
 +
 +
Let's check that the home directory is created and that the user practica
 +
is working:
 +
 +
<syntaxhighlight lang="bash">
 +
ls /home # we will see the practica directory.
 +
sudo -u practica whoami # it will tell us that we are the user practica
 +
</syntaxhighlight>
 +
 +
 +
== passwd ==
 +
 +
It is used to modify the password of a user, for example, we are going to modify the password of the user we have previously created.
 +
the password of the user we have previously created:
 +
 +
<syntaxhighlight lang="bash">
 +
sudo passwd practice
 +
</syntaxhighlight>
 +
 +
It will ask us twice for the password, and we will have it updated. Let's go
 +
now try the su command to change the user:
 +
 +
<syntaxhighlight lang="bash">
 +
su practice
 +
whoami
 +
</syntaxhighlight>
 +
 +
We will see that we have changed the user, to close this user, we type
 +
exit' or press 'CTLR + D'.
 +
 +
 +
== deluser and userdel ==
 +
 +
Both are used to delete a user, similar to adduser and useradd, userdel is the command and deluser is the script.
 +
command and deluser is the script.
 +
 +
Let's delete the user we have created, including deleting his /home.
 +
(-r option):
 +
 +
<syntaxhighlight lang="bash">
 +
userdel -r practice
 +
</syntaxhighlight>
 +
 +
Let's check that we no longer have user or /home:
 +
 +
<syntaxhighlight lang="bash">
 +
ls /home
 +
sudo -u practice whoami
 +
</syntaxhighlight>
 +
 +
 +
== addgroup and groupadd ==
 +
 +
Both are used to create a group. Groups are used to group users
 +
and that they have the same permissions. For example, when we install some
 +
tools like docker, it creates the docker group automatically so that we can easily add a user to the group.
 +
so that we can easily add a user to the group, and this one has the permissions to work with that tool.
 +
permissions to work with that tool.
 +
 +
Let's create a new group, let's call it guest:
 +
 +
<syntaxhighlight lang="bash">
 +
sudo groupadd guest
 +
</syntaxhighlight>
 +
 +
 +
== usermod and groups ==
 +
 +
usermod is used to assign a group to a user.
 +
 +
groups is used to view the groups a user belongs to.
 +
 +
Let's add a practica user, we will add him to the guest group and we will see the groups he belongs to
 +
we will see the groups to which he belongs:
 +
 +
<syntaxhighlight lang="bash">
 +
sudo useradd -m practica
 +
sudo usermod -G guest practica
 +
groups practica
 +
</syntaxhighlight>
 +
 +
We will see that the user practica belongs to the guest group.
 +
 +
 +
== delgroup and groupdel ==
 +
 +
Both are used to delete a group. Let's delete the group we created
 +
previously:
 +
 +
<syntaxhighlight lang="bash">
 +
sudo groupdel guest
 +
</syntaxhighlight>
 +
 +
We can notice now that the user practica does not belong to the guest group:
 +
 +
<syntaxhighlight lang="bash">
 +
groups practica
 +
</syntaxhighlight>
 +
 +
 +
= Step 5: Permissions =
 +
 +
== Nomenclature rwx and numeric ==
 +
 +
If we do an ls -l, the first string that appears is the permissions of the file (-rwxrw).
 +
file (-rwxrw-r--), we can divide it in 4 elements:
 +
 +
- (rwx) (rw-) (r--)
 +
| | | |
 +
type owner group others
 +
 +
* type: the type of the file, it can be d (directory), l (symbolic link), - (normal file)
 +
* owner: owner permissions
 +
* group: group permissions
 +
* others: permissions for other users
 +
 +
Meaning of permissions:
 +
 +
* r: read permissions
 +
* w: write permissions
 +
* x: execute permissions
 +
 +
Finally, there is an abbreviated mode for these permissions which is used with
 +
numbers.
 +
 +
rwx
 +
001 in binary is 1 in decimal. Execute permissions
 +
010 in binary is 2 in decimal. Write permissions
 +
100 in binary is 4 in decimal. Read permissions
 +
 +
The sum of the permissions, gives us the numerical value. Let's see an example:
 +
rwx rw- r--
 +
111 110 100
 +
7 6 4
 +
 +
== chmod ==
 +
 +
Allows you to change the access permissions to a file or directory. We can give
 +
permissions in two different ways.
 +
 +
 +
=== character mode ===
 +
 +
Let's first see some details:
 +
 +
User classes:
 +
 +
* owner (u)
 +
* group (g)
 +
* others (o)
 +
* all (a)
 +
 +
Modifier:
 +
 +
* add (+)
 +
* delete (-)
 +
* overwrite (=)
 +
 +
To give permissions we would have to select the class of user the modifier
 +
and the permissions, let's see an example:
 +
 +
<syntaxhighlight lang="bash">
 +
chmod u+x file # add execute permissions to the user
 +
chmod go-w file # remove write permissions to the group and other users
 +
chmod u=rwx,go=r file # give the user all permissions, and the group and others only read permissions
 +
</syntaxhighlight>
 +
 +
 +
=== Octal mode ===
 +
 +
With octacl mode, it is as if we always use modify overwrite,
 +
let's take some examples:
 +
 +
<syntaxhighlight lang="bash">
 +
chmod 744 file # To the user we give all permissions, and to the group and others we only give read permissions.
 +
chmod 777 file # Everyone has permissions for everything
 +
chmod 600 file # only the owner has read and write permissions
 +
</syntaxhighlight>
 +
 +
 +
=== Examples ===
 +
 +
Let's see how permissions work with some examples. We are going to create
 +
a test file and we are going to remove the write permissions to our user, and we are going to give it
 +
user, and we are going to give write permissions to others:
 +
 +
<syntaxhighlight lang="bash">
 +
touch tests
 +
ls -l tests # this way we will see that by default the permissions are rw-rw-r--
 +
chmod u-w,o+w tests
 +
ls -l tests # now the permissions should be -w-rw-rw-
 +
</syntaxhighlight>
 +
 +
Once the permissions are given, let's see that with our user we can read but not
 +
write:
 +
 +
<syntaxhighlight lang="bash">
 +
cat tests
 +
echo "testing" > tests
 +
exit
 +
</syntaxhighlight>
 +
 +
And now let's see that the user tests, can read and write:
 +
 +
<syntaxhighlight lang="bash">
 +
your practice
 +
cat tests
 +
echo "testing" > tests
 +
</syntaxhighlight>
 +
 +
 +
== chown ==
 +
 +
Allows you to change the owner of a file or directory.
 +
 +
Let's for example now pass the file tests to the user practica:
 +
 +
<syntaxhighlight lang="bash">
 +
sudo chown practice tests
 +
ls -l tests
 +
</syntaxhighlight>
 +
 +
We will see that the current user is now tests, although we see that the ls shows
 +
the following:
 +
 +
<syntaxhighlight lang="bash">
 +
-r--rw-rw- 1 practice ubuntu 9 Aug 29 09:34 tests
 +
</syntaxhighlight>
 +
 +
There is one thing to note, and that is that files and directories are assigned a user and a group by default.
 +
a user and a group, by default the same user is used as group
 +
when we create a new file or directory. If we want that when we change
 +
the owner, we can also change the group of the file, we can do it in the following way
 +
following way:
 +
 +
<syntaxhighlight lang="bash">
 +
sudo chown practice:practice tests
 +
</syntaxhighlight>
 +
 +
Once this is done, let's now test that the permissions are still working
 +
as before, now with the ubuntu user, we should be able to read and write to the file tests.
 +
the file tests, and with the user practica, only read, this is so because
 +
now the owner is practica:
 +
 +
<syntaxhighlight lang="bash">
 +
# tests ubuntu user
 +
cat tests
 +
echo test >> tests
 +
# tests user practica
 +
your practice
 +
cat tests
 +
echo "testing" > tests
 +
exit
 +
</syntaxhighlight>
 +
 +
 +
= Step 6: Processes =
 +
 +
 +
== ps ==
 +
 +
Used to see what processes are running on the system, let's see an example:
 +
 +
<syntaxhighlight lang="bash">
 +
ps aux # with the aux option we will show all the processes in the system.
 +
</syntaxhighlight>
 +
 +
Each line is a process, and each process displays its PID, user, amount of
 +
memory and cpu used, command and other details.
 +
 +
<syntaxhighlight lang="bash">
 +
man kill & # adding the & to the end of a command causes it to run in the background, so it will stay open.
 +
</syntaxhighlight>
 +
 +
The output of this command will show us the PID of the process we just executed, let's check it.
 +
run, let's check it:
 +
 +
<syntaxhighlight lang="bash">
 +
ps aux | grep "man kill"
 +
</syntaxhighlight>
 +
 +
We will see that the PID matches.
 +
 +
== kill ==
 +
 +
This command will be used to kill a process. kill is used followed by a signal to send the process followed by one or more PIDs.
 +
signal to send to the process followed by one or more PIDs. to see the available signals, we can list them: == == == This command is used to kill a process.
 +
to see the available signals, we can list them:
 +
 +
<syntaxhighlight lang="bash">
 +
kill -l
 +
</syntaxhighlight>
 +
 +
The most commonly used are SIGTERM and SIGKILL, the first one tries to end the process in an unabrupt way, the second one
 +
the first one tries to terminate the process in a less abrupt way, and the second one is usually used when the process
 +
does not heed this first signal. We are now going to terminate the process we created
 +
before, if we don't remember the PID, let's look it up again:
 +
 +
<syntaxhighlight lang="bash">
 +
ps aux | grep "man kill"
 +
</syntaxhighlight>
 +
 +
We send signal to terminate the process and check that it has finished:
 +
 +
<syntaxhighlight lang="bash">
 +
kill -SIGTERM 8470
 +
ps aux | grep "man kill"
 +
</syntaxhighlight>
 +
 +
We can also notice that in the signal listing, there are some numbers, we can
 +
use those numbers instead of the words, for example:
 +
 +
<syntaxhighlight lang="bash">
 +
man kill &
 +
kill -15 PID_PREVIOUS_COMAND
 +
</syntaxhighlight>
 +
 +
We check that the process has finished correctly:
 +
 +
<syntaxhighlight lang="bash">
 +
ps aux | grep "man kill"
 +
</syntaxhighlight>
 +
 +
 +
= Step 7: CPU, RAM and Disk status =
 +
 +
== top ==
 +
 +
This command will help us to see the list of processes and the status of CPU and memory.
 +
memory.
 +
 +
Let's try the command and observe the output in detail:
 +
 +
<syntaxhighlight lang="bash">
 +
top
 +
</syntaxhighlight>
 +
 +
To exit, we press q.
 +
 +
 +
== df and du ==
 +
 +
df (disk free) and du (disk usage). Both are utilities for displaying disk usage.
 +
the disks.
 +
 +
With df we will show the space information on each mounted device:
 +
 +
<syntaxhighlight lang="bash">
 +
df
 +
</syntaxhighlight>
 +
 +
These commands that show us the size of the files, almost always have
 +
an option to show it in a more readable format (-h)
 +
 +
<syntaxhighlight lang="bash">
 +
df -h
 +
</syntaxhighlight>
 +
 +
Much better.
 +
 +
Now let's look at the du command, which will show the size of a file or directory and its subdirectories.
 +
directory and its subdirectories, let's not forget the -h:
 +
 +
<syntaxhighlight lang="bash">
 +
du -h /home/ubuntu
 +
du -h /home/ubuntu/tests
 +
</syntaxhighlight>
 +
 +
If we only want to know the total of a folder and we are not interested in its subfolders, we can use
 +
subfolders, we can use the -s option:
 +
 +
<syntaxhighlight lang="bash">
 +
du -sh /home/ubuntu
 +
</syntaxhighlight>
 +
 +
 +
== free ==
 +
 +
Used to view the memory status, as always, -h option:
 +
 +
<syntaxhighlight lang="bash">
 +
free -h
 +
</syntaxhighlight>
 +
 +
 +
== lsblk ==
 +
 +
Shows us the information of all block devices (hard disks,
 +
pendrivers, CD_ROM, SSD, ...).
 +
 +
<syntaxhighlight lang="bash">
 +
lsblk
 +
</syntaxhighlight>
 +
 +
In the following practice this command will be used more thoroughly, as we will
 +
we will be working with devices.
 +
 +
 +
= Step 8: Software package management and repositories =
 +
 +
Linux systems include, in addition to the basic operating system tools, software repositories that can be optionally installed by the system administrator. The manufacturers of Linux distributions include ready-to-install software packages that are integrated with the system.
 +
 +
A package includes the software necessary for a certain application to function properly, as well as its dependency packages.
 +
 +
In Ubuntu, the software package and repository management tool is called '''apt''''.
 +
 +
=== List of software package repositories ===
 +
 +
In Ubuntu the repository list is located in the /etc/apt/sources.list file. We can check which repositories we have added:
 +
 +
<syntaxhighlight lang="bash">
 +
cat /etc/apt/sources.list
 +
</syntaxhighlight>
 +
 +
=== Updating the software package listing ===
 +
 +
To update the package listing we will use the command:
 +
 +
<syntaxhighlight lang="bash">
 +
sudo apt update
 +
</syntaxhighlight>
 +
 +
=== Installing a new package ===
 +
 +
To install a new package, we will use the install command, for example, let's install tree, similar to ls but shows the directory tree:
 +
 +
<syntaxhighlight lang="bash">
 +
sudo apt install tree
 +
</syntaxhighlight>
 +
 +
=== Uninstallation of a package ===
 +
 +
We will use the purge command or the remove command, purge removes everything, and remove keeps the configuration if the package had it. Let's remove the previously installed package:
 +
 +
<syntaxhighlight lang="bash">
 +
sudo apt purge tree
 +
</syntaxhighlight>
 +
 +
= Step 9: Remote administration with ssh =
 +
 +
''ssh'' (Secure SHell) allows you to remotely administer a system from the shell. In order to access the virtual machine via ''[[ssh]]'', the ''openssh-server'' package must be installed.
 +
 +
sudo apt-get install openssh-server
 +
 +
Once installed, we need to query the IP address of the virtual machine
 +
 +
ip address
 +
 +
From the hypervisor we can access by ''ssh'' to the virtual machine with the command:
 +
 +
ssh user@ip
 +
 +
Being ''user'' the user name with which you access the virtual machine and the ''ip'' address shown by the command ''ip address''.
 +
 +
For example, if the selected user is ''ubuntu'' and the IP is 192.168.122.123, then the invocation to ''ssh'' is the following:
 +
 +
ssh ubuntu@192.168.122.123
 +
 +
In case openssh-server is not installed on the virtual machine, the connection may be rejected:
 +
ssh: connect to host 192.168.122.123 port 22: Connection refused
 +
 +
To solve the problem it is necessary to install OpenSSH client on the virtual machine
 +
 +
sudo apt install openssh-client
 +
 +
And then install OpenSSH server
 +
 +
sudo apt install openssh-server
 +
 +
 +
= Exercise =
 +
 +
* Create a file 'x.txt' with this content (use echo or cat!)
 +
 +
hello world
 +
1234567890
 +
bye bye
 +
test test
 +
 +
* create a folder x that has a subfolder y
 +
* move 'x.txt' to subfolder y
 +
* make a copy of 'x.txt' to folder 'x' whose name is 'x.txt.backup'
 +
* make a copy of the entire 'x' folder and name it 'x.backup'
 +
* remove the 'x.backup' folder
 +
* find all folder in your filesystem whose name is 'bin'
 +
* count the number of folders whose name is 'bin' in your filesystem
 +
* check if there is a user 'practica' in /etc/passwd
 +
* check for all files in your home folder with read+write permission for the group and store it in a file called "group_permissions.txt"

Revisión actual del 17:02 16 nov 2021

In this practice we will learn how to use the command interpreter (also known as command line) and to learn basic notions of Linux system administration.

Step 0: Brief introduction to the Linux system

Basic structure of the file system

In a Linux system, all folders and files in the file system start at the root folder which is represented by the /.

/
├── bin
├── usr
│ ├─── local
│ ├─── bin
│ └── ..
├── dev
│ ├─── sda
│ ├─── sda1
│ └── ..
├─── home
│ ├─── practice
│ │ ├─── topic1.pdf
│ │ ├─── bulletin1.pdf
│ │ └── ..
│ ├─── professor
│ └── ..
└─── etc
    ├─── firefox
    ├─── libvirt
    ├── ..
    └── ..

As you can see, the file system follows a tree structure.

Basic notions

General command format:

command [-options] [arguments]

In Linux, use of lowercase and uppercase in file names is meaningful.

Folders . and ..

Every folder on a Linux system has two pseudofolders, the . and the ..:

  • The pseudofolder . refers to the parent folder that contains this folder.
  • The pseudofolder .. refers to the current folder, it is therefore a self-reference.

In the case of the root folder, the pseudofolder . and .. refer to the root folder itself, it is therefore an exception.

Current working directory and the command cd

Each command interpreter has a current working directory (current working directory). The current working folder can be modified with the command cd (change directory, in English).

For example:

$ cd ..

It would place us in the parent folder, using a relative path.

If we retype from /home " cd .. " takes us to /$

To place us in the root, we can use an absolute path:

$ cd /

In case we want to get back to the user folder, it would be enough to invoke cd without further or cd ~

$ cd

VERY IMPORTANT: cd.. is not the same as cd .., the space after the cd is required.

Absolute paths, relative paths

Absolute or relative paths are used to refer to a file or folder.

Absolute paths always start with /, so they take the root folder (/) as a reference point. For example, an absolute path to the test folder that is stored in the user's ubuntu folder is '/home/ubuntu/test/.

The relative path takes the current working folder as reference. To find out the current working folder we have the command pwd.

$ pwd
/home/ubuntu

Based on the current working folder, using the pseudofolder .', relative paths can be constructed. For example, to reference a file file.txt in the temporary folder, we can use the relative path ../../tmp/file.txt.

Although it is probably more convenient to use an absolute path in this case, which would be /tmp/file.txt.

Command interpreter

The shell is a textual application launcher that uses the keyboard as an input device. Using the keyboard, you type the name of the program you want to launch and when you press the enter key, the shell executes the program. By default, the command interpreter used by Ubuntu is bash.

man: see the man page of a command

The man command will be useful for viewing man pages.

Its basic structure is:

man <command>

Although it is also used with the option -a of All, which also displays entries beginning with .:

man -a <command>

This program takes as input the name of the command you want to query its man page, for example:

man ls

It gives us man page information for the ls command.

To exit the manual page, press the q key (the first letter of the word quit, in English).

quit = exit

Usually, programs offer help options, such as --help.

man --help

The compact version can also be used when specifying options:

man -h

In general, all commands usually offer a helper option.

Step 1: Basic operations with the file system

ls

Lists the files and folders. If nothing is specified, displays the files and folders contained in the current working folder.

ls

If a path is specified, displays the folders and files contained in that path, for example:

$ ls /
bin boot data dev etc home initrd.img initrd.img.old lib lib64 lost+found media mnt opt proc root run sbin snap srv sys tmp usr var vmlinuz vmlinuz.old

When using / as the absolute path, it displays the contents of the root folder.

The most commonly used options of this program are usually:

  • -a: shows hidden files and folders. In Linux, any file or folder whose name starts with . is considered hidden. This also includes the pseudofolders . and . in the list.
  • -l: displays in list and provides data such as last modified date, owner, group, size in bytes and name:
$ ls -a

. ..  .bash_history .bash_logout .bashrc .bashrc .profile .ssh

Both options can be combined:

$ ls -la /

total 92
drwxr-xr-x 24 root root root 4096 Aug 28 09:50 .
drwxr-xr-x 24 root root root 4096 Aug 28 09:50 ...
drwxr-xr-xr-x 2 root root root 4096 Aug 22 12:37 bin
drwxr-xr-x 4 root root root 4096 Aug 22 12:37 boot
drwx------ 3 root root 4096 Aug 28 09:50 data
drwxr-xr-x 18 root root root 3780 Aug 28 09:46 dev
[...]

Other useful options are:

  • -s : shows the size in blocks of each file.
  • -t : brings the day and time of modification
  • -R : lists also the subfolders
  • --color : shows the content colored


The permissions are used on the basis of three capabilities:

  • Read, which is coded with r (read).
  • Writing, which is coded with w (write). (write).
  • Execution, which is coded with x (execute). (execute).

The first column shows the permissions that encode:

  • Type of entry: d indicates directory' (folder).
  • Permissions of the owner, first third of rwx
  • Group permissions, second string of rwx
  • Permissions for the rest of the world, third string of rwx.

Next, the owner and group are displayed, followed by the last modification date and the name of the file or folder.

The help in ls only works with ls --help not with ls -h

mkdir

Creates a new directory.

$ mkdir test

Creates a test folder in the current directory.

Let's check that it has been created properly:

$ ls
test

With the -p option, it allows to create successive nested folders at once, for example:

$ mkdir -p a/b/c

Which creates the following folder structure.

a
└── b
    └── c

touch

Creates a new empty file (no contents) with the name we specify.

$ touch file.txt

We check that the file has been created correctly.

$ ls
file.txt

cp

Copies a file or directory.

$ cp file.txt file_copy.txt

To copy a folder and its contents we have to use the -r (recursive) option.

$ cp -r test test_copy
  • We check the result with the ls command.
  • If we are in the destination folder, the structure would be as follows:
$ cp <source path name> <file name>

mv

Move file or folder.

mv file_copy.txt file2.txt

It is also used for renaming.

mv test_copy test_copy2

rm

  • Deletes an empty file or folder:
rm <name>
  • To delete a non-empty folder and its contents:
rm -r <name>

IMPORTANT: Never do rm -r *

  • EXERCISES:
  1. Create the following structure within the /tmp directory (use the mkdir, touch and cp commands):
/tmp
├── folder1.
├── folder2
│ ├─── folder1.txt
│ └└── file2.md
└── folder3.
    ├── file1.txt
    └─── file2.md
  1. Based on the previous exercise, change the structure to the following (use mv and rm commands):
/tmp
└── folder.
    ├─── file1.txt
    └─── file2.md

Step 2: Screen print and output redirection

echo

It is used to print a line of text on the screen or environment variables. It also allows us to write to files.

echo <option> <text>
echo "Hello world"

There is difference between quotes \" and single quotes \'.

A few options:

  • -n, to tell echo not to add a line break after the message

An environment variable is an edited variable with some value, by default there are some created, such as the HOME variable, which contains the user's directory:

echo $HOME

We will take advantage and create a variable ourselves:

ME="my name"

We check:

echo $ME

Output redirection

> and >> is used to redirect an output. For example, using the command echo, we can send a text inside a file:

echo "This is a test" > file.txt

The difference between > and >> is that > overwrites whatever is in the file and adds the content, and >> does not overwrite, it just adds the new content.

Let's do the test:

echo "This is a test" > file.txt
echo "This is a test2" > file.txt
echo "This is a test" >> file2.txt
echo "This is a test2" >> file2.txt

In any case, if the file does not exist, it is created.

Step 3: Advanced file and folder handling

cat

  • Displays the contents of a file:
cat file.txt
cat file1 file2
  • Creates a file:
cat > filename

SAVE FILE: Press ENTER and then CTRL + D

  • Add more lines to the file:
cat >> filename
  • Concatenate files:
cat file1 file2 > file3

grep

Used to locate matches of a certain pattern in files and displays the match, if found, on screen.

grep <option> <pattern> <files to search for>.

For example, to search for the pattern test in the file file.txt.

$ grep -n test file.txt
1:This is a test
  • Main command options:

-c : Writes the number of lines found.

-i : Is case insensitive.

-l : Displays the names of the files containing the searched characters.

-n : Each line is preceded by its number in the file.

-s : Messages indicating that a file cannot be opened are not displayed.

-v : Displays only lines that do not meet the condition.

wc

Used to count the number of words, characters, lines or bytes contained in a file.

The most commonly used options are:

  • -l: displays the number of lines contained in the file.
  • w: shows the number of words.

-m: shows the number of characters -c: shows the number of bytes.

Let's try and count everything in the file file2.txt:

wc -l file2.txt
wc -w file2.txt
wc -m file2.txt
wc -c file2.txt

We can count more than one file at a time:

wc -l file.txt file2.txt

It will show us the count of each file and the total.

find

To search for files we can use the find command.

For example, to search for the file python3 in the whole directory tree (from the root) we can use the command:

find / -name python3

We can restrict the search to files with -type f

find / -type f -name python3

or for directories with -type d

Pipe (|)

The pipeline will serve us to connect several commands at the same time, let's see some examples, which is always better understood:

cat file2.txt | grep test
grep -nr test file2.txt | wc -l

In the first example, we display the contents of file2.txt, and this output is passed to the grep command. We pass it to the grep command, which searches for lines containing test.

In the second example, we look for all lines containing the word test, and this output is passed to the w output is passed to the wc command, which counts the lines in the output of the grep command. grep command.

Redirections

Let's look at a few concepts before we continue:

  • Standard input: represents the data that an application needs to run, such as a data file or information entered from the terminal and is represented in the terminal as type 0.
  • Standard output: it is the way that the applications use to show you information, there we can see the progress or simply the messages that the application wants to give you at a certain moment and it is represented in the terminal as type 1.
  • Standard error: it is the way in which the programs inform you about the problems that can be found at the moment of the execution and it is represented in the terminal as type 2.

Redirections are used to move information from one type to another.

We have already seen previously the use of > and >>, and also the |, let's see some more:

  • command < file: Takes the file input.
  • command 2> file: Sends the error output of command to file
  • command 2>&1: Send the error output to the standard output
  • command &> file: Send the standard and error output to file; equivalent to command > file 2>&1

Step 4: Users and groups

whoami

Used to get the current user.

whoami

Most likely we will get ubuntu.


who

This is to find out who is currently on the machine logged in.

who

It's probably just us:

ubuntu pts/0 2019-08-28 13:51 (192.168.122.1)


su and sudo

su (substitute user): used to change user. For example:

su root

Here you will be prompted for the password of the user you want to change to.

sudo (super user do): not all distributions integrate it, because it's less secure than su. secure than su, this command allows us to execute a command as another user, by default, if we do not give user, it will do it with the root user. If sudo password, it asks for the user's password, since the user is the one who has permissions to use sudo or not. has permissions to use sudo or not.

sudo ls -a /root

If we had for example a user 'practica' and we wanted to run a command on his behalf:

sudo -u practica ls

As root I can add users so they can sudo like this:

adduser sudo user

This makes the user user able to do sudo.


adduser and useradd

Both commands are used to create a new user, but they have their differences.

useradd is a system command, and adduser is a script written in perl that uses the useradd command. uses the useradd command. The difference at first glance is that adduser will create the /home/user directory and useradd needs the -m option to do the same thing. option to do the same. You can use whichever one you want, although useradd is more recommended, as it will work the same on all distributions.

Let's add a user practica with its directory in /home (note that we need super user permissions to do this). we need super user permissions to perform this task):

sudo useradd -m practice

Let's check that the home directory is created and that the user practica is working:

ls /home # we will see the practica directory.
sudo -u practica whoami # it will tell us that we are the user practica


passwd

It is used to modify the password of a user, for example, we are going to modify the password of the user we have previously created. the password of the user we have previously created:

sudo passwd practice

It will ask us twice for the password, and we will have it updated. Let's go now try the su command to change the user:

su practice
whoami

We will see that we have changed the user, to close this user, we type exit' or press 'CTLR + D'.


deluser and userdel

Both are used to delete a user, similar to adduser and useradd, userdel is the command and deluser is the script. command and deluser is the script.

Let's delete the user we have created, including deleting his /home. (-r option):

userdel -r practice

Let's check that we no longer have user or /home:

ls /home
sudo -u practice whoami


addgroup and groupadd

Both are used to create a group. Groups are used to group users and that they have the same permissions. For example, when we install some tools like docker, it creates the docker group automatically so that we can easily add a user to the group. so that we can easily add a user to the group, and this one has the permissions to work with that tool. permissions to work with that tool.

Let's create a new group, let's call it guest:

sudo groupadd guest


usermod and groups

usermod is used to assign a group to a user.

groups is used to view the groups a user belongs to.

Let's add a practica user, we will add him to the guest group and we will see the groups he belongs to we will see the groups to which he belongs:

sudo useradd -m practica
sudo usermod -G guest practica
groups practica

We will see that the user practica belongs to the guest group.


delgroup and groupdel

Both are used to delete a group. Let's delete the group we created previously:

sudo groupdel guest

We can notice now that the user practica does not belong to the guest group:

groups practica


Step 5: Permissions

Nomenclature rwx and numeric

If we do an ls -l, the first string that appears is the permissions of the file (-rwxrw). file (-rwxrw-r--), we can divide it in 4 elements:

- (rwx) (rw-) (r--) | | | | type owner group others

  • type: the type of the file, it can be d (directory), l (symbolic link), - (normal file)
  • owner: owner permissions
  • group: group permissions
  • others: permissions for other users

Meaning of permissions:

  • r: read permissions
  • w: write permissions
  • x: execute permissions

Finally, there is an abbreviated mode for these permissions which is used with numbers.

rwx 001 in binary is 1 in decimal. Execute permissions 010 in binary is 2 in decimal. Write permissions 100 in binary is 4 in decimal. Read permissions

The sum of the permissions, gives us the numerical value. Let's see an example: rwx rw- r-- 111 110 100

7 6 4

chmod

Allows you to change the access permissions to a file or directory. We can give permissions in two different ways.


character mode

Let's first see some details:

User classes:

  • owner (u)
  • group (g)
  • others (o)
  • all (a)

Modifier:

  • add (+)
  • delete (-)
  • overwrite (=)

To give permissions we would have to select the class of user the modifier and the permissions, let's see an example:

chmod u+x file # add execute permissions to the user
chmod go-w file # remove write permissions to the group and other users
chmod u=rwx,go=r file # give the user all permissions, and the group and others only read permissions


Octal mode

With octacl mode, it is as if we always use modify overwrite, let's take some examples:

chmod 744 file # To the user we give all permissions, and to the group and others we only give read permissions.
chmod 777 file # Everyone has permissions for everything
chmod 600 file # only the owner has read and write permissions


Examples

Let's see how permissions work with some examples. We are going to create a test file and we are going to remove the write permissions to our user, and we are going to give it user, and we are going to give write permissions to others:

touch tests
ls -l tests # this way we will see that by default the permissions are rw-rw-r--
chmod u-w,o+w tests
ls -l tests # now the permissions should be -w-rw-rw-

Once the permissions are given, let's see that with our user we can read but not write:

cat tests
echo "testing" > tests
exit

And now let's see that the user tests, can read and write:

your practice
cat tests
echo "testing" > tests


chown

Allows you to change the owner of a file or directory.

Let's for example now pass the file tests to the user practica:

sudo chown practice tests
ls -l tests

We will see that the current user is now tests, although we see that the ls shows the following:

-r--rw-rw- 1 practice ubuntu 9 Aug 29 09:34 tests

There is one thing to note, and that is that files and directories are assigned a user and a group by default. a user and a group, by default the same user is used as group when we create a new file or directory. If we want that when we change the owner, we can also change the group of the file, we can do it in the following way following way:

sudo chown practice:practice tests

Once this is done, let's now test that the permissions are still working as before, now with the ubuntu user, we should be able to read and write to the file tests. the file tests, and with the user practica, only read, this is so because now the owner is practica:

# tests ubuntu user
cat tests
echo test >> tests
# tests user practica
your practice
cat tests
echo "testing" > tests
exit


Step 6: Processes

ps

Used to see what processes are running on the system, let's see an example:

ps aux # with the aux option we will show all the processes in the system.

Each line is a process, and each process displays its PID, user, amount of memory and cpu used, command and other details.

man kill & # adding the & to the end of a command causes it to run in the background, so it will stay open.

The output of this command will show us the PID of the process we just executed, let's check it. run, let's check it:

ps aux | grep "man kill"

We will see that the PID matches.

kill

This command will be used to kill a process. kill is used followed by a signal to send the process followed by one or more PIDs. signal to send to the process followed by one or more PIDs. to see the available signals, we can list them: == == == This command is used to kill a process. to see the available signals, we can list them:

kill -l

The most commonly used are SIGTERM and SIGKILL, the first one tries to end the process in an unabrupt way, the second one the first one tries to terminate the process in a less abrupt way, and the second one is usually used when the process does not heed this first signal. We are now going to terminate the process we created before, if we don't remember the PID, let's look it up again:

ps aux | grep "man kill"

We send signal to terminate the process and check that it has finished:

kill -SIGTERM 8470
ps aux | grep "man kill"

We can also notice that in the signal listing, there are some numbers, we can use those numbers instead of the words, for example:

man kill &
kill -15 PID_PREVIOUS_COMAND

We check that the process has finished correctly:

ps aux | grep "man kill"


Step 7: CPU, RAM and Disk status

top

This command will help us to see the list of processes and the status of CPU and memory. memory.

Let's try the command and observe the output in detail:

top

To exit, we press q.


df and du

df (disk free) and du (disk usage). Both are utilities for displaying disk usage. the disks.

With df we will show the space information on each mounted device:

df

These commands that show us the size of the files, almost always have an option to show it in a more readable format (-h)

df -h

Much better.

Now let's look at the du command, which will show the size of a file or directory and its subdirectories. directory and its subdirectories, let's not forget the -h:

du -h /home/ubuntu
du -h /home/ubuntu/tests

If we only want to know the total of a folder and we are not interested in its subfolders, we can use subfolders, we can use the -s option:

du -sh /home/ubuntu


free

Used to view the memory status, as always, -h option:

free -h


lsblk

Shows us the information of all block devices (hard disks, pendrivers, CD_ROM, SSD, ...).

lsblk

In the following practice this command will be used more thoroughly, as we will we will be working with devices.


Step 8: Software package management and repositories

Linux systems include, in addition to the basic operating system tools, software repositories that can be optionally installed by the system administrator. The manufacturers of Linux distributions include ready-to-install software packages that are integrated with the system.

A package includes the software necessary for a certain application to function properly, as well as its dependency packages.

In Ubuntu, the software package and repository management tool is called apt'.

List of software package repositories

In Ubuntu the repository list is located in the /etc/apt/sources.list file. We can check which repositories we have added:

cat /etc/apt/sources.list

Updating the software package listing

To update the package listing we will use the command:

sudo apt update

Installing a new package

To install a new package, we will use the install command, for example, let's install tree, similar to ls but shows the directory tree:

sudo apt install tree

Uninstallation of a package

We will use the purge command or the remove command, purge removes everything, and remove keeps the configuration if the package had it. Let's remove the previously installed package:

sudo apt purge tree

Step 9: Remote administration with ssh

ssh (Secure SHell) allows you to remotely administer a system from the shell. In order to access the virtual machine via ssh, the openssh-server package must be installed.

sudo apt-get install openssh-server

Once installed, we need to query the IP address of the virtual machine

ip address

From the hypervisor we can access by ssh to the virtual machine with the command:

ssh user@ip

Being user the user name with which you access the virtual machine and the ip address shown by the command ip address.

For example, if the selected user is ubuntu and the IP is 192.168.122.123, then the invocation to ssh is the following:

ssh ubuntu@192.168.122.123

In case openssh-server is not installed on the virtual machine, the connection may be rejected:

ssh: connect to host 192.168.122.123 port 22: Connection refused

To solve the problem it is necessary to install OpenSSH client on the virtual machine

sudo apt install openssh-client

And then install OpenSSH server

sudo apt install openssh-server


Exercise

  • Create a file 'x.txt' with this content (use echo or cat!)
hello world
1234567890
bye bye
test test
  • create a folder x that has a subfolder y
  • move 'x.txt' to subfolder y
  • make a copy of 'x.txt' to folder 'x' whose name is 'x.txt.backup'
  • make a copy of the entire 'x' folder and name it 'x.backup'
  • remove the 'x.backup' folder
  • find all folder in your filesystem whose name is 'bin'
  • count the number of folders whose name is 'bin' in your filesystem
  • check if there is a user 'practica' in /etc/passwd
  • check for all files in your home folder with read+write permission for the group and store it in a file called "group_permissions.txt"