Linux SysAdmin Topic 104.odp
Linux SysAdmin Topic 104.odp
Topic 104
Devices, Linux Filesystems, Filesystem Hierarchy
Standard
104.1 Create partitions and filesystems
GPT vs. MBR
Globally Unique Identifiers Partition Table or GPT is the modern replacement for the
antique MS-DOS Master Boot Record (MBR). Some advantages of GPT:
- 64-bit disk pointers allows 264 total sectors, so a hard disk with 512-byte blocks can
be as large as 8 zebibytes. With 4096-byte sectors your maximum disk size is really
really large
- The default maximum number of partitions is 128, and if your operating system
supports it you can have more
104.1 Create partitions and filesystems
- No more CHS cruft or hacky primary-extended-logical partitioning scheme, which
falls down if you need Windows because Windows is inflexible and hogs primary
partitions
- GPT has fault-tolerance by keeping copies of the partition table in the first and last
sector on the disk
- GPT computes a cyclic redundancy check (CRC) checksum to verify its own
integrity, and of the partition table
- Unique IDs for disks and partitions.
# gdisk /dev/sda
104.1 Create partitions and filesystems
mkfs - Used to create a filesystem on a new partition. You may use mkfs or mke2fs.
OPTIONS
-V Produce verbose output, including all file system-specific commands that are executed.
Specifying this option more than once inhibits execution of any file system-specific
commands. This is really only useful for testing.
104.1 Create partitions and filesystems
-t fstype Specifies the type of file system to be built. If not specified, the default file
system type is used.
fs-options File system-specific options to be passed to the real file system builder.
Although not guaranteed, the following options are supported by most file system
builders.
-c Check the device for bad blocks before building the file system.
Supports huge individual file size and overall file system size.
Directory can contain a maximum of 64,000 subdirectories (as opposed to 32,000 in ext3)
104.1 Create partitions and filesystems
ext3
Journaling has a dedicated area in the file system, where all the changes are tracked.
When the system crashes, the possibility of file system corruption is less because of
journaling.
XFS is a highly scalable, high-performance file journalling file system which was
originally designed at Silicon Graphics, Inc in 1993. Originally XFS was used on Silicon
Graphics Inc's own operating system Irix, however, it was later ported to the Linux kernel
in 2001. Today XFS is supported by most Linux distributions and has now become the
default filesystem on RHEL (Red Hat Enterprise Linux), Oracle Linux 7, CentOS 7 and
many other distributions. Originally XFS was created to support extremely large
filesystems with sizes of up to 16 exabytes and file sizes of up to 8 exabytes.
104.1 Create partitions and filesystems
btrfs (read as butter-FS)
btrfs is a modern copy on write (CoW) filesystem for Linux aimed at implementing advanced features
while also focusing on fault tolerance, repair and easy administration. Jointly developed at multiple
companies, btrfs is licensed under the GPL and open for contribution from anyone.
btrfs is not a successor to the default Ext4 file system used in most Linux distributions, but it can be
expected to replace Ext4 in the future. Theodore Ts’o, a maintainer for Ext3 and later, Ext4, has stated
that he sees Btrfs as a better way forward than continuing to rely on the ext* technology.
VFAT is an extension of the FAT file system and was introduced with Windows 95. VFAT
maintains backward compatibility with FAT but relaxes the rules. For example, VFAT
filenames can contain up to 255 characters, spaces, and multiple periods.
The exFAT file system is ideal for flash drives and SD cards. It’s like FAT32, but without
the 4 GB file size limit. You can use exFAT drives on Linux with full read-write support,
but you’ll need to install a few packages first.
104.1 Create partitions and filesystems
mkswap sets up a Linux swap area on a device or in a file
After creating the swap area, you need the swapon command to start using it. Usually
swap areas are listed in /etc/fstab so that they can be taken into use at boot time by a
swapon -a command in some boot script.
104.1 Create partitions and filesystems
Options
-c Check the device (if it is a block device) for bad blocks before creating the swap area.
-f Force
-p PSZ Specify the page size to use.
-v0 Create an old style swap area.
-v1 Create a new style swap area.
104.2 Maintain the integrity of filesystems
du
The Linux “du” (Disk Usage) is a standard Unix/Linux command, used to check the information of
disk usage of files and directories on a machine.
Options:
-h (human readable sizes)
-a (all including files)
-d level (with directory level)
-c (with total size at the last line)
-s (with summary of a grand total disk usage size of a directory)
-k (in kilobytes)
104.2 Maintain the integrity of filesystems
df ("disk free")
df is used to get full summary of available and used disk space usage of file system on Linux system.
Options:
-a Show counts for all filesystems -t fs type Limit listing
to fs type
-h Print in human readable form -k Show size in Kilobytes
-m Display size in Megabytes -i Display inode
information
-l Limit listing to local filesystems -x fs type Exclude fs
type from listing
104.2 Maintain the integrity of filesystems
fsck - Check and repair a Linux file system
fsck will try to run filesystems on different physical drives in parallel to reduce total amount time to
check all of the filesystems.
Code Meaning
0 No errors
1 Errors found & corrected
2 System should be rebooted
4 Filesystem error left uncorrected
8 Operational error
16 Usage or syntax error
128 Shared library error
104.2 Maintain the integrity of filesystems
e2fsck - check a Linux ext2/ext3/ext4 file system
e2fsck also supports ext2 filesystems containing a journal, which are also sometimes known as ext3
filesystems.
To make an ext3 filesystem, you first make an ext2 filesystem and then add a journal to it using
tune2fs or use the -j option to mke2fs.
Usage is:
Option
Options:
It can be used to examine and change the state of an ext2/ext3/ext4 file system. debugfs is an
interactive debugger. It understands a number of commands:
dumpe2fs prints the super block and blocks group information for the filesystem present on device.
A superblock is a record of the characteristics of a filesystem, including its size, the block size, the empty and the
filled blocks and their respective counts, the size and location of the inode tables, the disk block map and usage
information, and the size of the block groups.
Common options
xfs_fsr: XFS is an extent-based file system. The xfs_fsr utility reorganizes and improves the layout of the
file extents, which improves overall performance. Run this command on a mounted XFS file system or on
individual files in the file system.
xfs_repair: Repair a corrupted or damaged XFS file system. Unmount the file system before running this
command. If the file system cannot be repaired, restore files from a backup with xfsrestore.
xfs_db: Debug an XFS file system. This utility provides a command set that allows you to perform scans on
the file system and to navigate and display its data structures.
/etc/fstab is the system's filesystem table. At boot time the /etc/fstab file assigns mount
points for block devices.
104.3 Control mounting and unmounting of
filesystems
# <file system> <mount point> <type> <options> <dump> <pass>
Column 5 of the fstab indicates whether to use the backup utility dump for the file system. 0 means no backup.
Column 6 indicates the sequence of the file system checks (with the fsck utility) when the system is booted:
2: all other modifiable file systems; file systems on different drives are checked in parallel
104.3 Control mounting and unmounting of
filesystems
mount
The mount command without any options will display all filesystems mounted as at the command time.
The mount command is used to make a particular device available on a specific directory (mount point). The syntax
is:
For example we can mount a CDROM on the mount point /mnt/cdrom with:
owner the device will change it's permission and belong to the user that mounted it
Syntax:
For example the following commands will both unmount the CDROM device:
umount /dev/cdrom
or
umount /mnt/cdrom
104.3 Control mounting and unmounting of
filesystems
blkid
Using blkid command you can view attributes of block devices that are on your system. This is a quick way to find
the type of the block devices on your system.
For example,
# blkid
/dev/sdb1: UUID="6e0acfe3-81ed-4f9f-8ab5-0d65ba1f0ef2" TYPE="ext2"
/dev/sdc1: UUID="aa82d7bb-ab2b-4739-935f-fd8a5c9a6cb0" TYPE="ext2"
/dev/sda1: UUID="187171ab-c9b8-43ec-b0bb-77c736ca22e0" TYPE="ext4" LABEL="/home"
/dev/sda2: UUID="1a225baa-7027-4619-aaa5-900e24c1fdff" TYPE="swap"
/dev/sdb3: UUID="2a294b33-eb61-40a3-b3fc-ad6eaf7f156f" TYPE="ext2"
104.3 Control mounting and unmounting of
filesystems
blkid commands
Example:
104.5 Manage file permissions and ownership
You own the files and directories that you created. As their owner, you have the
permission to specify who else is allowed access to them.
You can see who can read (r) and write to (w) the file, as well as who created the file (user), and to which group the
owner belongs (user). (By default, the name of your group is the same as your login name.)
Other information to the right of the group includes file size, date and time of file creation, and file name.
The first column shows current permissions; it has ten slots. The first slot represents the type of file. The remaining
nine slots are actually three sets of permissions for three different categories of users.
104.5 Manage file permissions and ownership
Using ls -l command and option will give ownership and permision information about
files.
104.5 Manage file permissions and ownership
A file has 3 modes of access:
r=4
w=2
x=1
-=0
104.5 Manage file permissions and ownership
chown - change file owner and group
5 = 4 (r ) + 0 (w) + 1 (x)
4 = 4 (r ) + 0 (w) + 0 (x)
3 = 0 (r ) + 2 (w) + 1 (x)
104.5 Manage file permissions and ownership
When using the symbolic mode, consider the following symbols:
Identities
u — the user who owns the file (that is, the owner)
g — the group to which the user belongs
o — others (not the owner or the owner's group)
a — everyone or all (u, g, and o)
Permissions Actions
r — read access + — adds the permission
w — write access - — removes the permission
x — execute access = — makes it the only permission
104.5 Manage file permissions and ownership
Some examples of permission settings using symbolic mode:
Add execute permission for the file’s owner (and leave everything else)
# chmod u+x file.txt
Remove write permission from group and others (and leave everything else)
# chmod go-w file.txt
Set the file to read only for everyone (kills existing permissions)
# chmod a=r file.txt
104.5 Manage file permissions and ownership
umask - set file mode creation mask
When a file is created, the system needs to know what permissions to assign to the newly
created file. This is done using ’umask’.
You set the bits in umask that you don't want set on any newly created file.
A newly created file will never have the execute bit set, regardless of the value of umask.
For example, a umask of 022 will ensure that write access is not granted to group and others.
Or, use 026 to make sure that other users can neither read nor edit your files.
104.5 Manage file permissions and ownership
Set-user Identification (SUID)
When a command or script with SUID bit set is run, its effective UID becomes that of the
owner of the file, rather than of the user who is running it.
$ ls -l /dev/sda1
brw-rw---- 1 root disk ... /dev/hda1
$ hexdump -n 10 /dev/sda1
hexdump: /dev/sda1: Permission denied
$ hexdump -n 10 /dev/sda1
0000000 ace9 4100 4a50 5726 1a4e
104.5 Manage file permissions and ownership
Set-group identification (SGID)
SGID permission is similar to the SUID permission, only difference is – when the script or
command with SGID on is run, it runs as if it were a member of the same group in which
the file is a member.
When SGID permission is set on a directory, files created in the directory belong to the
group of which the directory is a member.
For example if a user having write permission in the directory creates a file there, that file
is a member of the same group as the directory and not the user’s group.
It is useful for shared directories such as /var/tmp and /tmp because users can create files, read and
execute files owned by other users, but are not allowed to remove files owned by other users.
chmod +t [path_to_directory]
chmod 1777 [path_to_directory]
104.6 Create and change hard and symbolic links
inode
An inode (index node) is a data structure of a file. An inode stores basic information about
a regular file, directory, or other file system object:
=> File type (executable, block special etc) => Permissions (read, write etc)
=> Owner => Group
=> File Size => File access, change and
modification time
=> File deletion time => Number of links (soft/hard)
=> Extended attribute such as append => Access Control List (ACLs)
only or no one can delete file including
root user (immutability)
104.6 Create and change hard and symbolic links
To access the file's inode:
stat [file]
ls -i
104.6 Create and change hard and symbolic links
A soft link to a file or a directory creates a new inode that points to the same data area:
This is the listing for these files. Notice that the reference count is 1 for both files.
A hard link is an additional name for the same inode and as such the reference count of the file
increases by one for every new hard link.
ln lilo.conf lilo.link
In the listing notice that the reference count is 2 and that both files have the same size. In fact they are
identical.
Soft link
● can cross the file system,
● allows you to link between directories,
● has different inode number and file permissions than original file,
● permissions will not be updated,
● has only the path of the original file, not the contents.
Hard Link
● can't cross the file system boundaries (i.e. A hardlink can only work on the same filesystem),
● can't link directories,
● has the same inode number and permissions of original file,
● permissions will be updated if we change the permissions of source file,
● has the actual contents of original file, so that you still can view the contents, even if the original file moved or
removed.
104.7 Find system files and place files in the correct
location
The Filesystem Hierarchy Standard (FHS)
/ Primary hierarchy root and root directory of the entire file system hierarchy.
/bin Essential command binaries that need to be available in single user mode; for all
users, e.g., cat, ls, cp.
/home Users' home directories, containing saved files, personal settings, etc.
/proc Virtual filesystem providing process and kernel information as files. In Linux,
corresponds to a procfs mount. Generally automatically generated and populated by the
system, on the fly.
104.7 Find system files and place files in the correct
location
/root Home directory for the root user.
/run Run-time variable data: Information about the running system since last boot, e.g.,
currently logged-in users and running daemons. Files under this directory must be either
removed or truncated at the beginning of the boot process; but this is not necessary on
systems that provide this directory as a temporary filesystem (tmpfs).
/srv Site-specific data served by this system, such as data and scripts for web servers, data
offered by FTP servers, and repositories for version control systems
104.7 Find system files and place files in the correct
location
/sys Contains information about devices, drivers, and some kernel features.
/tmp Temporary files (see also /var/tmp). Often not preserved between system reboots, and
may be severely size restricted.
/usr Secondary hierarchy for read-only user data; contains the majority of (multi-)user
utilities and applications.
/var Variable files—files whose content is expected to continually change during normal
operation of the system—such as logs, spool files, and temporary e-mail files. (mysql files
- /var/lib/)
104.7 Find system files and place files in the correct
location
locate - find files by name
To change how the updatedb updates the database, the file /etc/updatedb.conf needs to be
edited. If this does not exist, it may be located in /etc/cron.daily/locate. Values of the following
variables may be changed.
PRUNE_BIND_MOUNTS="yes"
PRUNENAMES=".git .bzr .hg .svn"
PRUNEPATHS="/tmp /var/spool /media"
PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs
devfs mfs shfs sysfs cifs lustre tmpfs usbfs udf fuse.glusterfs fuse.sshfs curlftpfs"