RedHat Flashcards
w command
displays a list of users currently logged into the computer. This is especially useful to show which users are logged in using ssh from which remote locations, and what they are doing
ssh command
encrypts the connection to secure the communication against eavesdropping or hijacking of the passwords and content; i option is used to specify the user’s private key file; the private key file must be readable only by the user that owns the file (chmod 600 could be used to ensure this)
exit command
terminate the current shell session. Alternatively, finish a session by pressing Ctrl+D
date command
displays the current date and time; can be used to calculate a date in the future: date -d “+45 days” -u (The -u option reports the time in UTC.)
passwd command
changes a user’s own password; The superuser can use the passwd command to change other users’ passwords; passwd username command sets the initial password or changes the existing password of username.
file command
scans the beginning of a file’s contents and displays what type it is. The files to be classified are passed as arguments to the command.
cat command
create single or multiple files, view the contents of files, concatenate the contents from multiple files, and redirect contents of the file to a terminal or files.
less command
displays one page of a file at a time and lets you scroll at your leisure.
head and tail commands
display the beginning and end of a file, respectively. By default these commands display 10 lines of the file, but they both have a -n option that allows a different number of lines to be specified. The file to display is passed as an argument to these commands.
wc command
counts lines, words, and characters in a file. It takes a -l, -w, or -c option to display only the number of lines, words, or characters, respectively.
useradd command
used by the superuser, root, to create additional users on the system; Tab completion following a partial option can be used to complete the option without a lot of typing.
history command
displays a list of previously executed commands prefixed with a command number; exclamation point character (!) is a metacharacter that is used to expand previous commands without having to retype them. The !number command expands to the command matching the number specified. The !string command expands to the most recent command that begins with the string specified.
/ directory
root directory at the top of the file-system hierarchy
• static
content remains unchanged until explicitly edited or reconfigured
• dynamic or variable
content may be modified or appended by active processes
• persistent
content remains after a reboot, like configuration settings
• runtime
process- or system-specific content that is deleted by a reboot
/usr
Installed software, shared libraries, include files, and read-only program data. Important subdirectories include:
• /usr/bin: User commands.
• /usr/sbin: System administration commands.
• /usr/local: Locally customized software.
/etc
Configuration files specific to this system.
/var
Variable data specific to this system that should persist between boots. Files that dynamically change, such as databases, cache directories, log files, printer-spooled documents, and website content may be found under /var.
/run
Runtime data for processes started since the last boot. This includes process ID files and lock files, among other things. The contents of this directory are recreated on reboot. This directory consolidates /var/run and /var/lock from earlier versions of Red Hat Enterprise Linux.
/home
Home directories are where regular users store their personal data and configuration files.
/root
Home directory for the administrative superuser, root.
/tmp
A world-writable space for temporary files. Files which have not been accessed, changed, or modified for 10 days are deleted from this directory automatically. Another temporary directory exists, /var/tmp, in which files that have not been accessed, changed, or modified in more than 30 days are deleted automatically.
/boot
Files needed in order to start the boot process.
/dev
Contains special device files that are used by the system to access hardware.
absolute path
fully qualified name, specifying the files exact location in the file system hierarchy. It begins at the root (/) directory and specifies each subdirectory that must be traversed to reach the specific file. A path name with a forward slash (/) as the first character is an absolute path name
working directory or current working directory
current location
relative path
identifies a unique file, specifying only the path necessary to reach the file from the working directory; path name with anything other than a forward slash as the first character is a relative path name
pwd command
displays the full path name of the current working directory for that shell
ls command
lists directory contents for the specified directory or, if no directory is given, for the current working directory; To view the owner of a file use the ls -l command. To view the owner of a directory use the ls -ld command. In the following output, the third column shows the username.
cd command
change your shell’s current working directory. If you do not specify any arguments to the command, it will change to your home directory; command cd - changes to the previous directory; cd .. command uses the .. hidden directory to move up one level to the parent directory
touch command
normally updates a file’s timestamp to the current date and time without otherwise modifying it. This is useful for creating empty files
(.)
current directory
(..)
parent directory
hidden files
File names beginning with a dot (.) indicate hidden files; you cannot see them in the normal view using ls and other commands. This is not a security feature. Hidden files keep necessary user configuration files from cluttering home directories.
mkdir command
creates one or more directories or subdirectories; -p (parent) option creates missing parent directories for the requested destination; Use the mkdir command and a space-delimited list of the directory names to create multiple directories.; Use the mkdir -p command and space-delimited relative paths for each of the subdirectory names to create multiple parent directories with subdirectories.
cp command
copies a file, creating a new file either in the current directory or in a specified directory. It can also copy multiple files to a directory; Warning - If the destination file already exists, the cp command overwrites the file.; When copying multiple files with one command, the last argument must be a directory.; By default, the cp does not copy directories; it ignores them.; Use the copy command with the -r (recursive) option, to copy the directory
mv command
moves files from one location to another; Use the mv command to rename a file.
rm command
removes files. By default, rm will not remove directories that contain files, unless you add the -r or –recursive option; use the rm -ri command to interactively prompt for confirmation before deleting. This is essentially the opposite of using the -f option, which forces the removal without prompting the user for confirmation; Warning - If you specify both the -i and -f options, the -f option takes priority and you will not be prompted for confirmation before rm deletes files.; use the rmdir command, rm -d (which is equivalent to rmdir), or rm -r to remove an empty directory
Hard Links
You can find out if a file has multiple hard links with the ls -l command. One of the things it reports is each file’s link count, the number of hard links the file has.; If you want to find out whether two files are hard links of each other, one way is to use the -i option with the ls command to list the files’ inode number.; Even if the original file gets deleted, the contents of the file are still available as long as at least one hard link exists. Data is only deleted from storage when the last hard link is deleted.; • A hard link points a name to data on a storage device
Soft Links
soft link (sometimes called a symbolic link); the first character of the long listing is l instead of -. This indicates that the file is a soft link and not a regular file; • A soft link points a name to another name, that points to data on a storage device; by default cd will update your current working directory using the name of the soft link rather than the name of the actual directory. (There is an option, -P, that will update it with the name of the actual directory instead.)
ln command
to create a new hard link (another 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.
df command
list the directories that are on different file systems; Files in two different “Mounted on” directories and their subdirectories are on different file systems.
ln -s command
creates a soft link, which is also called a “symbolic link.” A soft link is not a regular file, but a special type of file that points to an existing file or directory.
• They can link two files on different file systems.
• They can point to a directory or special file, not just a regular file.
The command needs at least two arguments, a path to the existing file, and the path to the soft link that you want to create.
tilde character (~)
matches the current user’s home directory
Brace Expansion | echo command
echo command can also be used to display the values of brace and variable expansion characters, and others; double-dot syntax (..) expands to a sequence; Braces contain a comma-separated list of strings, or a sequence expression
single quotes (‘) or double quotes (“)
used to enclose strings. They have slightly different effects. Single quotes stop all shell expansion. Double quotes stop most shell expansion.
Use double quotation marks to suppress globbing and shell expansion, but still allow command and variable substitution.
man -k keyword
displays a list of keyword-matching man page topics with section numbers; The man command -K (uppercase) option performs a full-text page search, not just titles and descriptions like the -k option. A full-text search uses greater system resources and take more time.
whereis command
Locate the binary, source, and manual pages located in the /usr/share/man directory
pinfo command
launch the Info document viewer; Info documents typically cover particular software packages as a whole, tend to have more practical examples of how to use the software, and are structured as hypertext documents.
> file
redirect stdout to overwrite a file
> > file
redirect stdout to append to a file
2> file
redirect stderr to overwrite a file
2> /dev/null
discard stderr error messages by redirecting to /dev/null
> file 2>&1&> file
redirect stdout and stderr to overwrite the same file
> > file 2>&1&» file
redirect stdout and stderr to append to the same file
> file 2>&1
redirects standard output to file and then redirects standard error to the same place as standard output (file)
2>&1 > file
redirects standard error to the default place for standard output (the terminal window, so no change) and then redirects only standard output to file.
&>file or >file 2>&1
• Store output and generated errors together.
&»file or»_space;file 2>&1
append output and generated errors together
less command
display output on the terminal one screen at a time.
tee command
tee copies its standard input to its standard output and also redirects its standard output to the files named as arguments to the command
pipe character (|)
connects the standard output of the first command to the standard input of the next command; Standard error can be redirected through a pipe, but the merging redirection operators (&> and &») cannot be used to do this; 2>&1 is the correct way to redirect both standard output and standard error through a pipe
set command
list all shell variables that are currently set
Retrieving Values with Variable Expansion
precede the name of the variable with a dollar sign ($); echo $VARIABLENAME
PATH variable: echo $PATH
contains a list of colon-separated directories that contain programs; When you run a command such as ls, the shell looks for the executable file ls in each of those directories in order, and runs the first matching file it finds. (On a typical system, this is /usr/bin/ls.); add additional directories to the end of your PATH.
add /home/user/sbin to the end of your PATH for the current session like this:
[user@host ~]$ export PATH=${PATH}:/home/user/sbin
env command
list all the environment variables for a particular shell
Setting the Default Text Editor
export EDITOR=nano
Setting Variables Automatically
Assuming the default /etc/profile, /etc/bashrc, and ~/.bash_profile files, if you want to make a change to your user account that affects all your interactive shell prompts at startup, edit your ~/.bashrc file.
unset command
unset and unexport a variable entirely
export -n command
unexport a variable without unsetting it
id command
show information about the currently logged-in user; To view basic information about another user, pass the username to the id command as an argument.
ps command
view process information; The default is to show only processes in the current shell. Add the a option to view all processes with a terminal. To view the user associated with a process, include the u option.
su command
allows users to switch to a different user account; The command su starts a non-login shell, while the command su - (with the dash option) starts a login shell. The main distinction between the two commands is that su - sets up the shell environment as if it were a new login as that user, while su just starts a shell as that user, but uses the original user’s environment settings.; administrators should run su - to get a shell with the target user’s normal environment settings.
sudo
all commands executed are logged by default to /var/log/secure; all members of the wheel group can use sudo to run commands as any user, including root; run sudo su - from that account to get an interactive root user shell; Another way to access the root account with sudo is to use the sudo -i command. This will switch to the root account and run that user’s default shell (usually bash) and associated shell login scripts. If you just want to run the shell, you can use the sudo -s command.; The main configuration file for sudo is /etc/sudoers. To avoid problems if multiple administrators try to edit it at the same time, it should only be edited with the special visudo command.
enable full sudo access
To enable full sudo access for the user user01, you could create /etc/sudoers.d/user01 with the following content:
user01 ALL=(ALL) ALL
To enable full sudo access for the group group01, you could create /etc/sudoers.d/group01 with the following content:
%group01 ALL=(ALL) ALL
It is also possible to set up sudo to allow a user to run commands as another user without entering their password:
ansible ALL=(ALL) NOPASSWD:ALL
default configuration of the sudo -i
You can make sudo -i behave more like su - by editing /etc/sudoers with visudo. Find the line
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
and replace it with the following two lines:
Defaults secure_path = /usr/local/bin:/usr/bin
Defaults>root secure_path = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
useradd username command
creates a new user named username. It sets up the user’s home directory and account information, and creates a private group for the user named username. At this point the account does not have a valid password set, and the user cannot log in until a password is set.; • Some defaults, such as the range of valid UID numbers and default password aging rules, are read from the /etc/login.defs file. Values in this file are only used when creating new users. A change to this file does not affect existing users.; the useradd command assigns new users the first free UID greater than or equal to 1000, unless you explicitly specify one using the -u option
usermod options
- c, –comment COMMENT Add the user’s real name to the comment field.
- g, –gid GROUP Specify the primary group for the user account.
- G, –groups GROUPS Specify a comma-separated list of supplementary groups for the user account.
- a, –append Used with the -G option to add the supplementary groups to the user’s current set of group memberships instead of replacing the set of supplementary groups with a new set.
- d, –home HOME_DIR Specify a particular home directory for the user account.
- m, –move-home Move the user’s home directory to a new location. Must be used with the -d option.
- s, –shell SHELL Specify a particular login shell for the user account.
- L, –lock Lock the user account.
- U, –unlock Unlock the user account.
userdel username command
removes the details of username from /etc/passwd, but leaves the user’s home directory intact
userdel -r username
removes the details of username from /etc/passwd and also deletes the user’s home directory
find / -nouser -o -nogroup command
find all unowned files and directories.
UID Ranges
- UID 0 is always assigned to the superuser account, root.
- UID 1-200 is a range of “system users” assigned statically to system processes by Red Hat.
- UID 201-999 is a range of “system users” used by system processes that do not own files on the file system. They are typically assigned dynamically from the available pool when the software that needs them is installed. Programs run as these “unprivileged” system users in order to limit their access to only the resources they need to function.
- UID 1000+ is the range available for assignment to regular users.
Default ranges used by useradd and groupadd can be changed in the /etc/login.defs file.
groupadd command
creates groups; -g option specifies a particular GID for the group to use; -r option creates a system group using a GID from the range of valid system GIDs listed in the /etc/login.defs file; • The SYS_GID_MIN and SYS_GID_MAX configuration items in /etc/login.defs define the range of system GIDs.
groupmod command
changes the properties of an existing group; -n option specifies a new name for the group.; -g option specifies a new GID
groupdel command
removes groups; You cannot remove a group if it is the primary group of any existing user.
chage command
implement a password aging policy; The preceding chage command uses the -m, -M, -W, and -I options to set the minimum age, maximum age, warning period, and inactivity period of the user’s password, respectively.
chmod command
change permissions
chmod WhoWhatWhich file|directory
• Who is u, g, o, a (for user, group, other, all)
• What is +, -, = (for add, remove, set exactly)
• Which is r, w, x (for read, write, execute)
(using a capital X as the permission flag will add execute permission only if the file is a directory or already has execute set for user, group, or other)
-R option to recursively set permissions on the files in an entire directory tree
chown command
change file ownership; can be used with the -R option to recursively change the ownership of an entire directory tree; can also be used to change group ownership of a file by preceding the group name with a colon (:); can also be used to change both owner and group at the same time by using the owner:group syntax; You may encounter examples of chown commands using an alternative syntax that separates owner and group with a period instead of a colon (You should not use this syntax. Always use a colon. A period is a valid character in a user name, but a colon is not. )
umask command
display the current value of the shell’s umask; The system’s default umask values for Bash shell users are defined in the /etc/profile and /etc/bashrc files. Users can override the system defaults in the .bash_profile and .bashrc files in their home directories.; The default umask for users is set by the shell startup scripts. By default, if your account’s UID is 200 or more and your username and primary group name are the same, you will be assigned a umask of 002. Otherwise, your umask will be 022.; As root, you can change this by adding a shell startup script named /etc/profile.d/local-umask.sh
ps command
listing current processes; displays once
top command
show the state of each process; display that dynamically updates
jobs command
display the list of jobs that Bash is tracking for a particular
fg command / &
A background job can be brought to the foreground with its job ID (%job number).; Any command or pipeline can be started in the background by appending an ampersand (&) to the end of the command line.; To send a foreground process to the background, first press the keyboard generated suspend request (Ctrl+z) in the terminal.
ps j command
displays information relating to jobs. The PID is the unique process ID of the process. THe PPID is the PID of the parent process of this process, the process that started (forked) it. The PGID is the PID of the process group leader, normally the first process in the job’s pipeline. The SID is the PID of the session leader, which (for a job) is normally the interactive shell that is running on its controlling terminal.
bg command
To start the suspended process running in the background, use the same job ID.
kill command
sends a signal to a process by PID number. Despite its name, the kill command can be used to send any signal, not just those for terminating programs. You can use the kill -l command to list the names and numbers of all available signals.
pkill command
send a signal to one or more processes which match selection criteria. Selection criteria can be a command name, a process owned by a specific user, or all system-wide processes
w command
list user logins and current running processes; All user login sessions are associated with a terminal device (TTY). If the device name is of the form pts/N, it is a pseudo-terminal associated with a graphical terminal window or remote login session. If it is of the form ttyN, the user is on a system console, alternate console, or other directly connected terminal device.
pgrep command
lists processes (which operates much like pkill, including using the same options)
pstree command
view a process tree for the system or a single user
uptime command
display the current load average. It prints the current time, how long the machine has been up, how many user sessions are running, and the current load average.