Terminal (3/3): system administration

Terminal (3/3): system administration

In the previous parts we saw the fundamentals of the terminal and the network, compression and process utilities. In this final chapter we put on the administrator’s hat: **memory, disk and interface management**, **package handling**, **administration users and permissions**, **security**, **command scheduling** and some final **miscellaneous tricks**.

Disclaimer: This post has been translated to English using a machine translation model. Please, let me know if you find any mistakes.

πŸ“š **This entry is part of the _Introduction to the terminal_ series**, divided into three chapters that are read in order:

> * Part 1: Navigation, files, and commands

* Part 2: Network, compression, and processes

* πŸ‘‰ **Part 3: System administration**

RAM memory managementlink image 40

If we only want to obtain information from memory, we can use the free command

	
< > Input
Python
!free
Copied
>_ Output
			
total usado libre compartido bΓΊfer/cachΓ© disponible
Memoria: 32774516 6563544 20091804 276296 6119168 25479600
Swap: 2097148 0 2097148

But since this information isn't very easy to digest, we added the -h (human) flag to make it easier to read

	
< > Input
Python
!free -h
Copied
>_ Output
			
total usado libre compartido bΓΊfer/cachΓ© disponible
Memoria: 31Gi 6,3Gi 19Gi 270Mi 5,8Gi 24Gi
Swap: 2,0Gi 0B 2,0Gi

Hard Drive Managementlink image 41

To get information about the hard drive, we use the du command. If we only enter this command in the terminal, it will give us information about all the folders on our machine, so, in order not to get too much information, it is necessary to provide it with a path that we want to scan

	
< > Input
Python
!du ~/Documentos/web/portafolio/posts/
Copied
>_ Output
			
8 /home/wallabot/Documentos/web/portafolio/posts/__pycache__
1648 /home/wallabot/Documentos/web/portafolio/posts/notebooks_translated
4288 /home/wallabot/Documentos/web/portafolio/posts/html_files
336 /home/wallabot/Documentos/web/portafolio/posts/prueba/tocompress
1132 /home/wallabot/Documentos/web/portafolio/posts/prueba
16 /home/wallabot/Documentos/web/portafolio/posts/introduccion_python/__pycache__
28 /home/wallabot/Documentos/web/portafolio/posts/introduccion_python
11232 /home/wallabot/Documentos/web/portafolio/posts/

As before, we add the -h flag (human) to make it easier to read

	
< > Input
Python
!du ~/Documentos/web/portafolio/posts/ -h
Copied
>_ Output
			
8,0K /home/wallabot/Documentos/web/portafolio/posts/__pycache__
1,7M /home/wallabot/Documentos/web/portafolio/posts/notebooks_translated
4,2M /home/wallabot/Documentos/web/portafolio/posts/html_files
336K /home/wallabot/Documentos/web/portafolio/posts/prueba/tocompress
1,2M /home/wallabot/Documentos/web/portafolio/posts/prueba
16K /home/wallabot/Documentos/web/portafolio/posts/introduccion_python/__pycache__
28K /home/wallabot/Documentos/web/portafolio/posts/introduccion_python
11M /home/wallabot/Documentos/web/portafolio/posts/

Interface managementlink image 42

In Ubuntu, by default we start in a graphical interface, but we can open other interfaces, which will not be graphical, by pressing CTRL+ALT+F<num> where the number can range from 1 to 6. Only 2 will have the graphical interface and 1 will be the login prompt

When handling multiple interfaces, we may not know which one we are in, so by entering the tty command, it will tell us which one we are in

	
< > Input
Python
!tty
Copied
>_ Output
			
/dev/pts/0

Package managementlink image 43

PPA Repositories (Personal Package Archives)link image 44

In Linux, package management is done through repositories. This is a list of addresses where the binaries of our programs are located. Thus, when we want to update or install our programs (we will explain how later), what the operating system will do is look at the list of these repositories and go to the indicated addresses to search for the binaries

This list of repositories is located in /etc/apt/sources.list and inside the /etc/apt/sources.list.d folder. Let's take a look at this list.

	
< > Input
Python
terminal("cat /etc/apt/sources.list", max_lines_output=10)
Copied
>_ Output
			
# deb cdrom:[Ubuntu 20.04.2.0 LTS _Focal Fossa_ - Release amd64 (20210209.1)]/ focal main restricted
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://es.archive.ubuntu.com/ubuntu/ focal main restricted
# deb-src http://es.archive.ubuntu.com/ubuntu/ focal main restricted
## Major bug fix updates produced after the final release of the
## distribution.
deb http://es.archive.ubuntu.com/ubuntu/ focal-updates main restricted
...
deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /
# deb-src https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /
deb https://apt.kitware.com/ubuntu/ focal main
# deb-src https://apt.kitware.com/ubuntu/ focal main

The first lines that include the word cd-rom are references to the installation CD; they always come with the words deb cdrom: even if the system was installed over the network or from a USB drive.

From here on, various lines begin to appear that start with deb or deb-src. deb contains the binaries, and deb-src contains the source code.

Every valid repository address has one of these formats:

  • deb http://server_address/folder_name version_name (main or universe or multiverse or main restricted, etc)
  • deb-src http://server_address/folder_name version_name (main or universe or multiverse or main restricted, etc)

The six types of Ubuntu repositories are:

  1. Main

The Main repository is enabled by default and contains only free and open-source software or FOSS for its acronym in English (Free and Open-Source Software).

  1. Universe

As Main, Universe also offers FOSS. The difference is that in this repository it is not Ubuntu that guarantees regular security updates, but rather the community that is responsible for its support. It comes enabled by default, but not always. Some operating systems have it disabled by default and we may need to enable it if we are running a Live Session. If we have not added it, we can do so with this command:

sudo add-apt-repository universe

What do we find in Β«UniverseΒ»? I’d say most of the worthwhile software, including VLC and OpenShot.

  1. Multiverse

From here on come the Ubuntu repositories with less freedom. Multiverse contains software that is no longer FOSS, and Ubuntu cannot enable this repository by default due to legal and licensing issues. On the other hand, it also cannot provide patches and updates. With this in mind, we need to decide whether or not to add it, something we can do with this command:

sudo add-apt-repository multiverse
  1. Restricted

In Ubuntu repositories we can find free and open-source software, but this is not possible when it comes to something related to hardware. In the Restricted repositories, we will find drivers, such as those for graphics cards, touchpads, or network cards.

sudo add-apt-repository restricted
  1. Partner

This repository contains proprietary software compiled by Ubuntu from its partners.

  1. Third-party Ubuntu repositories

Finally, we have third-party repositories. Ubuntu always tries to offer the best possible user experience, and that is one of the reasons why it rejects certain software. There are also developers who prefer to have total control over what they offer, and for that reason they create their own repositories.

Add repositorieslink image 45

If we are on Ubuntu, we can add a repository using the command add-apt-repository <repository>. But as in other distributions that are not Ubuntu, we do not have this command

Update the repositorieslink image 46

By using the apt update command, we can update to the latest versions of the packages we have in our repository.

Update the packageslink image 47

Using the apt upgrade command, we can update the programs we have installed and whose repository we have previously updated.

Update the kernellink image 48

If we use the apt dist-upgrade command, kernel packages will also be updated

Warning!: Updating kernel packages can cause some packages to break

Reminder: When updating kernel packages, for them to take effect it is necessary to restart the computer

With the command apt search <package> we can find packages

	
< > Input
Python
terminal("apt search vlc", max_lines_output=10)
Copied
>_ Output
			
Ordenando...
Buscar en todo el texto...
anacrolix-dms/focal 1.1.0-1 amd64
Go UPnP DLNA Digital Media Server with basic video transcoding
cubemap/focal 1.4.3-1build1 amd64
scalable video reflector, designed to be used with VLC
dvblast/focal 3.4-1 amd64
Simple and powerful dvb-streaming application
...
x264/focal 2:0.155.2917+git0a84d98-2 amd64
video encoder for the H.264/MPEG-4 AVC standard

List of installed packageslink image 50

To see which packages we have installed, we can use dpkg -l, which will give us a list of all the packages installed on our computer.

	
< > Input
Python
terminal("dpkg -l", max_lines_output=10)
Copied
>_ Output
			
Deseado=desconocido(U)/Instalar/eliminaR/Purgar/retener(H)
| Estado=No/Inst/ficheros-Conf/desempaqUetado/medio-conF/medio-inst(H)/espera-disparo(W)/pendienTe-disparo
|/ Err?=(ninguno)/requiere-Reinst (Estado,Err: mayΓΊsc.=malo)
||/ Nombre VersiΓ³n Arquitectura DescripciΓ³n
+++-==========================================-=====================================-============-===========================================================================================================================================================================================================================================================================================================================================================================================================================================
ii accountsservice 0.6.55-0ubuntu12~20.04.5 amd64 query and manipulate user account information
ii acl 2.2.53-6 amd64 access control list - utilities
ii acpi-support 0.143 amd64 scripts for handling many ACPI events
ii acpid 1:2.0.32-1ubuntu1 amd64 Advanced Configuration and Power Interface event daemon
ii adduser 3.118ubuntu2 all add and remove users and groups
...
ii zip 3.0-11build1 amd64 Archiver for .zip files
ii zlib1g:amd64 1:1.2.11.dfsg-2ubuntu1.5 amd64 compression library - runtime
ii zlib1g:i386 1:1.2.11.dfsg-2ubuntu1.5 i386 compression library - runtime
ii zlib1g-dev:amd64 1:1.2.11.dfsg-2ubuntu1.5 amd64 compression library - development

If we want to check whether we have a package installed, we can use the previous command, create a pipe, and search for the package name with grep

	
< > Input
Python
terminal("dpkg -l | grep vlc")
Copied
>_ Output
			
Deseado=desconocido(U)/Instalar/eliminaR/Purgar/retener(H)
| Estado=No/Inst/ficheros-Conf/desempaqUetado/medio-conF/medio-inst(H)/espera-disparo(W)/pendienTe-disparo
|/ Err?=(ninguno)/requiere-Reinst (Estado,Err: mayΓΊsc.=malo)
||/ Nombre VersiΓ³n Arquitectura DescripciΓ³n
+++-==============-============-============-=================================
ii grep 3.4-1 amd64 GNU grep, egrep and fgrep
ii vlc 3.0.9.2-1 amd64 multimedia player and streamer

Install downloaded packages and not from repositorieslink image 51

On some occasions, when you want to install a program, what they do is give you a .deb file, so to install it, we use the command dpkg -i <file.deb>

User Administratorlink image 52

Active user information with idlink image 53

Using the id command, I can see which user I am

	
< > Input
Python
!id
Copied
>_ Output
			
uid=1000(wallabot) gid=1000(wallabot) grupos=1000(wallabot),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),120(lpadmin),131(lxd),132(sambashare),998(docker)

With this command, I can see my id, the group ID gid, and the groups I belong to. In Debian-based distributions, users are assigned an ID starting from 1000, while the root user is assigned ID 0

Active user information with whoamilink image 54

Another command to find out which user I am is whoami

	
< > Input
Python
!whoami
Copied
>_ Output
			
wallabot

File with information on all userslink image 55

User information is in the /etc/passwd file

	
< > Input
Python
terminal("cat /etc/passwd", max_lines_output=10)
Copied
>_ Output
			
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
...
sshd:x:126:65534::/run/sshd:/usr/sbin/nologin
nvidia-persistenced:x:127:135:NVIDIA Persistence Daemon,,,:/nonexistent:/usr/sbin/nologin
fwupd-refresh:x:128:136:fwupd-refresh user,,,:/run/systemd:/usr/sbin/nologin
glances:x:129:137::/var/lib/glances:/usr/sbin/nologin

Change a user's passwordlink image 56

If you want to change a user's password, you have to use the command passwd <user> <password>. If the user is not specified, it will use whatever the command whoami returns.

Create users with useraddlink image 57

To create a new user, use the command useradd <user name>, let's create a new user

	
< > Input
Python
!sudo useradd usertest1
Copied
>_ Output
			

Let's see if it is in the file with all the users

	
< > Input
Python
!cat /etc/passwd | grep usertest
Copied
>_ Output
			
usertest1:x:1001:1001::/home/usertest1:/bin/sh

As we can see, the user has been created with id 1001, the next one after the one my wallabot user had, which was the last one

However, when creating this user, it did not ask us to assign a password. In addition, if we look at what is inside home

	
< > Input
Python
!ls /home
Copied
>_ Output
			
wallabot

Only the wallabot user folder is present, but not the test1 user folder

Create users with adduserlink image 58

Therefore, let's look at another command for creating users that does require a password and does create a folder in home. This command is adduser

	
< > Input
Python
!sudo adduser usertest2
Copied
>_ Output
			
[sudo] contraseΓ±a para wallabot:
AΓ±adiendo el usuario `usertest2' ...
AΓ±adiendo el nuevo grupo `usertest2' (1002) ...
AΓ±adiendo el nuevo usuario `usertest2' (1002) con grupo `usertest2' ...
Creando el directorio personal `/home/usertest2' ...
Copiando los ficheros desde `/etc/skel' ...
Nueva contraseΓ±a:
Vuelva a escribir la nueva contraseΓ±a:
passwd: contraseΓ±a actualizada correctamente
Cambiando la informaciΓ³n de usuario para usertest2
Introduzca el nuevo valor, o presione INTRO para el predeterminado
Nombre completo []:
NΓΊmero de habitaciΓ³n []:
TelΓ©fono del trabajo []:
TelΓ©fono de casa []:
Otro []:
ΒΏEs correcta la informaciΓ³n? [S/n] s

If we look in the file with all the users

	
< > Input
Python
!cat /etc/passwd | grep usertest
Copied
>_ Output
			
usertest1:x:1001:1001::/home/usertest1:/bin/sh
usertest2:x:1002:1002:,,,:/home/usertest2:/bin/bash

We see that the user usertest2 has been created

	
< > Input
Python
!ls /home
Copied
>_ Output
			
usertest2 wallabot

And we can see that a folder has been created for it in home

To delete a user, use the command userdel <username>

	
< > Input
Python
!sudo userdel usertest1
Copied
>_ Output
			
	
< > Input
Python
!sudo userdel usertest2
Copied
>_ Output
			

Let's see if they are in the file with all the users.

	
< > Input
Python
!cat /etc/passwd | grep usertest
Copied

We see that nothing appears anymore; in fact, we see the end of that file.

	
< > Input
Python
!tail /etc/passwd
Copied
>_ Output
			
geoclue:x:122:127::/var/lib/geoclue:/usr/sbin/nologin
pulse:x:123:128:PulseAudio daemon,,,:/var/run/pulse:/usr/sbin/nologin
gnome-initial-setup:x:124:65534::/run/gnome-initial-setup/:/bin/false
gdm:x:125:130:Gnome Display Manager:/var/lib/gdm3:/bin/false
wallabot:x:1000:1000:wallabot,,,:/home/wallabot:/bin/bash
systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin
sshd:x:126:65534::/run/sshd:/usr/sbin/nologin
nvidia-persistenced:x:127:135:NVIDIA Persistence Daemon,,,:/nonexistent:/usr/sbin/nologin
fwupd-refresh:x:128:136:fwupd-refresh user,,,:/run/systemd:/usr/sbin/nologin
glances:x:129:137::/var/lib/glances:/usr/sbin/nologin

Make an administrator userlink image 59

First, we are going to create a new user, who will not be an administrator at first.

	
< > Input
Python
!sudo adduser noadmin
Copied
>_ Output
			
AΓ±adiendo el usuario `noadmin' ...
AΓ±adiendo el nuevo grupo `noadmin' (1001) ...
AΓ±adiendo el nuevo usuario `noadmin' (1001) con grupo `noadmin' ...
Creando el directorio personal `/home/noadmin' ...
Copiando los ficheros desde `/etc/skel' ...
Nueva contraseΓ±a:
Vuelva a escribir la nueva contraseΓ±a:
passwd: contraseΓ±a actualizada correctamente
Cambiando la informaciΓ³n de usuario para noadmin
Introduzca el nuevo valor, o presione INTRO para el predeterminado
Nombre completo []:
NΓΊmero de habitaciΓ³n []:
TelΓ©fono del trabajo []:
TelΓ©fono de casa []:
Otro []:
ΒΏEs correcta la informaciΓ³n? [S/n] s

Let's see which groups the user we just created belongs to; to do this, we use the command groups <user>

	
< > Input
Python
!groups noadmin
Copied
>_ Output
			
noadmin : noadmin

As we can see, it is only in the noadmin group, which is a group that was created when the user was created

Let's see which groups my user belongs to

	
< > Input
Python
!groups wallabot
Copied
>_ Output
			
wallabot : wallabot adm cdrom sudo dip plugdev lpadmin lxd sambashare docker

As we can see, my user belongs to several more groups, including one called sudo. Users who have access to this group have administrator privileges, so for the new user we have created to have these privileges, we need to add it to the sudo group.

To add a user to a group there are two ways: one is with the command gpasswd -a <user> <group>

	
< > Input
Python
!sudo gpasswd -a noadmin sudo
Copied
>_ Output
			
AΓ±adiendo al usuario noadmin al grupo sudo

We now see which groups the noadmin user belongs to

	
< > Input
Python
!groups noadmin
Copied
>_ Output
			
noadmin : noadmin sudo

As we can see, it already belongs to the sudo group, so it would already have administrator privileges

We remove the user noadmin from the sudo group with the command gpasswd -d <user> <group>

	
< > Input
Python
!sudo gpasswd -d noadmin sudo
Copied
>_ Output
			
Eliminando al usuario noadmin del grupo sudo

We see that noadmin no longer belongs to the sudo group

	
< > Input
Python
!groups noadmin
Copied
>_ Output
			
noadmin : noadmin

The second command to add a user to a group is usermod -aG <group> <user>

	
< > Input
Python
!sudo usermod -aG sudo noadmin
Copied
>_ Output
			

We return to see which groups the user noadmin belongs to

	
< > Input
Python
!groups noadmin
Copied
>_ Output
			
noadmin : noadmin sudo

We remove the user noadmin from the sudo group and delete it

	
< > Input
Python
!sudo gpasswd -d noadmin sudo
Copied
>_ Output
			
Eliminando al usuario noadmin del grupo sudo
	
< > Input
Python
!sudo userdel noadmin
Copied
>_ Output
			

Command historylink image 60

historylink image 61

If we enter the history command in the terminal, we see a history of the commands used

	
< > Input
Python
!history
Copied
>_ Output
			
1009 docker build . nvidia/cuda
1010 docker build --help
1011 docker build --build-arg nvidia/cuda
1012 docker build --build-arg [nvidia/cuda]
1013 cd ../docker/
1014 docker ps -a
1015 docker rm boring_wescoff
1016 docker compose up -d
1017 docker compose exec deepstream61 bash
1018 cd ..
....
1996 ps
1997 ps aux
1998 camerasIP.sh
1999 sudo su
2000 sudo useradd usertest
2001 sudo userdel usertest
2002 sudo useradd usertest
2003 sudo userdel usertest
2004 sudo su
2005 sudo apt install history
2006 history
2007 clear
2008 history

If we want to execute one of the commands from the history, we do so using !<num command>, for example, if I want to re-run command 1996

	
< > Input
Python
! !1996
Copied
>_ Output
			
PID TTY TIME CMD
6610 pts/0 00:00:00 bash
20826 pts/0 00:00:00 ps

A more refined way to search through the history is to press CTRL+r. When you do this, the following message will appear in the console

	
< > Input
Python
!reverse-i-search)`':
Copied
>_ Output
			

So as you type, commands matching what you’ve entered will appear. For example, if I type if, the last time I used ifconfig appears.

If we enter CTRL+r again, older matches will appear.

Remove commands from historylink image 63

There are commands like ls, cd, and pwd that do not add much value if they are kept in the history, so it can be configured so they are not saved in the history. To do this, we modify the ~/.bashrc file and add the line HISTIGNORE="pwd:ls:cd"

Securitylink image 64

Firewall ufwlink image 65

Ubuntu comes with the ufw firewall installed, but to verify it we use the following command

	
< > Input
Python
!sudo ufw status
Copied
>_ Output
			
Estado: inactivo

As we can see, by default it is inactive, so we are going to create a set of rules. For example, let’s start by opening port 22 (SSH). To do this, we use the command sudo allow <port> comment "<coment>", which lets us open a port and add a comment

	
< > Input
Python
!sudo ufw allow 22 comment 'ssh'
Copied
>_ Output
			
Regla aΓ±adida
Regla aΓ±adida (v6)

As we can see, it has opened port 22 for IPv4 and IPv6. Now let's activate ufw with the command ufw enable

	
< > Input
Python
!sudo ufw enable
Copied
>_ Output
			
El cortafuegos estΓ‘ activo y habilitado en el arranque del sistema

If we want to see the rules we have in the firewall, we use the ufw status command

	
< > Input
Python
!sudo ufw enable
Copied
>_ Output
			
Hasta AcciΓ³n Desde
----- ------ -----
22 ALLOW Anywhere # ssh
22 (v6) ALLOW Anywhere (v6) # ssh

We can also tell it to show us the numbered rules with the command ufw status numbered

	
< > Input
Python
!sudo ufw status numbered
Copied
>_ Output
			
Estado: activo
Hasta AcciΓ³n Desde
----- ------ -----
[ 1] 22 ALLOW IN Anywhere # ssh
[ 2] 22 (v6) ALLOW IN Anywhere (v6) # ssh

Since they are numbered, we can delete one using the command ufw delete <rule number>, so to remove the rule for IPv6 we do

	
< > Input
Python
!sudo ufw delete 2
Copied
>_ Output
			
Estado: activo
Borrando:
allow 22 comment 'ssh'
ΒΏContinuar con la operaciΓ³n (s|n)? s
Regla eliminada (v6)

We see the status again

	
< > Input
Python
!sudo ufw status numbered
Copied
>_ Output
			
Estado: activo
Hasta AcciΓ³n Desde
----- ------ -----
[ 1] 22 ALLOW IN Anywhere # ssh

We see that rule number 2 has indeed been removed.

If we want to enable SSH access from a single IP, we use the from <IP> flag

	
< > Input
Python
!sudo ufw allow from 192.168.1.103 proto tcp to any port 22 comment 'ssh ip'
Copied
>_ Output
			
Regla aΓ±adida

We check the rules again

	
< > Input
Python
!sudo ufw status numbered
Copied
>_ Output
			
Estado: activo
Hasta AcciΓ³n Desde
----- ------ -----
[ 1] 22 ALLOW IN Anywhere # ssh
[ 2] 22/tcp ALLOW IN 192.168.1.103 # ssh ip

If we want to delete all the rules, we use the reset command

	
< > Input
Python
!sudo ufw reset
Copied
>_ Output
			
Estado: activo
Reiniciando todas las reglas a sus valores predeterminados instalados.
ΒΏContinuar con la operaciΓ³n (s|n)? s
Respaldando Β«user.rulesΒ» en Β«/etc/ufw/user.rules.20221205_171730Β»
Respaldando Β«before.rulesΒ» en Β«/etc/ufw/before.rules.20221205_171730Β»
Respaldando Β«after.rulesΒ» en Β«/etc/ufw/after.rules.20221205_171730Β»
Respaldando Β«user6.rulesΒ» en Β«/etc/ufw/user6.rules.20221205_171730Β»
Respaldando Β«before6.rulesΒ» en Β«/etc/ufw/before6.rules.20221205_171730Β»
Respaldando Β«after6.rulesΒ» en Β«/etc/ufw/after6.rules.20221205_171730Β»

We check the status again

	
< > Input
Python
!sudo ufw status numbered
Copied
>_ Output
			
Estado: inactivo

We see that there are no more rules

To disable the firewall, we use the ufw disable command

	
< > Input
Python
!sudo ufw disable
Copied
>_ Output
			
El cortafuegos estΓ‘ detenido y deshabilitado en el arranque del sistema

We check the status again

	
< > Input
Python
!sudo ufw status
Copied
>_ Output
			
Estado: inactivo

Security audit with Lynislink image 66

To install Lynis, you have to use the command sudo apt install lynis

To perform an audit of your system, you need to use the command lynis audit system. This starts a scan of the entire system and reports it back to you.

I’m not going to show the result of my system scan so as not to expose my vulnerabilities on the Internet

Command Schedulinglink image 67

Scheduling periodic commands with cronlink image 68

With the cron command, we can schedule commands to run periodically. To do this, we need to edit the /etc/crontab file

	
< > Input
Python
!cat /etc/crontab
Copied
>_ Output
			
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
17 * * * * root cd / &amp;&amp; run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / &amp;&amp; run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / &amp;&amp; run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / &amp;&amp; run-parts --report /etc/cron.monthly )
#

As can be seen, in this file there is a series of commands that are executed periodically. The format of these commands is as follows

<minute> <hour> <day of the month> <month> <day of the week> <user> <command>

To properly create the date and time when you want the command to run, there are plenty of online pages that help you write it correctly, such as crontab guru

One-time command scheduling with atlink image 69

When we want a command to be executed in the future, but we do not want it to run periodically, only once, we can use the at command. For example, you start an Azure or Amazon machine, for which you are charged, and you want to make sure it will be turned off so you do not get a surprise on the bill; you can schedule it to shut down at night. That way, even if you forget to turn it off, it will shut itself down.

We do an ls to the /tmp folder

	
< > Input
Python
!ls -l /tmp
Copied
>_ Output
			
total 60
-rw------- 1 wallabot wallabot 0 sep 24 08:51 config-err-QM3AAe
-rw-r--r-- 1 root root 2049 sep 24 08:51 glances-root.log
drwx------ 2 wallabot wallabot 4096 sep 24 09:06 pyright-9853-BG3nXEXw0Tao
drwxrwxr-x 3 wallabot wallabot 4096 sep 24 09:06 python-languageserver-cancellation
drwx------ 3 root root 4096 sep 24 08:51 snap-private-tmp
drwx------ 2 wallabot wallabot 4096 sep 24 08:51 ssh-mHjlSPPoqCp7
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-colord.service-rpjPri
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-fwupd.service-FzPoQf
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-geoclue.service-F6pMWi
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-ModemManager.service-Orf6Bi
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-switcheroo-control.service-1QXRqj
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-systemd-logind.service-lL35tg
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-systemd-resolved.service-iaswSi
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-systemd-timesyncd.service-Yet8lj
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-upower.service-oTL7Gg
drwx------ 2 wallabot wallabot 4096 sep 24 09:31 tracker-extract-files.1000

Now let’s program it to create a new file in /tmp within 5 minutes

	
< > Input
Python
!at 09:55 touch /tmp/at.txt
Copied
>_ Output
			
warning: commands will be executed using /bin/sh
at&gt; touch /tmp/at.txt
at&gt; &lt;EOT&gt;
job 1 at Sun Sep 24 09:55:00 2023

As we can see, you need to write at <time> and on the next line the command you want to run

We now see the files in /tmp

	
< > Input
Python
!ls -l /tmp
Copied
>_ Output
			
total 60
-rw-rw-r-- 1 wallabot wallabot 0 sep 24 09:55 at.txt
-rw------- 1 wallabot wallabot 0 sep 24 08:51 config-err-QM3AAe
-rw-r--r-- 1 root root 2049 sep 24 08:51 glances-root.log
drwx------ 2 wallabot wallabot 4096 sep 24 09:06 pyright-9853-BG3nXEXw0Tao
drwxrwxr-x 3 wallabot wallabot 4096 sep 24 09:06 python-languageserver-cancellation
drwx------ 3 root root 4096 sep 24 08:51 snap-private-tmp
drwx------ 2 wallabot wallabot 4096 sep 24 08:51 ssh-mHjlSPPoqCp7
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-colord.service-rpjPri
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-fwupd.service-FzPoQf
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-geoclue.service-F6pMWi
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-ModemManager.service-Orf6Bi
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-switcheroo-control.service-1QXRqj
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-systemd-logind.service-lL35tg
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-systemd-resolved.service-iaswSi
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-systemd-timesyncd.service-Yet8lj
drwx------ 3 root root 4096 sep 24 08:51 systemd-private-371a1d9479324db8bd79b6844b8b589b-upower.service-oTL7Gg
drwx------ 2 wallabot wallabot 4096 sep 24 09:31 tracker-extract-files.1000

As we can see, there is an at.txt file that was created at 09:55

Keyboard shortcutslink image 70

Below are a few useful keyboard shortcuts for using the terminal

  • Ctrl+a: moves the cursor to the beginning of the command line.
  • Ctrl+e: moves the cursor to the end of the command line.
  • Ctrl+l: clears the terminal, similar to what the clear command does.
  • Ctrl+u: clears from the cursor position to the beginning of the line. If at the end, clears the entire line.
  • Ctrl+k: clears from the cursor position to the end of the line. If at the beginning, clears the entire line.
  • Ctrl+h: does the same as the backspace key, deletes the character immediately before the cursor position.
  • Ctrl+w: deletes the word immediately before the cursor.
  • Alt+d or Esc+d: deletes the next word after the cursor.
  • Ctrl+p: sets the command line to the last command entered.
  • Ctrl+r: starts the search for previously used commands, typing part of a previously used command that we have executed, including options and parameters. Perform a search by pressing the key combination again and we will find earlier matches.
  • Ctrl+c: ends the process that is running, useful for regaining control of the system.
  • Ctrl+d: exits the terminal, similar to the exit command.
  • Ctrl+z: suspends the execution of the process that is running and puts it in the background; with the fg command, we can resume its execution.
  • Ctrl+t: swaps the position of the two characters before the cursor, useful for correcting typos.
  • Esc+t: swaps the position of the two words before the cursor, useful for correcting typos.
  • Alt+f: moves the cursor to the beginning of the next word on the line, the same as Ctrl+right in the GNOME terminal.
  • Alt+b: moves the cursor to the beginning of the previous word on the line, the same as Ctrl+left in the GNOME terminal.
  • Tab: auto-completes commands or directory or file paths.
  • Ctrl+Shift+F: opens a dialog to search for text in the terminal output.
  • Ctrl+Shift+g: searches for the next occurrence of the previous search in the terminal.
  • Ctrl+Shift+h: searches for the previous occurrence of the previous search in the terminal.
  • Ctrl+Shift+c: copies the selected terminal text to the clipboard.
  • Ctrl+Shift+v: pastes the clipboard text into the command line.
  • Up: sets in the command line the previous command from the history, just like Ctrl+p.
  • Down: sets the next command from the history in the command line.
  • Left Mouse: selects lines of text from the terminal.
  • Ctrl+Left Mouse: selects blocks of text in the terminal.

Folder system in Linuxlink image 71

In the following image, we can see what the folder system in Linux looks like

carpetas_linux

This image has been taken from the post on LinkedIn by Roberto Morais

Odditieslink image 72

You forgot to use sudolink image 73

Has it ever happened to you that you were about to use a command that needed to be run with sudo, but you forgot to type sudo? Well, after receiving the corresponding error, if you run sudo !!, the previous command will be executed with sudo

	
< > Input
Python
!apt update
Copied
>_ Output
			
Leyendo lista de paquetes... Hecho
E: No se pudo abrir el fichero de bloqueo Β«/var/lib/apt/lists/lockΒ» - open (13: Permiso denegado)
E: No se pudo bloquear el directorio /var/lib/apt/lists/
W: Se produjo un problema al desligar el fichero /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permiso denegado)
W: Se produjo un problema al desligar el fichero /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permiso denegado)

As can be seen, when running sudo update we get an error, but if we now run sudo !! it will execute sudo apt update

	
< > Input
Python
!sudo !!
Copied
>_ Output
			
[sudo] contraseΓ±a para wallabot:

Kernel messageslink image 74

With the dmesg command (display kernel ring buffer messages) we can view the kernel messages. For example, this is very useful for seeing whether a USB device has been connected, or for debugging hardware errors on our computer

	
< > Input
Python
!dmesg | tail
Copied
>_ Output
			
[ 35.812312] input: LogiOps Virtual Input as /devices/virtual/input/input33
[ 35.916406] input: LogiOps Virtual Input as /devices/virtual/input/input34
[ 36.002064] input: LogiOps Virtual Input as /devices/virtual/input/input35
[ 63.879806] input: MX Master 3 as /devices/virtual/misc/uhid/0005:046D:B023.0006/input/input36
[ 63.879931] logitech-hidpp-device 0005:046D:B023.0006: input,hidraw3: BLUETOOTH HID v0.15 Keyboard [MX Master 3] on 4c:77:cb:1d:66:d0
[ 63.902120] logitech-hidpp-device 0005:046D:B023.0006: HID++ 4.5 device connected.
[ 69.604899] input: MX Keys Keyboard as /devices/virtual/misc/uhid/0005:046D:B35B.0007/input/input37
[ 69.605221] input: MX Keys Mouse as /devices/virtual/misc/uhid/0005:046D:B35B.0007/input/input38
[ 69.606204] hid-generic 0005:046D:B35B.0007: input,hidraw4: BLUETOOTH HID v0.13 Keyboard [MX Keys] on 4c:77:cb:1d:66:d0
[ 188.285030] input: T9 (AVRCP) as /devices/virtual/input/input40

With the --follow flag, we can see in real time the new messages that are generated.

	
< > Input
Python
!dmesg --follow
Copied
>_ Output
			
[ 0.000000] Linux version 5.15.0-84-generic (buildd@lcy02-amd64-005) (gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0, GNU ld (GNU Binutils for Ubuntu) 2.34) #93~20.04.1-Ubuntu SMP Wed Sep 6 16:15:40 UTC 2023 (Ubuntu 5.15.0-84.93~20.04.1-generic 5.15.116)
[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.15.0-84-generic root=UUID=59002381-d88d-44a6-b83d-8c5a226ce058 ro quiet splash vt.handoff=7
[ 0.000000] KERNEL supported cpus:
[ 0.000000] Intel GenuineIntel
[ 0.000000] AMD AuthenticAMD
[ 0.000000] Hygon HygonGenuine
[ 0.000000] Centaur CentaurHauls
[ 0.000000] zhaoxin Shanghai
[ 0.000000] BIOS-provided physical RAM map:
[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009ffff] usable
[ 0.000000] BIOS-e820: [mem 0x00000000000a0000-0x00000000000fffff] reserved
[ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x0000000009d01fff] usable
[ 0.000000] BIOS-e820: [mem 0x0000000009d02000-0x0000000009ffffff] reserved
[ 0.000000] BIOS-e820: [mem 0x000000000a000000-0x000000000a1fffff] usable
[ 0.000000] BIOS-e820: [mem 0x000000000a200000-0x000000000a20bfff] ACPI NVS
[ 0.000000] BIOS-e820: [mem 0x000000000a20c000-0x00000000b8983fff] usable
[ 0.000000] BIOS-e820: [mem 0x00000000b8984000-0x00000000b8acdfff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000b8ace000-0x00000000b8c56fff] ACPI data
[ 0.000000] BIOS-e820: [mem 0x00000000b8c57000-0x00000000b9107fff] ACPI NVS
[ 0.000000] BIOS-e820: [mem 0x00000000b9108000-0x00000000ba55cfff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000ba55d000-0x00000000bcffffff] usable
[ 0.000000] BIOS-e820: [mem 0x00000000bd000000-0x00000000bfffffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000f8000000-0x00000000fbffffff] reserved
...
[ 35.916406] input: LogiOps Virtual Input as /devices/virtual/input/input34
[ 36.002064] input: LogiOps Virtual Input as /devices/virtual/input/input35
[ 63.879806] input: MX Master 3 as /devices/virtual/misc/uhid/0005:046D:B023.0006/input/input36
[ 63.879931] logitech-hidpp-device 0005:046D:B023.0006: input,hidraw3: BLUETOOTH HID v0.15 Keyboard [MX Master 3] on 4c:77:cb:1d:66:d0
[ 63.902120] logitech-hidpp-device 0005:046D:B023.0006: HID++ 4.5 device connected.
[ 69.604899] input: MX Keys Keyboard as /devices/virtual/misc/uhid/0005:046D:B35B.0007/input/input37
[ 69.605221] input: MX Keys Mouse as /devices/virtual/misc/uhid/0005:046D:B35B.0007/input/input38
[ 69.606204] hid-generic 0005:046D:B35B.0007: input,hidraw4: BLUETOOTH HID v0.13 Keyboard [MX Keys] on 4c:77:cb:1d:66:d0
[ 188.285030] input: T9 (AVRCP) as /devices/virtual/input/input40
>_ Output
			
^C

Hardware Informationlink image 75

With lshw we can see information about our computer's hardware

	
< > Input
Python
!lshw
Copied
>_ Output
			
AVISO: deberΓ­a ejecutar este programa como superusuario.
wallabot
descripciΓ³n: Computer
anchura: 64 bits
capacidades: smp vsyscall32
*-core
descripciΓ³n: Motherboard
id fΓ­sico: 0
*-memory
descripciΓ³n: Memoria de sistema
id fΓ­sico: 0
tamaΓ±o: 32GiB
...
producto: PnP device PNP0501
id fΓ­sico: 6
capacidades: pnp
configuraciΓ³n: driver=serial
*-pnp00:05
producto: PnP device PNP0c02
id fΓ­sico: 7
capacidades: pnp
configuraciΓ³n: driver=system
AVISO: la salida puede ser incompleta o imprecisa, deberΓ­a ejecutar este programa como superusuario.

Cowsaylink image 76

There is a command called cowsay that takes a text as a parameter and draws a cow saying that text.

It’s possible that you don’t have it installed, so to install it, you need to enter the command

sudo apt install cowsay
	
< > Input
Python
terminal("cowsay MaximoFN")
Copied
>_ Output
			
__________
< MaximoFN >
----------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||

If you add the -f dragon flag, the speaker is a dragon.

	
< > Input
Python
terminal("cowsay -f dragon MaximoFN")
Copied
>_ Output
			
__________
< MaximoFN >
----------
\ / \ //\
\ |\___/| / \// \\
/0 0 \__ / // | \ \
/ / \/_/ // | \ \
@_^_@'/ \/_ // | \ \
//_^_/ \/_ // | \ \
( //) | \/// | \ \
( / /) _|_ / ) // | \ _\
( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.
(( / / )) ,-{ _ `-.|.-~-. .~ `.
(( // / )) '/\ / ~-. _ .-~ .-~^-. \
(( /// )) `. { } / \ \
(( / )) .----~-.\ \-' .~ \ `. \^-.
///.----..> \ _ -~ `. ^-` ^-_
///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~
/.-~
	
< > Input
Python
terminal("cowsay -f dragon-and-cow MaximoFN")
Copied
>_ Output
			
__________
< MaximoFN >
----------
\ ^ /^
\ / \ // \
\ |\___/| / \// .\
\ /O O \__ / // | \ \ *----*
/ / \/_/ // | \ \ \ |
@___@` \/_ // | \ \ \/\ \
0/0/| \/_ // | \ \ \ \
0/0/0/0/| \/// | \ \ | |
0/0/0/0/0/_|_ / ( // | \ _\ | /
0/0/0/0/0/0/`/,_ _ _/ ) ; -. | _ _\.-~ / /
,-} _ *-.|.-~-. .~ ~
\ \__/ `/\ / ~-. _ .-~ /
\____(oo) *. } { /
( (--) .----~-.\ \-` .~
//__\\ \__ Ack! ///.----..< \ _ -~
// \\ ///-._ _ _ _ _ _ _{^ - - - - ~

Cleaninglink image 77

Since the prueba folder has been created, we delete it to leave everything as we found it.

	
< > Input
Python
!rm -r prueba
Copied

Continue reading

Last posts -->

Have you seen these projects?

Gymnasia

Gymnasia Gymnasia
React Native
Expo
TypeScript
FastAPI
Next.js
OpenAI
Anthropic

Mobile personal training app with AI assistant, exercise library, workout tracking, diet and body measurements

Horeca chatbot

Horeca chatbot Horeca chatbot
Python
LangChain
PostgreSQL
PGVector
React
Kubernetes
Docker
GitHub Actions

Chatbot conversational for cooks of hotels and restaurants. A cook, kitchen manager or room service of a hotel or restaurant can talk to the chatbot to get information about recipes and menus. But it also implements agents, with which it can edit or create new recipes or menus

View all projects -->
>_ Available for projects

Do you have an AI project?

Let's talk.

maximofn@gmail.com

Machine Learning and AI specialist. I develop solutions with generative AI, intelligent agents and custom models.

Do you want to watch any talk?

Last talks -->

Do you want to improve with these tips?

Last tips -->

Use this locally

Hugging Face spaces allow us to run models with very simple demos, but what if the demo breaks? Or if the user deletes it? That's why I've created docker containers with some interesting spaces, to be able to use them locally, whatever happens. In fact, if you click on any project view button, it may take you to a space that doesn't work.

Flow edit

Flow edit Flow edit

FLUX.1-RealismLora

FLUX.1-RealismLora FLUX.1-RealismLora
View all containers -->
>_ Available for projects

Do you have an AI project?

Let's talk.

maximofn@gmail.com

Machine Learning and AI specialist. I develop solutions with generative AI, intelligent agents and custom models.

Do you want to train your model with these datasets?

short-jokes-dataset

HuggingFace

Dataset with jokes in English

Use: Fine-tuning text generation models for humor

231K rows 2 columns 45 MB
View on HuggingFace β†’

opus100

HuggingFace

Dataset with translations from English to Spanish

Use: Training English-Spanish translation models

1M rows 2 columns 210 MB
View on HuggingFace β†’

netflix_titles

HuggingFace

Dataset with Netflix movies and series

Use: Netflix catalog analysis and recommendation systems

8.8K rows 12 columns 3.5 MB
View on HuggingFace β†’
View more datasets -->