Andos
Andos
Databases Laboratory
Previous: WIFI access
• To access the UDC network from your laptop you
must configure the WIFI connection.
• More información:
https://axudatic.udc.gal/display
/SIC/Rede+wifi+eduroam
https://www.udc.es/es/bibliotec
a.comsoc/Servizos/wifi/
Outline
– Work environment
– Linux shell:
• Basic concepts.
– Getting help, the environment, basic commands
– Super-user (root). sudo.
– File systems.
• Files and directories
• Structure: the path.
• Permissions
• Related commands
– Users and groups
• Privileges
– Processes
• Execution in foreground and background, termination, wait
• Redirects and pipes.
– Software installation and packages
– Networks: basic commands
Work environment (I)
• We will learn basic notions about linux commands. In some
cases we will need to work with special administration
permissions (root).
• For security reasons, we cannot be administrators of the
lab computers. We are regular (unprivileged) users.
• We need an environment where we can work as system
administrators, so we should have installed our own Ubuntu
(or another linux distribution)
Work environment (II)
• Solution: Virtual machine technology
1. A special software (in this case, Vmware (versión vmware-player *))
emulates a real computer, where we can install one or more operating
systems.
*Vmware-player (free version of Vmware-workstation for “non-commercial use”)
2. Download a preconfigured virtual machine with Ubuntu-14 operating
system already installed.
NOTA, students must go to the practice class of commands with the virtual machine
already downloaded, and running!!
Work environment (III)
• Vmware virtual machine running Ubuntu 14.04-5 (32bits)
– The environment:
• There are several environment variables (HOSTNAME, PATH, HOME,TERM,...) that can be modified. New
variables can also be added to the environment (export var=valor)
– export: export with no parameters shows the content of all the variables.
export PATH=$PATH:/home/ib/bin ## changes the PATH adding to it /home/ib/bin
export X=“value of variable X” ## creates variable X
– unset PATH unset X ## removes variables PATH and X
– echo $PATH echo $X ## shows the content of variables $PATH and $X
– echo $HOME ## shows the content of variable $HOME
Note that to refer to a variable we write a $ before the name. Example: cd $HOME
Exercises: In a terminal…
Example: Edit the file a.txt (if it does not exist, it is created)
pico a.txt
Example: We have two files, a.txt and b.txt. ¿are they equal? (we can view their content with cat)
– Files have:
• A name,
• An owner and group (see chown and chgrp)
• A type (file ‘-’, directory ‘d’, link ‘l’, block device ‘b’ , character device ‘c’, ...)
• A size, modification date, creation date
– A set of permissions: (see chmod)
• Read (r), write (w), execution (x)
• Associated to the file owner
root has permissions on
all files
• Associated to the users in the same group as the file
• Associated to other users (other than the owner and the group members).
permissions group
owner
permissions user
permissions others
type
modification
group
size
name
date
Ex:
chmod +w .bashrc
chmod 644 .bashrc
chmod 770 .bashrc
File systems
Files: permissions chmod
A file has permissions for user, group, and others. By typing ls -l we can see:
-rwxr--r-- 1 ib ib 98 2012-09-27 13:07 a.txt
chmod can change those permissions in two basic ways ( see man chmod ):
SYNOPSIS Options:
chmod [OPTION]... MODE[,MODE]... FILE... -R [r]ecursive. Changes files and directories
chmod [OPTION]... OCTAL-MODE FILE... recursively.
1. [ugoa...][[+-=][rwx...]…]
[ugoa...] specifies respectively: [u]ser, [g]roup, [o]thers, [a]ll. Permissions are applied to user, group,
others, or all of them (all).
[+-=] determines if permissions are added (+), removed (-) or set to a value (=)
[rwx…] manages read (r), write (w), or execute (x) permissions.
Note that other permissions exists (s, X, t) but we will not describe them here.
2. Formato 0777 (octal digits), where each digit is referred to [u]ser, [g]roup, [o]thers
Note: The first digit is related to other permissions s,X,t (that we do not address here)
Each octal digit can take values [0..7], and represents 3 bits in binary:
ex: 5 = 101 is equivalent to permission r-x
7 = 111 is equivalent to permission rwx
Examples:
Initial permissions Call to chmod (on file F) Final permissions
---------- chmod 760 F chmod u+rwx,g+rw F -rwxrw----
-r-xr-xrw- chmod 740 F chmod u+w,g-x,o-wr F -rwxr-----
---------- chmod 644 F chmod a+r,u+w F -rw-r--r--
File systems
Files: permissions chmod
Exercises: In a terminal
1. Create a file proba.txt, and set the permissions to rxw------. Check that the permissions have been
properly granted.
touch proba.txt or echo “contido do ficheiro” > proba.txt
chmod 700 proba.txt
ls –l proba.txt
2. Change the permissions of proba.txt so that all system users can read it.
chmod 744 proba.txt or chmod a+r proba.txt
ls –l proba.txt Note: permissions must be rwxr--r--
3. Change the previous permissions so that the owner and the users of the group of proba.txt can edit or
delete the file.
chmod 764 proba.txt or chmod g+w proba.txt
ls –l proba.txt Note: permissions must be rwxrw-r--
4. Change the previous permissions so that only the owner of proba.txt can access (read and write) the
file, but nobody can execute it.
chmod 600 proba.txt or chmod u-x, g-rw, o-r proba.txt
ls –l proba.txt Note: permissions must be rw-------
5. Create a new file x.txt and set its permissions by using chmod 540. ¿Do we need to do something so
that the owner and the users of the group of x.txt could delete this file?
As the current permissions are r-xr----- and we want to have the permissions ?w??w----, we must
grant write permissions to the owner and the group:
chmod 760 x.txt or chmod u+w,g+w
ls –l x.txt Note: permissions must be rwxrw-----
File systems
tmp
Hierarchical structure: the PATH
ib
– The F.S structure is hierarchical
• Typically it is a tree structure (directories/subdirectories/…)
• There are also links (so it becomes a graph)
– A file can be in the root (/) folder, or lower in the directory hierarchy. The path from the
root to the file is called PATH of the file.
• /home/ib/Carta.txt
– Current directory: relative VS absolute paths
• The root directory is: /
• The current directory is : . (pwd gives us the path to the current directory.)
• The parent directory is: ..
• For text files: contents (cat, more, less, head, tail), statistics (wc)
• Compare 2 files: (diff, cmp)
• Search inside a file (grep)
• Search location of files (whereis, which, locate, updatedb)
• Package | compress files and directories (tar, gzip, bzip2, p7zip,...)
• Search files that match a pattern (find –name “modelo“, -perm 777 –user ib)
Examples:
1. Display the content of the current directory
ls ; ls . ; ls –l ; ls –l .
2. Display the content of directory /tmp (in long format and sorting the files by size)
ls -lS /tmp
3. List all the entries of /etc whose name start by ‘host’
ls /etc/host*
4. Recursively list the content of directory HOME of the current user
ls -R $HOME ; ls -R ~
5. Display information about the file /etc/password
ls -l /etc/passwd
File systems: basic commands
mkdir - make directories
SYNOPSIS
mkdir [OPTION]... DIRECTORY...
mkdir
DESCRIPTION
Create the DIRECTORY(ies), if they do not already exist.
Options:
-p create also parents if they did not exist
Examples: (assume that our user is ‘ib’ and that our working directory is /home/ib)
1. Create directory DIR in /tmp
mkdir /tmp/DIR
2. Try to créate directory /tmp/DIR/DIR2/subdir by typing mkdir /tmp/DIR/DIR2/subdir
You’ll see an error: mkdir: the directory «/tmp/DIR/DIR2/subdir» can not be created: File or directory does not exist
3. Create directory /tmp/DIR/DIR2/subdir by using the necessary option to also create the intermediate directories
mkdir -p /tmp/DIR/DIR2/subdir
4. Try to delete directory /tmp/DIR by typing rmdir /tmp/DIR
5. Delete directory /tmp/DIR/DIR2/subdir
rmdir /tmp/DIR/DIR2/subdir
6. Change the current directory to ‘/home/ib’
cd ; cd ~ ; cd /home/ib ; cd $HOME
7. Change the current direcotyr to ‘/tmp’
cd /tmp
8. Once located in /tmp, change again the current directorty to ‘/home/ib’
cd ../home/ib ; cd /home/ib ; cd ; cd ~ ; cd $HOME
File systems: basic commands
rm - remove files or directories
SYNOPSIS
rm [OPTION]... FILE...
rm | rm -r
DESCRIPTION
rm removes each specified file. By default, it does not remove directories.
Options:
-r recursively delete subdirectories
-f force, ignore non-existing files (do not show error) and do not ask for confirmations
-i interactive, ask for confirmation before deleting each file
-l ask for confirmation before deleting 3 or more files, or if –r option is enabled
1. Delete files a.pas, b.txt, and c.dat from the current directory
rm a.pas b.txt c.dat
2. Delete directory /home/ib/TMP and all its content
rm -rf TMP ; rm -rf ~/TMP
3. Delete all files with extensión .dat from directory /home/ib
rm /home/ib/*.dat ; [if we are already in /home/ib you can type rm *.dat]
4. Without being logged as “root”, delete all the content of directory /tmp ¿Is there any error?¿Why?
rm -rf /tmp/* ; [note that /tmp could contain files from other users]
File systems: basic commands
cp - copy files and directories
SYNOPSIS
cp [OPTION]... SOURCE... DIRECTORY
cp
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... -t DIRECTORY SOURCE...
DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY. Target files can be overwritten if existed
Options:
-u [u]pdate, move only when SOURCE is more recent than destination (DEST), or if DEST did not exist.
-r [r]ecursive. Copy directories recursively
-i [i]nteractive. Ask for confirmation before overwriting an existing file.
-n do not overwrite existing files in destination.
Options:
-s creates symbolic link
tmp
...
novolink ib
Note: ln also allows creating hard links (which will not be explained here).
File systems: basic commands
find
find - search for files in a directory hierarchy
SYNOPSIS
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]
DESCRIPTION
GNU find searches the directory tree rooted at each given file name by evaluating the given expression from left to right,
according to the rules of precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and
operations, true for or), at which point find moves on to the next file name. [...]
Options:
-maxdepth N: descend at most N levels from the source directory specified.
-name patrón: check that the name of the file/directory matches the pattern (*r.txt,...)
-mtime N: file modified in the last N*24 hours
-size N [cwbkMG]: the file uses N units of space. c=byte, b=512bytes, k=1kbyte, M=1Mbyte, G=1Gigabyte.
-type X: file type d=directory, f=regular file, l=symbolic link, and others(b,c,p,s,D)
-user uname: the owner is user uname.
-perm permisos: permissions match those specified(p.ex. 777)
...
Examples (assume we already typed sudo updatedb, to update the information used by locate)
1. List files whose path contains 'ps'
locate *ps*
2. List files whose extension is '.pdf'
locate *.pdf
File systems: partitioning, formatting
file system
File systems: mount, umount. Mounting point
Mount/umount f.s..
– Before accessing a f.s. we need to mount it to make it accessible. This requires selecting a point in the
file system (a directory) that will be called mounting point (e.g. /mnt/pendrive). The mounting point
will be the path from which the root of the mounted f.s. will be accessed (e,g, /mnt/pendrive/windows
accesses c:\windows, if the mounted f.s. is unit c: of a Windows partition).
We force writing to disk all modified data that could still be in memory buffers: buffer cache!!
we avoid data loss and f.s. inconsistencies.
File systems: mount / umount
Examples: assume that /dev/sda2 is the directory / (root) of our Linux installation
/dev/sda (disk 1: SSD - Solid State Drive) /dev/sdb (disk 2: SATA Hard Disk)
1. Mount the file system located at the first partition of the second disk in directory /media/disk3tb
sudo mkdir /media/disk3tb
sudo mount -t ext4 /dev/sdb1 /media/disk3tb
2. We can check that the hard disk was properly mounted:
df -h | grep sdb1
mount | grep sdb1
3. Umount the file system previously mounted
sudo umount /media/disk3tb
sudo umount /dev/sdb1
4. Mount the file system located at the third partition of the first disk in /mnt
sudo mount -t ext4 /dev/sda3 /mnt
File systems: basic commands
Disk space usage and storage devices
ib@server:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 1,4T 0 disk
└─sdb1 8:17 0 1,4T 0 part /home
sda 8:0 0 465,8G 0 disk
├─sda1 8:1 0 94M 0 part /boot/efi
├─sda2 8:2 0 18,6G 0 part /
├─sda3 8:3 0 59,6G 0 part [SWAP]
└─sda4 8:4 0 387,4G 0 part /media/HDD1.4
sdc 8:32 0 2,7T 0 disk
└─sdc1 8:33 0 2,7T 0 part /media/HDD3.1
Example:
1. Display information about the storage devices. For each existing file system, show the corresponding
device, mounting point and information about the available space.
lsblk
lsblk -o NAME,TYPE,FSTYPE,UUID,MOUNTPOINT
File systems: basic commands
Disk space usage?
df - report file system disk space usage [SIZE OF FILE SYSTEMS]
SYNOPSIS df [OPTION]... [FILE]... df
Options:
-h human readable: print sizes in powers of 1024 (e.g., 1023M)
Example:
1. Display the space used by all the mounted file systems
df -h
2. Display the space used by /dev/sda1
df -h /dev/sda1
3. Display the space used by $HOME
du -s $HOME
4. Display the space used (in MB) by the root directory and its subdirectories
sudo du --max-depth=1 -m /
File systems: partitioning, formatting
Disk 1
– Partitioning:
• The fdisk command allows the manipulation of partition tables MBR to see/create/modify
existing partitions in an storage device. Use gdisk to manage GPT partitions
– We can replace /dev/sd?? in /etc/fstab by an UUID or Unique identifier of the partition. The UUID does
not change even if we add new devices to the computer, or if we modify the order of disks Uniquely
identifies a partition. Command blkid allows us to obtain the UUID for a device.
– [u]mount <mounting point> (do not need to specify a mode, device, etc., because it is in fstab)
• $mount /mnt mount partition /dev/sda3 (it has UUID =“06 ….d7”, and type ext3) in directory /mnt
• $umount /mnt unmounts partition /dev/sda3
– mount –a and umount –a mount/unmount all the devices described in /etc/fstab (or /etc/mtab for
umount).
File systems, others: check f.s. / swap
– Ex: fsck –f /dev/sda3 Option “–f” forces the check even if the f.s. is “clean”
» adduser pepe admin /etc/shadow: encrypted passwords of users, if shadow passwords are used.
/etc/group: members in each group
» cat /etc/passwd | grep pepe
/etc/skel: directory that contains the default content for new users
» addgroup pepecolegas
» adduser pepe pepecolegas ; adduser root pepecolegas ; adduser ib pepecolegas
» cat /etc/group | grep pepecolegas (shows the following) “pepecolegas:x:1001:pepe,root,ib”
» deluser pepe ; delgroup pepecolegas deletes user pepe and group “pepecolegas”
User management: password change
A connected user (in a terminal) can change his/her password with command:
• passwd changes the password of the current user (asks for the old password)
• Examples:
– passwd –d ib deletes password of user “ib”
– passwd ib
User management: owner and group of a resource
– Some special key shortcuts (when a process is being executed on the current terminal):
• CTRL+C, CTRL+Z, CTRL+D (kill/sleep a process, close session, respectively)
• pstree
• top: shows processes that use more CPU time.
(top –c shows the complete command). press “q” to exit.
• kill <signal> PID: example $kill -9 1266
• killall <process> example $killall a.out
Processes in foreground or background
– By default processes launched in a shell are started in foreground, but they can be sent to
background starting them with “&”
• ls –l > a.txt foreground
• ls –l > a.txt & background
– jobs. Shows tasks. Basically, processes that are running (in background) in a terminal. It also shows
the process state.
ib@ib:~$ jobs -l (also shows pid)
1. Create an executable script named infinito with the following content yes $1 $2 $3 > /dev/null
echo 'yes $1 $2 $3 > /dev/null' >infinite
chmod +x infinite
2. Open "nano“ editor to edit file x.txt, and then press CTRL+Z; do the same to edit y.txt
nano x.txt
CTRL+Z
nano y.txt
CTRL+Z
3. Execute script infinito in “background”
./infinite first&
4. What are the processes running in the terminal?
ps -l
##note there is a new bash that launched
##process "yes"
7. Kill the process corresponding to "nano x.txt", and then execute commands jobs and ps -l
kill -9 16382
jobs ps -l
8. Stop the execution of the parent of the process “yes” (bash). Display the tasks by using jobs
kill -STOP 16388
jobs -l
11. Bring "nano y.txt“ to foreground, try to edit, and exit with (CTRL+X)
fg %2
12. Launch in background two processes "yes" with command: yes 1st >/dev/null& yes 2nd >/dev/null&
13. Check that two "yes“ processed were created. Kill those processed by using the command killall.
jobs ps -l
killall yes
Redirects and pipes
Examples:
1. Check packages updates
sudo apt-get update
2. Install packages updates
sudo apt-get upgrade
3. Install text editor 'geany'
sudo apt-get install geany
4. Remove text editor 'geany'
sudo apt-get remove geany
5. Look for package installing ssh server
apt-cache search geany
Outline
– Work environment
– Linux shell:
• Basic concepts.
– Getting help, the environment, basic commands
– Super-user (root). sudo.
– File systems.
• Files and directories
• Structure: the path.
• Permissions
• Related commands
– Users and groups
• Privileges
– Processes
• Execution in foreground and background, termination, wait
• Redirects and pipes.
– Software installation and packages
– Networks: basic commands
• Remote sessions, files transfer. Other commands
Redes: algúns comandos de interese.
– hostname. Indica o nome do computador ao que estou conectado.
– ssh <host>
• conexión a terminal remoto “encriptada”
- Exemplos:
- ssh host :: o usuario actual intenta abrir unha sesión remota. Solicítaselle password.
- ssh usuario@host :: intento de abrir unha sesión con usuario “usuario” na máquina “host”.
O host remoto solicitará tamén unha password.
- ssh –l usuario host :: equivalente á anterior
– ping <host>
• Mira se unha máquina remota é accesible.