Week 02 - Red Hat Linux Oct 2023 v3.3 PDF

Summary

This document covers the basics of Windows and Linux Security including file management tools and working with text files. It also includes various topics such as Absolute Paths and Relative paths, and introduces working with tar, gzip, and bzip2 commands.

Full Transcript

Official (Closed) - Non Sensitive WLSS L E Windows & Linux Security C T File Management Tools U & R Working with Text File E Diploma in CSF 1 Year 1 (2024/25), Semester 2 Diplo...

Official (Closed) - Non Sensitive WLSS L E Windows & Linux Security C T File Management Tools U & R Working with Text File E Diploma in CSF 1 Year 1 (2024/25), Semester 2 Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 1 Official (Closed) - Non Describe Linux File System Hierarchy Concepts Sensitive Goal: Copy, move, create, delete, and organize files from the Bash shell. Archive, compress, unpack, uncompress file using tar, gzip and bzip2 Objectives Describe how Linux organizes files, and the purposes of various directories in the file-system hierarchy. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 2 Official (Closed) - Non Describe Linux File System Hierarchy Concepts Sensitive Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 3 Official (Closed) - Non Significant Red Hat Enterprise Linux Directories Sensitive Location Purpose /boot Files to start the boot process. /dev Special device files that the system uses to access hardware. /etc System-specific configuration files. /home Home directory, where regular users store their data and configuration files. /root Home directory for the administrative superuser, root. /run Runtime data for processes that started since the last boot. This data includes process ID files and lock files. The contents of this directory are re-created on reboot. This directory consolidates the /var/run and /var/lock directories from earlier versions of Red Hat Enterprise Linux. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 4 Official (Closed) - Non Significant Red Hat Enterprise Linux Directories Sensitive /tmp A world-writable space for temporary files. Files that are not accessed, changed, or modified for 10 days are deleted from this directory automatically. The /var/tmp directory is also a temporary directory, in which files that are not accessed, changed, or modified in more than 30 days are deleted automatically. /usr Installed software, shared libraries, including files, and read- only program data. Significant subdirectories in the /usr directory include the following commands:/usr/bin: User commands /usr/sbin: System administration commands /usr/local: Locally customized software /var System-specific variable data should persist between boots. Files that dynamically change, such as databases, cache directories, log files, printer-spooled documents, and website content, might be found under /var. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 5 Official (Closed) - Non Absolute Paths and Relative Paths Sensitive The path of a file or directory specifies its unique file-system location. Following a file path traverses one or more named subdirectories, which are delimited by a forward slash (/), until the destination is reached. Directories, also called folders, can contain other files and other subdirectories. Directories are referenced in the same manner as files. Diploma in CSF Year 1, Semester 2 Last update: Nov 30, 2024 Slide 6 Official (Closed) - Non Absolute Paths Sensitive An absolute path is a fully qualified name that specifies the exact location of the file in the file- system hierarchy. The absolute path begins at the root (/) directory and includes each subdirectory that must be traversed to reach the specific file. For example, the absolute path name for the system message log file is /var/log/messages. Absolute path names can be long to type, so files can also be located relative to the current working directory of your shell prompt. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 7 Official (Closed) - Non Navigate Paths in the File System Sensitive The pwd command displays the full path name of the current working directory for that shell. This command helps you to determine the syntax to reach files by using relative path names. The ls command lists directory contents for the specified directory or, if no directory is given, for the current working directory. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 8 Official (Closed) - Non Navigate Paths in the File System Sensitive In the following example, a mixture of absolute and relative paths are used with the cd command to change the current working directory for the shell. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 9 Official (Closed) - Non Touch command Sensitive The touch command updates the time stamp of a file to the current date and time without otherwise modifying it. This command is useful for creating empty files, and can be used for practice, because when you use the touch command with a file name that does not exist, the file is created. In the following example, the touch command creates practice files in the Documents and Videos subdirectories. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 10 Official (Closed) - Non Create Directories Sensitive The mkdir command creates one or more directories or subdirectories. It takes as an argument a list of paths to the directories that you want to create. In the following example, files and directories are organized beneath the /home/user/Documents directory. Use the mkdir command and a space-delimited list of the directory names to create multiple directories. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 11 Official (Closed) - Non Create Directories Sensitive If the directory exists, or a parent directory of the directory that you are trying to create does not exist, then the mkdir command fails and it displays an error. The mkdir command -p (parent) option creates any missing parent directories for the requested destination. In the following example, the mkdir command creates three ChapterN subdirectories with one command. The -p option creates the missing Thesis parent directory. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 12 Official (Closed) - Non Move Files & Directories Sensitive The mv command moves files from one location to another. If you think of the absolute path to a file as its full name, then moving a file is effectively the same as renaming a file. The contents of the files that are moved remain unchanged. Use the mv command to rename a file. In the following example, the mv thesis_chapter2.txt command renames the thesis_chapter2.txt file to thesis_chapter2_reviewed.txt in the same directory. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 13 Official (Closed) - Non Using mv to rename the files Sensitive The mv command moves files from one location to another. If you think of the absolute path to a file as its full name, then moving a file is effectively the same as renaming a file. The contents of the files that are moved remain unchanged. Use the mv command to rename a file. In the following example, the mv thesis_chapter2.txt command renames the thesis_chapter2.txt file to thesis_chapter2_reviewed.txt in the same directory. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 14 Official (Closed) - Non Using mv to move the Files & Directories Sensitive Use the mv command to move a file to a different directory. In the next example, the thesis_chapter1.txt file is moved from the ~/Documents directory to the ~/Documents/Thesis/Chapter1 directory. You can use the mv command -v option to display a detailed output of the command operations. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 15 Official (Closed) - Non Removing Files & Directories Sensitive The rm command removes files. By default, rm does not remove directories. You can use the rm command -r or the --recursive option to enable the rm command to remove directories and their contents. The rm -r command traverses each subdirectory first, and individually removes their files before removing each directory. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 16 Official (Closed) - Non Removing Files & Directories Sensitive In the following example, the rm command removes the thesis_chapter1.txt file without options, but to remove the Thesis/Chapter1 directory, you must add the -r option. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 17 Official (Closed) - Non Removing Files & Directories Sensitive You can use the rm command -i option to interactively prompt for confirmation before deleting. This option is essentially the opposite of using the rm command -f option, which forces the removal without prompting the user for confirmation. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 18 Official (Closed) - Non Removing Files & Directories Sensitive Warning!!! If you specify both the -i and -f options, then the -f option takes priority and you are not prompted for confirmation before rm removes files. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 19 Official (Closed) - Non Removing Directories Using rm -r Sensitive You can also use the rmdir command to remove empty directories. Use the rm command -r option to remove non- empty directories. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 20 Official (Closed) - Non Activity 1: Creating files & directories Sensitive In the student user's home directory, use the mkdir command to create three subdirectories: Music, Pictures, and Videos. Use the touch command to create sets of empty practice files to use during this lab. In each set, replace X with the numbers 1 through 6. Create six files with names of the form songX.mp3. Create six files with names of the form snapX.jpg. Create six files with names of the form filmX.avi. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 21 Official (Closed) - Non Activity 1: Creating files & directories Sensitive Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 22 Official (Closed) - Non Activity 1: Move Files & Directories Sensitive Move the song files (.mp3 extension) to the Music directory, the snapshot files (.jpg extension) to the Pictures directory, and the movie files (.avi extension) to the Videos directory. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 23 Official (Closed) - Non Using tar Sensitive To create a tar file (archive) $ tar –cvf alltest.tar test1.txt test2.txt test3.txt test4.txt To extract a tar file $ tar –xvf alltest.tar Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 24 Official (Closed) - Non Using gzip Sensitive To create a gzip file $ gzip alltest.tar To unzip a gzip file $ gzip –d alltest.tar.gz Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 25 Official (Closed) - Non Using bzip2 Sensitive To compress a bzip2 file $ bzip2 –z alltest.tar To decompress a bzip2 file $ bzip2 –d alltest.tar.bz2 Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 26 Official (Closed) - Non Activity 2: Creating files & directories Sensitive Create three subdirectories for organizing your files, and name the subdirectories friends, family, and work. Use a single command to create all three subdirectories at the same time. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 27 Official (Closed) - Non Activity 2: Copy files & directories Sensitive Copy files that contain numbers 1 and 2 to the friends directory, and files that contain numbers 3 and 4 to the family directory. Keep in mind that you are making copies; therefore, the original files must remain in their original locations after you complete the step. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 28 Official (Closed) - Non Activity 2: Copy files & directories Sensitive Copy files that contain numbers 3 and 4 to the family directory. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 29 Official (Closed) - Non Activity 2: Copy files & directories Sensitive Copy the family and friends directories and their contents to the work directory. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 30 Official (Closed) - Non Activity 2: deleting files & directories Sensitive Your project tasks are now complete, and it is time to clean up the directories. Use the rm -r command to recursively delete the family, friends, and work directories and their contents. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 31 Official (Closed) - Non Make Links Between Files Sensitive You can create multiple file names that point to the same file. These file names are called links. You can create two types of links: a hard link, or a symbolic link (sometimes called a soft link). Each way has its advantages and disadvantages. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 32 Official (Closed) - Non Create Hard Links Sensitive Every file starts with a single hard link, from its initial name to the data on the file system. When you create a hard link to a file, you create another name that points to that same data. The new hard link acts exactly like the original file name. After the link is created, you cannot tell the difference between the new hard link and the original name of the file. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 33 Official (Closed) - Non Create Hard Links Sensitive You can determine whether a file has multiple hard links by using the ls -l command. One item that it reports is each file's link count, the number of hard links that the file has. In the next example, the link count of the newfile.txt file is 1. It has exactly one absolute path, which is the /home/user/newfile.txt location. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 34 Official (Closed) - Non Create Hard Links Sensitive You can use the ln command to create a hard link (another file name) that points to an existing file. The command needs at least two arguments: a path to the existing file, and the path to the hard link that you want to create. The following example creates a hard link called newfile- hlink2.txt for the existing newfile.txt file in the /tmp directory. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 35 Official (Closed) - Non Limitation of Hard Links Sensitive Hard links have some limitations. First, you can use hard links only with regular files. You cannot use the ln command to create a hard link to a directory or special file. Second, you can use hard links only if both files are on the same file system. The file-system hierarchy can be composed of multiple storage devices. Depending on the configuration of your system, when you change into a new directory, that directory and its contents might be stored on a different file system. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 36 Official (Closed) - Non Create Symbolic Links Sensitive The ln command -s option creates a symbolic link, which is also called a "soft link". A symbolic link is not a regular file, but a special type of file that points to an existing file or directory. Advantage: Symbolic links have some advantages over hard links: o Symbolic links can link two files on different file systems. o Symbolic links can point to a directory or special file, not just to a regular file. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 37 Official (Closed) - Non Create Symbolic Links Sensitive In the following example, the ln -s command creates a symbolic link for the /home/user/newfile-link2.txt file. The name for the symbolic link is /tmp/newfile-symlink.txt. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 38 Official (Closed) - Non Command-line Expansions Sensitive When you type a command at the Bash shell prompt, the shell processes that command line through multiple expansions before running it. You can use these shell expansions to perform complex tasks that would otherwise be difficult or impossible. Following are the main expansions that Bash shell performs: o Tilde expansion, which expand to a path to a user home directory o Command substitution, which replaces text with the output of a command Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 39 Official (Closed) - Non Table of Metacharacters and Matches Sensitive Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 40 Official (Closed) - Non Pathname Expansion and Pattern Matching Sensitive For the next example, imagine that you ran the following commands to create some sample files: Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 41 Official (Closed) - Non Pathname Expansion and Pattern Matching Sensitive In the next example, the first two commands use simple pattern matches with the asterisk (*) to match all the file names that start with "a" and all the file names that contain an "a", respectively. The third command uses the asterisk and square brackets to match all the file names that start with "a" or "c". Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 42 Official (Closed) - Non Pathname Expansion and Pattern Matching Sensitive The next example also uses question mark (?) characters to match some of those file names. The two commands match only file names with four and five characters in length, respectively. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 43 Official (Closed) - Non Tilde Expansion Sensitive The tilde character (~), matches the current user's home directory. If it starts with a string of characters other than a slash (/), then the shell interprets the string up to that slash as a username, if one matches, and replaces the string with the absolute path to that user's home directory. If no username matches, then the shell uses an actual tilde followed by the string of characters. In the following example, the echo command is used to display the value of the tilde character. You can also use the echo command to display the values of brace and variable expansion characters, and others. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 44 Official (Closed) - Non Tilde Expansion Sensitive Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 45 Official (Closed) - Non Introduction to the Linux Manual Pages Sensitive One source of documentation that is generally available on the local system is system manual pages or man pages. Software packages ship these pages to provide documentation, and you can access them from the command line by using the man command. The pages are stored in subdirectories of the /usr/share/man directory. Man pages originated from the historical Linux Programmer's Manual, which because of its size is split into multiple sections. Each section contains information about a particular topic. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 46 Official (Closed) - Non Common Sections of the Linux Manual Sensitive Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 47 Official (Closed) - Non Search for man Pages by Keyword Sensitive Use the man command -k option (equivalent to the apropos command) to search for a keyword in man page titles and descriptions. As a result, the keyword search displays a list of keyword-matching man page topics with section numbers. For example, the following command searches for man pages with the word passwd: Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 48 Official (Closed) - Non Introduction to the Linux Manual Pages Sensitive Read the su(1) man page. If you omit the user argument, then the su command assumes that the user is root. If the su command is followed by a single dash (-), then it starts a child login shell. Without the dash, the su command creates a non-login child shell that matches the user's current environment. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 49 Official (Closed) - Non Search for man Pages by Keyword Sensitive Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 50 Official (Closed) - Non Introduction to the Linux Manual Pages Sensitive Use the man -k ext4 command to find the command to tune ext4 file-system parameters. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 51 Official (Closed) - Non Create, View, and Edit Text Files Sensitive Goal Create, view, and edit text files from command output or in a text editor. Objectives o Save output or errors to a file with shell redirection, and process command output through multiple command-line programs with pipes. o Create and edit text files from the command line with the vim editor. o Set shell variables to run commands, and edit Bash startup scripts to set shell and environment variables to modify the behavior of the shell and programs that are run from the shell Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 52 Official (Closed) - Non Edit Files with Vim Sensitive The fundamental design principle of Linux is that it supports storage of the information and configuration settings in text-based files. These files follow various structures such as lists of settings, INI-like formats, structured XML or YAML, and others. The advantage of storing files in a text-based structure is that they are edited with any text editor. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 53 Official (Closed) - Non Benefits of the Vim Editor Sensitive When a system uses a text-only shell prompt, you should know how to use at least one text editor for editing files. You can then edit text-based configuration files from a terminal window or remote logins through the ssh command or the Web Console. You also do not need access to a graphical desktop to edit files on a server, and that server might not need to run a graphical desktop environment. The key reason to learn Vim is that it is almost always installed by default on a server for editing text-based files. The Portable Operating System Interface or POSIX standard specified the vi editor on Linux, and many other UNIX-like operating systems largely do likewise. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 54 Official (Closed) - Non Get Started with Vim Sensitive You can install the Vim editor in Red Hat Enterprise Linux by using either of two packages. These two packages provide different features and Vim commands for editing text-based files. With the vim-minimal package, you might install the vi editor with core features. This lightweight installation includes only the core features and the basic vi command. You can open a file for editing by using the vi command: Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 55 Official (Closed) - Non Edit Files with Vim Sensitive Alternatively, you can use the vim-enhanced package to install the Vim editor. This package provides a more comprehensive set of features, an online help system, and a tutorial program. Use the vim command to start Vim in this enhanced mode: Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 56 Official (Closed) - Non Vim Operating Modes Sensitive The Vim editor offers various modes of operation such as command mode, extended command mode, edit mode, and visual mode. As a Vim user, always verify the current mode, because the effect of keystrokes varies between modes Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 57 Official (Closed) - Non Vim Operating Modes Sensitive Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 58 Official (Closed) - Non Edit Files with Vim Sensitive When you first open Vim, it starts in command mode, which is used for navigation, cut and paste, and other text modification. Pressing the required keystroke accesses specific editing functions. o An i keystroke enters insert mode, where all typed text becomes file content. Pressing Esc returns to command mode. o A v keystroke enters visual mode, where multiple characters might be selected for text manipulation. Use Shift+V for multiline and Ctrl+V for block selection. To exit the visual mode, use the v, Shift+V, or Ctrl+V keystrokes. o The : keystroke begins extended command mode for tasks such as writing the file (to save it) and quitting the Vim editor. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 59 Official (Closed) - Non The Minimum, Basic Vim Workflow Sensitive Vim has efficient, coordinated keystrokes for advanced editing tasks. Although considered beneficial with practice, the capabilities of Vim can overwhelm new users. Red Hat recommends that you learn the following Vim keys and commands: o The u key undoes the most recent edit. o The x key deletes a single character. o The :w command writes (saves) the file and remains in command mode for more editing. o The :wq command writes (saves) the file and quits Vim. o The :q! command quits vim, and discards all file changes since the last write. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 60 Official (Closed) - Non Rearrange Existing Text Sensitive In Vim, you can yank and put (copy and paste), by using the y and p command characters. Position the cursor on the first character to select, and then enter visual mode. Use the arrow keys to expand the visual selection. When ready, press y to yank the selection into memory. Position the cursor at the new location, and then press p to put the selection at the cursor. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 61 Official (Closed) - Non Visual Mode in Vim Sensitive Visual mode is useful to highlight and manipulate text in different lines and columns. You can enter various visual modes in Vim by using the following key combinations. o Character mode : v o Line mode : Shift+v o Block mode : Ctrl+v Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 62 Official (Closed) - Non Edit Files with Vim Sensitive Run the vimtutor command. In the presentation, keyboard arrow keys help to navigate the window. Initially, when the vi editor was developed, users could not rely on having arrow keys or working keyboard mappings for arrow keys to move the cursor. Therefore, the vi editor was initially designed to move the cursor by using commands with standard character keys, such as the conveniently grouped h, j, k, and l. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 63 Official (Closed) - Non Edit Files with vimtutor Sensitive Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 64 Official (Closed) - Non Edit Files with Vim Sensitive $ vim test.txt Press “i” for insert and type the following text in the vim editor Type :wq to save the file and quit the editor. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 65 Official (Closed) - Non Summary Sensitive o Describe Linux File System Hierarchy Concepts o Absolute Paths and Relative Paths o Using tar, gzip, bzip2 o Know how to use at least one command-line text editor, and Vim is the recommended option because it is commonly installed by default in Linux distributions. Diploma in CSF Last update: Nov 30, 2024 Year 1, Semester 2 Slide 66

Use Quizgecko on...
Browser
Browser