Linux Command Fundamentals Flashcards
What commands help explain the use of specific commands and their syntax?
command –help
man command
pinfo
What is the difference between su and sudo?
- su is used to open the shell as another user.
- sudo allows commands to be run in escalated mode.
What is the wheel group used for in RHEL?
The wheel group is used to assign a user root privlidges.
Users in this group can be seen in the group configuration.
$ cat /etc/group | grep wheel wheel:x:10:student
What is su - used for?
su - can be used to open a shell as another user
su - without any username will open as root (if enabled).
What is sudo used for?
sudo allows authorised users to run tasks with escalated privileges.
sudo ls -l /root
The user must be a member of the wheel group
What two types of options are often used with commands?
- Long options; these start with a – (double dash) e.g. –help
- Short options; these start with a - (single dash) e.g. ls -l and mkdir -p
What does the whoami command do?
The whoami prints the username associated with the current user.
What is the ls command used for?
The ls command lists directory contents.
What command line utility is used to administer network interfaces?
The ip utility is used to administer network interfaces.
What text user interace is used to administer network interfaces?
nmtui
What command is used to show addresses assigned to all network interfaces?
ip a (ip address)
What is the cat command used for?
The cat command is used to print files to the standard output.
Usage: cat /etc/hosts
What command line utility is used to list directory contents?
ls lists directory contents
What command is used to locate the current directory?
The pwd is used to print name of current/working directory.
What is the pwd command used for?
The pwd is used to print name of current/working directory.
What command is used to reset a password?
The passwd command is used to update a user’s authentication token(s).
What is the passwd command used for?
The passwd command is used to update a user’s authentication token(s).
What is the touch command useful for?
The touch command can be used to quickly create a file and to test if a user has permissions to modify a file e.g.
$ touch /etc/hosts $ touch: cannot touch '/etc/hosts': Permission denied
What command can be used to quickly verify if a user has permission to modify a file?
The touch command can be used to quickly create a file and to test if a user has permissions to modify a file e.g.
$ touch /etc/hosts $ touch: cannot touch '/etc/hosts': Permission denied
What commands can be used to view the manual page names and descriptions?
- apropos
- man -k
Example:
$ man -k directory $ adcli (8) - Tool for performing actions
man -k & apropos both search the mandb based on a keyword
What could cause man -k or apropos to return “nothing appropriate”?
The database searched by apropos is updated by the mandb program; this is updated periodicallly by a cron job and may require a manul execution on newly installed systems.
To manually update the mandb, execute the command with root privileges.
$ mandb
see man man (-k) or man apropos and man mandb
What ls command displays hidden files?
The ls -a command does not ignore entries starting with . (dot).
What is ls -d option useful for?
The ls -d option is used to see the properties of the directory and not its contents.
When using the ls utility, what is the -l option used for?
The ls -l command returns the results in the long listing format.
When using the ls utility, what is the -s option used for?
The ls -s (–size) option prints the allocated size of each file, in blocks.
What is the ls -lrt command useful for?
The ls -lrt shows the last modified file last.
* -l - use a long listing format
* -r (–reverse) - reverse order while sorting
* -t - sort by time, newest first; see –time
What two commands can be used to list the contents of all directories in a parent folder?
The ls -R and tree commands returns all subfolders and their contents.
ls -R
tree
Example
$ ls -R
.:
Desktop Documents Downloads Music Pictures Public Templates users Videos
./Desktop:
./Documents:
./Downloads:
./Music:
./Pictures:
./Public:
./Templates:
./Videos:
$ tree
.
├── Desktop
├── Documents
├── Downloads
├── Music
├── Pictures
├── Public
├── Templates
├── users
└── Videos
-R, –recursive list subdirectories recursively
.
What key combination can be used to access to terminal session when in desktop session?
Ctrl + Alt + F3
What key combination can be used to access the desktop session when in a terminal session?
Ctrl + Alt + F2
What command can be used to create a list of active users and their terminals?
$ who
```
$ w
~~~
What can the chvt command be used for?
The command chvt N (terminal number from who) makes the /dev/ttyN the foreground terminal.
This command must be run using escalated privildeges.
What is the difference between command options and arguments and parameters?
What are the 9 man sections?
- User command (executable programs or shell commands)
- System calls (functions provided by the kernel)
- Library calls (functions within program libraries)
- Special files (usually found in /dev)
- File formats and conventions eg /etc/passwd
- Games
- Miscellaneous (including macro packages and conventions),
- System administration commands (usually only for root)
- Kernel routines [Non standard]
The command below can used to read an introduction of a specific section
$ man N intro
Sections 1, 5 and 8 are most important for basic administration (RHCSA)
How do you search through a man page?
using / (forward slash) with a string
use n to skip forward through the search results. shift + n reverse.
How do you find a command in a specific section?
If the section is known e.g. it is an adminstrator command (8)
man -k user | grep 8
If the section is not known, the man -k user command (without grep) will return all references to user and the section.
What are the two most common editors?
nano & vim (vi improved)
What command outputs the results to a file?
’>’ (greater than)
ls > directory
What command appends the results to a text file?
’»’ (double greater than)
ls >> directory
What command is used to view recently used commands?
history
The history file is found under ~/.bash_history
How do you remove a line from the history file?
history -d n
How do you replay a specific command from history?
!n
The history command returns all stored commands with a row number.
What does the 2> /dev/null command do?
The 2> /dev/null is used to redirect/supress errors.
The command broken down:
* 2 refers to a standard error,
* ‘>’ (greater than) is a file redirection operator and
* /dev/null is a null device used to suppress any data written to it.
The 3 file descriptors are:
‘0’ for standard input
‘1’ for standard output
‘2’ for standard error
How do you search for files and folders begining with a specific character?
ls -d a*
How do you search for files and folders with charachters begining a, b or c?
ls -d [abc]*
How do you create a directory path?
mkdir -p /tmp/folder1
-p, –parents no error if existing, make parent directories as needed
How do you remove a directory?
rmdir -p /tmp/folder1
How do you create a directory path with multiple subfolders?
mkdir -p /tmp/{folder1, folder2, folder3}
-p, –parents no error if existing, make parent directories as needed
How do you remove a directory path with multiple specific subfolders?
rmdir -p /tmp/{folder1, folder2, folder3}
How do you copy between directories?
cp /tmp/currenthome /tmp/newhome/
How do you copy files and folders recursively?
cp -R
-R, -r, –recursive copy directories recursively
How do you copy files into the current directory?
cp /tmp/currenthome .
. (dot) is the current directory
How do you move files between locations?
The mv command moves files between locations.
Usage: mv [OPTION]… [-T] SOURCE DEST
or: mv [OPTION]… SOURCE… DIRECTORY
or: mv [OPTION]… -t DIRECTORY SOURCE…
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
What command is used to delete folders that are not empty?
rm -rf /tmp/somefolder
Usage: rm [OPTION]… [FILE]…
Remove (unlink) the FILE(s).
Where is the sudo configuration managed?
Sudo configuration is manged through /etc/sudoers
What is a hard link?
A hard link points to the metadata (the file) on the filesystem (inode). Multiple hard links can point to the same file.
A file is only deleted once all hard links refering to an inode have been deleted; there must be at least one hard link to keep a file on the filesystem.
Hard links cannot point to directories and hardlinks must be on the same filesystem.
Hard links are created using the ln -P command; the -P option is not required, as it is the default option when using ln.
see man ln
What is a symbolic link?
AKA soft link
A symbolic is a reference to a specific name e.g. filename 2 refers to filename (hard link > inode).
If the referenced filename changes, the symbolic link is broken.
A symbolic link is most commonly used for directories e.g.
/bin > /usr/bin
What is the best practice when creating symbolic links?
Absolute filenames should be used to avoid braking changes.
What command is used to create a symbolic link?
ln -s /tmp/filename /tmp/symlink
What command is used to create a hard link?
ln /tmp/filename /tmp/symlink
the option -P is used for physical (hard links); this is option default.
What command is used to search for files in a directory hierarchy?
The find command can be used to search for files
$ find / -name "hosts"
A wildcard * can be used in the name to broaden the search string.
How do you copy the results of the find command?
By executing cp against the results e.g.$ find / -name "hosts" -exec cp {} ~/hosts/ 2>/dev/null \;
How do you find files of a specific size?
By using the find command along with the size option e.g.
$ find / type -f -size +100M
Type -f (regular file) and -size + (greater than), M for mebibytes
How do you search for a specific value within a file?
Using the find and grep commands together can help locate files with specific values/contents e.g.
$ find /etc -exec grep -l student {} \;
What is the backslash and semicolon combination used for?
The backslash () is an escape character that instructs the shell not to interpret the next character.
What are the curley braces used for in the command:$ find / -name "hosts" -exec cp {} ~/hosts/ 2>/dev/null \;
The results of the first command is used in the second command being executed under -exec.
In this example, the files found with the name “hosts” are then copied to a folder under the users directory called hosts.
What command displays the environment variables?
The env command displays the environment variables.
What can the alias command be used for?
To simplify repetitve and/or complex command combinations e.g. alias dir=’ls -ltr’
Where can you create user specific and startup programs?
The .bash_profile can be used to create run bash scripts and create alias e.g. alias dir=’ls -ltr’
How do you mount new partitions such as a USB device?
First view all mount points to locate the USB drive
$ lsblk
Create a directory to mount the device agaist
$sudo mkdir /mnt/usbdrive
Mount the drive to the directory
$ sudo mount /dev/sdb1 /mnt/usbdrive
How do you remove a mount point?
$ sudo umount /mnt/usbdrive
How do you create an archive file that contains the home and etc directories?
Using the tar command you can create a tar ball file
$ tar -czvf archive.tar /home /etc
-z filters through gzip for compression
How do you display the contents of a tar file?
Using the tar -tvf command you can create a tar ball file
$ tar -tvf archive.tar
-tvf lists all files in archive.tar verbosely.
What utility can be used in scripts to convert text from upper to lower case?
The tr (translate) command can convert case
$ echo hello | tr [:lower:] [:upper:] $ HELLO
What grep option can be used to filter out results?
The grep -v (–invert-match) selects non-matching lines e.g.
$ ps aux | grep ssh | grep -v grep
Without the last option, the results would include both the ssh process and the executed command by the user.
man grep
What does grep mean?
grep stands for
“global / regular expression search / and print”
How do you update the sudoers configuration?
the /etc/sudoers configuration must be edited using visudo
What is the best practice for editing the sudo configuration?
It is best practice to create drop-in files under /etc/sudoers.d
How do you add users to the wheel group?
usermod -aG wheel username
see man usermod.
-a (append), -G (groups)
How do you increase the authentication timeout for sudo access?
You can increase the authentication token expiration by adding:Defaults timestamp_type=global,timestamp_timeout=60
What is the best practice when deleting users?
what options should be used?
The options remove and force should be used to clearly remove the user
$ userdel -rf username
What 4 commands are used for creating and managing users?
- useradd: create user accounts
- usermod: modify user accounts
- userdel: delete user accounts
- passwd: set passwords
Where are user default settings stored?
Default user settings should be stored under /etc/login.defs
useradd settings are located under /etc/default/useradd (don’t use this)
How do you create files in the user home directory upon creation?
Using the /etc/skel directory; files in this directory are created in the users home directory upon creation.
What are the options to lock and unlock a user account?
usermod -L username will lock the account
usermod -U username will unlock the account
What option is used to set an account to expire?
usermod -e 2023-12-31 username is used to expire an account e.g. expire username on 31st December 2023.
What option is used to prevent accounts from logining into the system?
usermod -s /sbin/nologin myapp
man usermod -s (shell)
What command shows the status of an account?
passwd -S username outputs a short information about the status of the password for a given account.
The status information consists of 7 fields.
- username
- status e.g. LK (locked password), NP (no password), PS (useable password)
- Last password change
- Minimum age
- Maximum age
- Warning period
What command is used to temporarily set primary group membership?
The newgrp
What command is used to add a user to an existing group?
The groupmod -U username groupname command adds a user to a group
The usermod -G command replaces the existing groups with a new list.
What command is used to list all members of a group?
The lid -g groupname lists all members of the group
Where are passwords stored?
Passwords are stored (encrypted) in the /etc/shadow file.
Other than passwd, what other command can be used to change password settings?
The chage command can be used to change password expiry information.
What command can be used to display user account settings?
Example last password change, password experation date etc.
The chage -l username lists account aging information.
What command is used to set user ownership?
The chown command sets user ownership
$ chown user[:group] filename
group can be used optionally with and without the user possition.
What command is used to set group ownership?
The chgrp command sets group ownership
$ chgrp groupname filename
WHat command is used to manage filesystem permissions?
The chmod command is used to manage permissions.
chmod can be used in absolute or relative mode.
What are the three values used for file system permissions?
- read (4)
- write (2)
- execute (1)
What are the three groups to define permissions?
Owner, Group, Others
- Owner: rwx = 4+2+1 = 7
- Group: r– = 4+0+0 = 4
- Others: r– = 4+0+0 = 4
What does the first letter of the ls output (permissions) identify?
example: drwxr-xr-x.
The first mode field is the “special file” designator:
d (directory)
c (character device)
l (symlink)
p (named pipe)
s (socket)
b (block device)
D (door, not common on Linux systems, but has been ported)
What is the less command used for?
The less utility opens a text file in a pager, which allows for easy reading
What is the head command used for?
The head utility displays the first ten lines of the text file
man head
What is the tail command used for?
The tail utility displays the last ten lines of the text file
man tail
What is the cut command used for?
The cut command is used to filter specific columns or characters from a text file
man cut
What is the sort command used for?
The sort command sorts the contents of a text file
man sort
What is the wc command used for?
The wc (word count) command counts the number of lines, words, and characters in a text file
man wc
What does the grep option -i do?
The -i option matches upper- and lowercase letters (i.e., not case sensitive).
What does the grep option -v do?
The -v
shows only lines that do not contain the regular expression.
What does the grep option -r do?
The -r searches files in the current directory and all subdirectories.
What does the grep option -e do?
The -e searches for lines matching more than one regular expression.
Use -e before each regular expression you want to use.
What does the grep option -E do?
The -E interprets the search pattern as an extended regular expression.
What does the grep option -A <number>
do?
The -A <number>
shows <number> of lines after the matching regular expression.</number>
What does the grep option -B <number>
do?
The -B <number>
shows <number> of lines before the matching regular expression.</number>
ssh
What does the ssh option -v
do?
The -v
(verbose) option shows in detail what is happening while establishing the connection
ssh
What does the ssh option -Y
do?
The -Y
(verbose) option enables support for graphical applications
ssh
What does the ssh option -p <PORT>
do?
The -p <PORT>
is used to connect to an SSH service that is not listening on the default port 22
Elevated Permissions
What is a PolicyKit
?
A PolicyKit
enables you to set up graphical utilities to run with administrative privileges
Execute Permissions
What does the Read
permission allow on files and directories?
The Read
permission allows
* Files: view file content
* Directories: list contents of the directory
Execute Permissions
What does the Write
permission allow on files and directories?
The Write
permission allows
* Files: change contents of a file
* Directories: create and delete files
Execute Permissions
What does the Execute
permission allow on files and directories?
The Execute
permission allows
* Files: run a program file
* Directories: change to the directory
What is the numeric representation for read, write and execute?
- Read: 4
- Write: 2
- Execute: 1
What is the df -Th
intended for?
The df -Th command was designed to show available disk space on mounted devices; it includes most of the system mounts.
Because it will look on all mounted file systems, it is a convenient command to use to get an overview of current system mounts.
The -h option summarizes the output of the command in a human-readable way, and the -T option shows which file system type is used on the different mounts.
What is the findmnt command used for?
The findmnt command shows mounts and the relationship that exists between the different mounts.
Because the output of the mount command is a bit overwhelming, you may like the output of findmnt. Notice that because of width limitations of the book page, the output that belongs in the OPTIONS column appears on the left side of the page.
What is the awk utility used for?
Awk is a utility that enables a programmer to write tiny but effective programs in the form of statements that define text patterns that are to be searched for in each line of a document and the action that is to be taken when a match is found within a line.
awk -F : '/user/ { print $4 }' /etc/passwd
This command searches the /etc/passwd file for the text user and will print the fourth field of any matching line.
What is the sed utility used for?
sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline).
sed -i s/old-text/new-text/g ~/myfile
~~~
```
In this example, the sed utility is used to search for the text old-text in ~/myfile and replace all occurrences with the text new-text.