System Administration
Linux Disk and File
Management
Thái Minh Tuấn -
[email protected]Slides are adapted from:
[1] Slides prepared by Prof. Brian D. Davison (http://www.cse.lehigh.edu/~brian/)
[2] The Practice of System and Network Administration, 3rd Ed., by Limoncelli, Hogan, and Chalup (Addison Wesley, 2017) 1
[3] Practical Linux System Administration: A Guide to Installation, Configuration, and Management, by Kenneth Hess (O'Reilly Media, 2023)
Adding a disk to Linux
● Install new hardware
○ verify that hardware is recognized by BIOS or controller
● Boot, make certain device files already exist in /dev
○ e.g., /dev/sdc
● Use fdisk/parted (or similar) to partition the drive
○ Verify the system type on each partition
● Use mke2fs (-t ext4) on each regular partition
○ create (an ext4) filesystem
● Add entries to /etc/fstab
● Mount by hand, then reboot to verify everything
2
Disk partitions
● Drives are divided into one or more partitions that are treated
independently
○ Partitions make backups easier, confine damage
● Typically have at least two or three
○ root partition (one)
■ everything needed to bring system up in single-user mode (often copied onto
another disk for emergencies)
○ swap partition (at least one)
■ stores virtual memory when physical memory is insufficient
○ user partition(s)
■ home directories, data files, etc.
○ boot partition - boot loader, kernel, etc.
3
Logical Volumes
● Partitions are static, and sometimes you want to change them
● LVM (Linux Logical Volume Manager) lets you combine partitions and
drives to present an aggregate volume as a regular block device (just like a
disk or partition)
○ Use and allocate storage more efficiently
○ Move logical volumes among different physical devices
○ Grow and shrink logical volume sizes on the fly
○ Take “snapshots” of whole filesystems
○ Replace on-line drives without interrupting service
● Similar systems are available for other OSes
4
Filesystems
● Linux filesystems are created in partitions or volumes
○ ext2fs (2nd Extended File System) is old
○ ext3fs (3rd Extended File System) is common
■ Augments ext2fs to incorporate journaling
● Journals contain filesystem updates
● Journal log can reconstruct consistent filesystem
● Journal speeds filesystem consistency checks
○ ext4fs (Fourth Extended File System) is modern
■ Speeds large directories
■ Compatible with ext2 and ext3
○ Other filesystems also supported
■ ReiserFS, IBM's JFS, SGI's XFS
○ Can read foreign filesystems (e.g., FAT, NTFS, ISO 9660)
5
ext# filesystems
● For ext2/ext3/ext4, mke2fs is used, which creates
○ A set of inode storage cells
■ each holds info about one file
○ A set of scattered “superblocks”
■ holds global filesystem info (multiple copies for reliability)
■ size and location of inode tables, block map and usage, etc.
○ A map of the disk blocks in the filesystem (used and free)
○ The set of data blocks
6
Mounting a filesystem
● Filesystem must be mounted before use
○ Must be made part of root filesystem
● Can be mounted on (top of) any directory
○ mount /dev/sda1 /usr/local
○ df /usr/local
● Use /mnt for temporary mounts
● Want to set up automatic mounting
7
/etc/fstab
● (Almost) every filesystem that the system knows about automatically is in
/etc/fstab
8
[u]mounting, swap
● mount, umount, swapon and fsck all read the /etc/fstab
file
● Enables
○ # mount /mnt/cdrom
● fstab entries must be in the correct order
● at startup
○ mount -a executed, mounts all regular partitions
○ swapon enables swapping on all swap partitions
9
The Filesystem
● A filesystem incorporates:
○ A way of naming and and organizing things (namespace)
○ An API for navigating and manipulating objects
○ A security model for protecting, hiding, and sharing objects
○ An implementation to tie the model to the hardware
● Linux abstract kernel interface supports many different
filesystems
○ from disk, network, memory
10
Pathnames
● The Linux filesystem is a single unified hierarchy, starting with /
(the root directory)
● A pathname can be
○ Absolute
■ /etc/passwd
○ Relative
■ ./passwd
■ Always starts with current working directory
● No technical limitations on file naming other than length and /
○ some chars are more difficult to use (need quotes or escape)
11
Mounting & unmounting filesystems
● The filesystem is made of smaller filesystems
● Most filesystems occupy disk partitions
○ but can be anything that obeys the API
● Filesystems may be added or removed using the mount
and umount commands
○ The mount point is a directory
○ Ex:
■ # mount /dev/hdc1 /backup
12
[u]mounting filesystems
● List of filesystems is in /etc/fstab
○ Such filesystems are checked (fsck -A) and mounted (mount -a) at boot
● umount will fail if the filesystem is busy
○ busy = any open files, processes with cwd, or copies of executing
programs
○ /sbin/fuser will show such processes
■ f – file open for reading or writing
■ c – process cwd is on filesystem
■ e – process is executing a file
■ r – process root dir is on filesystem
■ m – process has mapped file or shared lib
13
File tree organization
● Not really well organized
● Many files organized by function
○ difficult to upgrade
○ /etc/ contains files that are never customized, and ones that
are entirely local
● There is at least one place for everything
● Admins need to learn standard places, not move or use
new ones
14
Filesystem hierarchy (http://www.pathname.com/fhs/)
15
/usr, /var
16
File types
● Linux defines seven types of files
○ [-] - Regular files
○ [d] - Directories
○ [c] - Character device files
○ [b] - Block device files
○ [s] - Local domain sockets
○ [p] - Named pipes (FIFO)
○ [l] - Symbolic links
● ls -ld shows the filetype of a file
17
Linux File Permissions
● Linux file permissions (modes)
○ Read (r): View a file or list directory contents.
○ Write (w): Create and modify a file or copy, move, delete and create files in a directory.
○ Execute (x): Execute/run a file or cd into a directory.
● 3 sets of permissions: User/Owner, Group, and Other
18
Linux File Permissions
Q: What Drew (member of dabo) do to
test.py?
19
Changing File Permissions
● chmod changes permissions
● Symbolic Mode
Representation Description Operator Description
u user/owner + Adds permissions
g group - Remove permissions
o other = Sets permissions
a all
● Example
○ Removing read and write permission for group and others: chmod go-rw <filename>
○ Adding execute permissions for all (ugo): chmod a+x <filename>
○ Assigning write permission to group: chmod g=w <filename>
20
Changing File Permissions
● Numeric Mode
Permission Value
read 4
write 2
execute 1
No permission 0
● Example
○ Set read (add 4), write (add 2) and execute (add 1) for user, read (add 4) and execute
(add 1) for group, and no permission (add 0) for others: chmod 750 file-name
21
Changing File Ownership
● chown changes ownership and group
○ chown -R user:group <filename>
● Example:
○ Transfer the ownership to user news:
■ chown news <filename>
○ Transfer the ownership of directory:
■ chown -R news <directoryname>
○ Transfer the group ownership to admins:
■ chown :admins <filename>
○ Transfer the user and group ownership simultaneously:
■ chown news:admins <filename>
22
Default Permissions
● umask: Set shell parameters to control default
permissions
● For example: gives everything to owner, forbids writes to
group, and gives nothing to other users
○ umask 027
● Usually set in /etc/profile or /etc/csh.login
23
Special Permissions
● setuid (Set User ID): execute a file with the permissions of the file's owner,
rather than the permissions of the user executing the file
○ chmod u+s <filename>
● setgid (Set Group ID): execute a file with the permissions of the file's
group, rather than the permissions of the user executing the file
○ chmod g+s <filename>
● sticky bit:
○ On a directory, means only the owner of the file, directory, or superuser can delete or
rename files (even if other users have write permissions on the directory)
○ Keeps /tmp more private and secure
○ chmod +t <directoryname>
24
Access Control Lists (ACL)
● An extension to the traditional file permissions in Linux
○ Grant or deny specific permissions to individual users or groups
○ Provide fine-grained control over file and directory access
● Viewing the current ACL: getfacl <filename>
● Setting an ACL: setfacl [option] [action/specification] <filename>
○ option: -m (modify), -x (remove), -d (defaults)
○ Grant read and write permissions to a user 'alice' on directory shared_folder
■ setfacl -m u:alice:rw- shared_folder
○ Give user ‘alice’ read, write, and execute permissions, and ‘marketing’ group read and
execute permissions to any new files or directories created within the shared_folder
■ setfacl -m -d u:alice:rwx,g:marketing:r-x shared_folder
25