0% found this document useful (0 votes)
27 views49 pages

Andos

The document discusses setting up a virtual machine environment for practicing Linux commands as a regular user cannot have root privileges on lab computers. It then provides an overview of basic Linux shell concepts like getting help, environment variables, the sudo command for superuser privileges, and commands for viewing files and searching file contents.

Uploaded by

spamtonkris
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views49 pages

Andos

The document discusses setting up a virtual machine environment for practicing Linux commands as a regular user cannot have root privileges on lab computers. It then provides an overview of basic Linux shell concepts like getting help, environment variables, the sudo command for superuser privileges, and commands for viewing files and searching file contents.

Uploaded by

spamtonkris
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Linux and commands

Computer Science Preliminaries


(B 2-3)

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.

• Work environment configuration:


– Follow the step-to-step guide at http://docencia.lbd.udc.es/IB/ :
1. Wifi labs: Install vmware-player, download and boot the virtual machine with Ubuntu-14.

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)

– Configured with: 1Gb RAM. 1 core-CPU, network adapter, and up to


20Gb of hard disk (initially, just 4.5Gb are in use, approx.)
– User with sudo permissions. User: ib & Password: ib
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
Some basic concepts: shell and environment,...
– There are several shells that we can use in a terminal, where we will type commands:
• sh, bash, csh, zsh, tcsh.
• By default we start with the “bash” shell (but it can be changed).

– To exit the shell: exit or CTRL+D


– To clean the terminal: clean and reset

– To get help about a command (and other elements):


• man
– man page
– man –k text
• command –h or command --help

– 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

• Some variables widely used:


– $HOME  Basis directory of current user.
– $PATH  Directories (separated by “:”) where the shell will look for executables
– $PWD  Directory of the shell where we currently are.
Some basic concepts: shell and environment,...
– Some basic commands
• ls lists the content of a directory (by default, the current directory)
• pwd shows the current directory
• which looks for an executable through $PATH directories.

Exercises: In a terminal…

1. Display the current directory


pwd
echo $PWD
2. Edit variable $PATH to also include the current directory (“.”)
echo $PATH
export PATH=$PATH:.
echo $PATH
3. Step-to-step…
• Display the path to the program that is executed when typing ls
which ls
• Delete variable $PATH, and check what happens if you type ls or which ls
unset PATH
ls
which ls
• Add the directory /bin to the variable $PATH, and check what happens if you type ls
export PATH=/bin $PATH or export PATH=$PATH:/bin
ls
4. Display the basis/home directory of the user currently logged in on the session.
echo $HOME
Some basic concepts: the shell
The super-user / root user (and the command sudo)
– System administrator.
– Many commands require to be executed in “root mode” (as superuser).
– A “non-root” user can be added to the group of administrators, and will be able to use the sudo
command.
• sudo: Execute commands in “root”. mode
• Example:
– ib@ib:~$ shutdown –r now
– shutdown: Es necesario ser superuser

– ib@ib:~$ sudo shutdown –r now


– [sudo] password for ib:
– ... the system is shutting down...

The su command allows


To open a shell as another user: su <user name>
– ib@ib:~$ sudo su user2
– user2@ib:~$

To open a shell as administrator (superuser) : su (no parameters)


– ib@ib:~$ sudo su

Shutdown/restart the system from command-line (requires superuser)


shutdown
– shutdown –h now (halt now)
– shutdown –h 20:10
– shutdown –h +10
– shutdown –r now (restart)
– shutdown –P now (poweroff: power off after shutdown, forces –h)
– shutdown –k now (warns users that the system is going to be shut down, but does not shut down).

reboot, halt, poweroff can be used to stop or restart the system


Some basic commands
cat - shows the contents of 1+ files
SYNOPSIS cat [OPTION]... [FILE]... cat
Options:
-b: number non-empty lines.
-n: number all lines

less & more - show the contents of files 1 page at a time


SYNOPSIS less/more [OPTION]... [FILE]...
Options: (many, see man)
less & more
head & tail
head - outputs (shows) the first part (10 first lines by default) of files
tail - outputs (shows) the last part (10 last lines by default) of files
SYNOPSIS head [OPTION]... [FILE]...
SYNOPSIS tail [OPTION]... [FILE]...
Options:
-c N display only the first N bytes of the file.
-n N display the first N lines (instead of exactly the first 10 lines)
grep - shows lines from 1+ file(s) matching a pattern (e.g. all lines containing a word)
grep [OPTIONS] pattern [FILE...]
Options: (many, ver man)
grep
Examples (assume that we are logged in as ‘ib’ user, and that we are located in $HOME)
1. Display the content of file .bashrc (/home/ib/.bashrc)
cat .bashrc
more .bashrc
less .bashrc
2. Display the content of file .bashrc (/home/ib/.bashrc) and number its lines.
cat -b .bashrc
3. Display the first/last 5 lines of file .bashrc
head -n 5 .bashrc ; tail -n 5 .bashrc
4. Display the contents of file .bashrc
cat .bashrc
5. Display the lines of file .bashrc containing the pattern "#"
grep '#' .bashrc
grep -n '#' .bashrc #precedes each line with a "line number"
6. TRICK: PIPE: '|' . We can send the output of a command/process to the input of another.
example: cat .bashrc | grep '#'
Text editors in a terminal
vi, nano, pico, emacs, ...
As its own name suggests, a text editor allows us to view/modifiy the content of a file.
There are many text editors that can be used in linux: vi, emacs, pico/nano, etc. You can get help using man and see
their options.
Among them, nano (or pico), is a very simple editor.

nano - Nano's ANOther editor, an enhanced free Pico clone


SYNOPSIS : nano [options] [[+line,column] file]...
Basic commands:
- CTRL+X --> [saves and] quit
- CTRL+O --> save to file
- CTRL+W --> looks for a pattern

Example: Edit the file a.txt (if it does not exist, it is created)
pico a.txt

editing the file CTRL+O


Some basic commands
Compare the contents of two files cmp & diff
cmp - compares two files byte by byte
SYNOPSIS cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
diff – compares files line by line
SYNOPSIS diff [OPTION]... FILES
Options: (see man)

Example: We have two files, a.txt and b.txt. ¿are they equal? (we can view their content with cat)

• cmp a.txt b.txt


 a.txt b.txt are different: byte 12, line 2
• diff a.txt b.txt
2c2
< -rw-rw-r-- 1 ib ib 0 sep 28 08:19 a.pas
---
> -r-------- 1 ib ib 0 sep 28 08:19 a.pas
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
File systems
Files: types

– A file is the basis of the file system:


• It stores information: an image, text, html,…
– It must have a name:
• 1..255 characters: Any character except “/”
• “Hidden” files start with “.”
• Case-sensitive!
a.txt and A.txt are different files
• Wildcards: *, ? ... when we want to use them (e.g.: ls *.txt or rm –f /home/??.txt)
– *: replaces 0+ characters ?: replaces 1 character
– Types:
• Ordinary files.
• Directories or folders: groups other files in a structured way.
• Special files: usually associated with devices connected to the computer:
– Block devices: hard drive or dvdrom (hda0,sda0),
– Character: a printer (lp0, psaux)…
• Others: links (symbolic/hard), pipes, sockets,…
– Permissions/ownership.
• Each file belongs to an owner and can have associated read, write and execution permissions
(executable file).
• Permissions assigned to: user | users in the group | other users
File systems
Files: permissions

– 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: ..

• Absolute path: from the root directory


– cat /home/ib/examples.desktop

• Relative path (from the current directory):


– cd /home/ib [enter] (changes to directory /home/ib)
– cat examples.desktop

• The home directory of a user is: ~


– cd ~ (changes to the home directory of the user,
e.g. to /home/ib, for user “ib”)
– cd ~/examples.desktop
File systems

F.S. structure: root directory and f.s. organization


– Everything starts at “/” (root directory)
File systems: basic commands

– There are many commands: use man to learn about them:


• Show the contents of a directory, information of a file,... (ls)
• Move in the file system: (cd | chdir)
• Know the current directory (pwd)
• Create/delete directories (mkdir, rmdir)
• Create empty files (touch, vi, nano | pico)

• Delete files and directories (rm, rm -r)


• Move/copy elements (mv, cp, cp -r).
• Create links (ln, ln -s)

• Manage partitions (fdisk/gdisk). Create fs in a partition | check for errors. (mkfs,fsck)


• Mount and unmount file systems (mount/umount)
• Show space usage (du, df).

• Manage permissions: (chmod, chown, chgrp)

• 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)

for more info see appendix


File systems: basic commands
ls - list directory contents
SYNOPSIS :
ls [OPTION]... [FILE]...
ls
DESCRIPTION
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort.
– Options:
-l  long format (includes permissions, user, group, size, modification_date...)
-h  shows the size in “human readable” format (kbytes, Mbytes,…)
-a  shows hidden files (entries that start with ‘.’)
-R  recursively list subdirectories
-r  use reverse ordering when sorting
-S  sort by file size
-t  sort by modification date

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

rmdir – remove empty directories


SYNOPSIS
rmdir [OPTION]... DIRECTORY... rmdir
DESCRIPTION
Remove the DIRECTORY(ies), if they are empty.
cd – change the current/working directory
SYNOPSIS
cd [dir]... or chdir [dir]...
cd - chdir
DESCRIPTION
Changes the current working directory to [dir]. If no [dir] is provided it moves to the
‘home’ directory of the current user.

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

Examples: (assume we are located in /home/ib)


Create the files a.pas, b.txt and c.dat in the current directory: touch a.pas b.txt c.dat
Create the directories /home/ib/TMP and /home/ib/TMP/subdir
Copy the file a.pas to the directory /home/ib/TMP cp a.pas TMP

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.

mv - move (rename) files


SYNOPSIS
mv
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... -t DIRECTORY SOURCE...
DESCRIPTION
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY
Options:
-u update, move only when SOURCE file is more recent than destination (DEST), or when DEST did not
exist.
Examples (assume that our user is ‘ib’ and that there are directory D and files a, b, c, d in $HOME)
1. Rename file a, so it becomes a.txt
mv a a.txt
2. Move files c and d to directory D
mv c d D
3. What does the command mv d DIR do?
4. Make a copy of file a.txt that is named a
cp a.txt a
5. Copy files c and d from directory D to the current directory (/home/ib)
cp D/c D/d . ## you can also type cd D, and then cp c d .., finally type cd ..
6. Copy directory D and all its content to /tmp/DIR (this directory does not exist yet)
cp -r D /tmp/DIR
File systems: basic commands
ln -s
ln - make links between files
SYNOPSIS
ln [OPTION]... [-T] TARGET LINK_NAME (1st form)
ln [OPTION]... TARGET (2nd form)
ln [OPTION]... TARGET... DIRECTORY (3rd form)
ln [OPTION]... -t DIRECTORY TARGET... (4th form)
DESCRIPTION
In the 1st form, create a link to TARGET with the name LINK_NAME.
In the 2nd form, create a link to TARGET in the current directory.
In the 3rd and 4th forms, create links to each TARGET in DIRECTORY. Create hard links by default, symbolic links
with (-s). When creating hard links, each TARGET must exist.
Symbolic links can hold arbitrary text; when resolved, a relative link is interpreted in relation to its parent dir.

Options:
-s creates symbolic link
tmp
...
novolink ib

Examples (assume we are located in /tmp )


1. Create a symbolic link called 'novolink‘ pointing to /home/ib
ln -s /home/ib novolink
2. Display the information about 'novolink'
ls -l novolink
shows: lrwxrwxrwx 1 ib ib 10 2018-09-22 16:28 novolink -> /home/ib
3. Follow the link, and display the content of the pointed directory
ls -l novolink/
we can also type: cd novolink, and then ls -l .
4. Delete the symbolic link just created
rm /tmp/novolink

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 are located in /usr )


1. Find files named stdio.h with permissions 644 that belong to user root
find -name stdio.h -perm 644 -user root
2. Find files with extension .c in /tmp
find /tmp -name *.c -type f

locate - find files by name


SYNOPSIS
locate [OPTION]... PATTERN...
locate (& updatedb)
DESCRIPTION
locate reads one or more databases prepared by updatedb(8) and writes file names matching at least one of the PATTERNs to
standard output, one per line.
If --regex is not specified, PATTERNs can contain globbing characters. If any PATTERN contains no globbing characters, locate
behaves as if the pattern were *PATTERN*.
By default, locate does not check whether files found in database still exist. locate can never report files created after
the most recent update of the relevant database.  It needs UPDATEDB to be called to update the DB that it uses!!

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

Storage devices and partitions.


– A hard drive is a storage device that can contain up to 4 primary partitions**, and one of them can be
extended (so it can contain multiple logical partitions). [**GPT  up to 128]
– Where are the system devices?  /dev
• /dev/hda, /dev/hdb, /dev/hdc, ...  IDE devices (hdd, cdrom, dvdrom...)
• /dev/sda, /dev/sdb, /dev/sdc, ...  SCSI, SATA devices (hdd, hdd-usb, dvdrom...) (ex: ls –l /dev/sd* )
E. g: 1st SATA device: /dev/sda, 2nd SATA device: /dev/sdb
– Naming of partitions.
• /dev/hda1, /dev/hda2,...  partitions 1, 2 of the first IDE device
• /dev/sda1, /dev/sdb1,...  1st partition of the 1st and 2nd SATA devices

The first disk of the system (/dev/sda) hat 3 partitions:


/dev/sda1
Disk 1
sda

/dev/sda2, that contains the root file system “/”


/dev/sda3 that contains a logical partition /dev/sda5

The second disk of the system (/dev/sdb) has 1 partition:


Disk 2

/dev/sdb1, that contains the home directories (/home)


sdb

The fourth device of the system (/dev/sdd) has 1 partition:


Disk 4

/dev/sdd1, it is in this case a pen drive with an “NTFS”


sdd

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).

mount [options] <device> <mounting point>


-t (type), -o (ro, rw, ... that is: readonly, readwrite)
• mount -t ext4 /dev/sda1 /mnt/debian
• cd /mnt/debian/home
debian

umount <device> or umount <mounting point>


• umount /mnt/debian, or also
• umount /dev/sda1 /dev/sda3

What is currently mounted? (several ways to know)


– mount (without parameters),
– df or df -h
– cat /etc/mtab (this file stores information about the partitions that are mounted and how)
Safely disconnect a USB device  umount!! /dev/sda1

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

lsblk – list block devices


SYNOPSIS lsblk
lsblk [options]
lsblk [options] device...
Options:
-f shows info about file systems.
-o specify which columns (data) are shown

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)

du - estimate file space usage


SYNOPSIS du [OPTION]... [FILE]...
[SIZE OF FILE OR DIRECTORY]
du
Options:
-m sizes in megabytes
-s shows only summary statistics
--max-depth=n for directories shows space usage for the next n-levels

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

• sudo fdisk /dev/sda (to see/mount partitions of /dev/sda)


– p  print state
– n  new partition (/dev/sda1, /dev/sda2...)
– d  delete partition
– t  change partition type (ext4, swap,...)
– w  write partition table to disk (changes are performed, cannot be “undone”!!)
– q  exit (discarding changes)
– h  help
(f.s. types.: ext4, ext3, ntfs, vfat, linux swap,...)

– Creation of a f.s. (formatting)


• mkfs (mkfs.ext4, mkfs.ext3, mkfs.ntfs,...)
– sudo mkfs –t ext3 /dev/sdd1 equal to sudo mkfs.ext3 /dev/sdd1
(creates an “ext3” file system in device sdd1 (1st partition of disk 4)
– sudo mkfs –t ext4 /dev/sda2 equal to sudo mkfs.ext4 /dev/sda2
(creates an “ext4” file system in device sda2 (2ªnd partition of disk 1)
File systems: mounting, /etc/fstab
The file/etc/fstab
– Determines which file systems will be mounted by default on startup, or simply establishes
which f.s. is mounted and/or where to mount a specific device.

– 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

– Error checking in a f.s.


• It cannot be mounted.
• fsck [–f] /dev/device […]

– Ex: fsck –f /dev/sda3  Option “–f” forces the check even if the f.s. is “clean”

– Disable swap, configure partition as swap, enable swap.


• swapoff –a  disables swap
• free  shows free memory (and available swap)
• mkswap /dev/sda2  configures a linux swap area in /dev/sda2
• swapon –a  enables swap areas again
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
User management: users and groups

– Linux is a multiuser O.S.


• Each user has privileges that define what they can do.
• Each user can be included in a group (that has privileges itself), so the user also enjoys the group’s privileges
• A user will have (among others): a user id, a group or more to which he/she belongs (e.g.. users, admin), a
home directory $HOME, a password, and a shell that will be used by default.
– The files that store this information are: /etc/passwd and /etc/shadow (encrypted password)

– Creation of new users and groups


– adduser <user>  uses default configuration to create account user (/etc/login.defs, /etc/skel*, /etc/shells)
– addgroup <group>  creates group <group>
– adduser <user> <group>  adds <user> to <group>

Files related with user accounts:


• examples /etc/passwd: information about each user: ID, main group, description, home directory,
» adduser pepe shell, etc. Also the encrypted password, unless shadow passwords are used.

» 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

– Passwd: Change the password of a user.

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)

• sudo passwd user  changes password of user “user”.


Notice that when root calls (sudo) passwd user, the old password is not requested, because
root is assumed to have permission to change/reset passwords.

• Examples:
– passwd –d ib  deletes password of user “ib”
– passwd ib
User management: owner and group of a resource

– We have previously mentioned that a file has (ls -l ficheiro):


• a set of permissions (see chmod) that allow changing the permissions to the file’s owner, to its group and to the
rest of the users.
• an owner that can be changed by using chown.
• a group, than can be changed by using chgrp (and also with chown).

chown - change file owner and group


SYNOPSIS chown [OPTION]... [OWNER][:[GROUP]] FILE... chown
chgrp - change group ownership
SYNOPSIS chgrp [OPTION]... GROUP FILE... chgrp
Options:
-R Apply changes recursively

Example: Given the file -rwxr--r-- 1 ib ib 98 2018-09-27 13:07 a.txt


and assuming that there are: a group grp2, and a user pepe

1. Change the owner of file a.txt, to 'pepe'


chown pepe a.txt
2. Change the owner and group of a.txt, and set 'pepe‘ as its owner and 'grp2‘ as its group
chown pepe:grp2 a.txt
3. Change the group of a.txt, to ‘grp2’
chgrp grp2 a.txt
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
Processes

– In the system there are many processes:


• System processes (e.g. init), and processes associated to services (daemons) such as sshd, httpd,inetd, etc.
• Users processes, associated to programs that users are executing at a given moment.
• A process has an identifier (PID), a parent (PPID), and uses resources (CPU, memory, etc.)
• A process can be in different states: executing, ready for execution or waiting.

– 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)

– Some typical commands related to process management :


• ps: e.g. $ps -l -u ib shows processes of user “ib”).

• 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)

– fg %[id.task] brings task to foreground (it takes control of the shell).


• If it was not running, it starts executing
example: fg %1
– bg %[id.task] sends a previously suspended task to background
• If it was stopped, it will remain stopped until it receives a signal to “continue”.
– Signals: kill allows sending signals to a process (not only to kill it)
• Kill a process with PID=2563 • Stop a process with PID=2563
– CTRL+C kills the active process – CTRL+Z sends the sleep signal
– Equal to kill -9 2563, or kill -KILL 2563 – Equal to kill -STOP 2563
• Continue execution of process with PID= 2563
– kill –CONT 2563
Processes: exercises
Examples:

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"

5. What are the active tasks?


jobs -l

6. What is the CPU use of process “yes” launched by the script?


top -c
Processes: exercises
Examples:

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

9. Kill that process


kill -9 16389
jobs

10. What are the active tasks? and the processes?


jobs -l ps -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

– There are 3 special files in /dev: stdin, stdout, stderr


– Any process can refer to them:
• stdin  identifier 0  standard input (keyboard)
• stdout  identifier 1  standard output (screen/terminal)
• stderr  identifier 2  standard error output (screen/terminal)
– We can redirect these “special outputs” to another file.
– ls / > listado.txt (equivalent to the next one)
– ls / 1> listado.txt (show the contents of the root folder, store it in file “listado.txt”).
– ls / >> listado.txt (does not overwrite the file “listado.txt”. Appends to the end of the file).
– ls /xaxbxc.txt
Shows ls: /xaxbxc.txt: file or directory does not exist
– ls / /xaxbxc.txt 1>listado.txt 2>erros.txt
» will write to file “listado.txt” the list of files in the root folder.
» will write to file “erros.txt” the text “ls: /xaxbxc.txt: file or directory does not exist”
– We can also redirect standard input “from a file”
– more < “listado.txt” (shows in the screen, page by page, the contents of “listado.txt”)
– Pipes: We can send the output of a process to the standard input of another.
– ls /dev | more (sends the output of “ls /dev” to the command more to show info page by
page)
– ls -R /home | grep .txt (grep filters lines containing “.txt” from those returned by “ls –R /home”)
– cat ~/.bashrc | grep -n if (grep displays the lines of .bashrc containing the string “if”)
– find / -name sdtin | more
– find / -name *ib* | more
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
• Apt, Synaptic, Ubuntu Software Center
– Networks: basic commands
Installation of packages/applications
Applications are usually in repositories (see /etc/apt/sources.list )
– Main: free software(modification and redistribution is generally allowed). Guaranteed support
– Restricted: soft used frequently but not “completely free”. Needed in some systems (e.g. drivers)
– Universe: freely-distributed code, without canonical support... if very popular  main.
– Multiverse: non-free. Users can install it under their responsibility (license? Support not available)

Installation from repositories


– apt:
• apt-get
install <package>
update
upgrade
dist-upgrade (global system upgrade)
remove <package>
autoremove
• apt-cache search <package>

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

– ftp (file transfer protocol). Conexión non encriptada (porto 21).


ftp <host> , ftp usuario:password@host
Comandos dispoñibles dentro durante o ftp
• open <host | dirección ip> : conéctase ao host para baixar/subir ficheiros
• ls, cd, pwd (operacións no host remoto)
• lls, lcd, lpwd (operación no host local)
• put <ficheiro> . Sube ficheiro local ao host remoto
• get <ficheiro> . Descarga ficheiro remoto ao host local
• mput <ficheiros>, mget <ficheiros> . Sube|baixa 1 ou máis ficheiros (* é permitido).
• quit . Cerra a sesión
– sftp (secure ftp). Conexión porto 22
• Similar ao ftp, pero a conexión é encriptada.

– ping <host>
• Mira se unha máquina remota é accesible.

You might also like