midterm Flashcards

1
Q

Which directory is passwd in?
What kinds of information does the passwd file store?

A

/etc/passwd

Store stuff like:
username, password (x if it’s encrypted), UID, GID, location of home directory, location of login shell

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Where is encrypted password stored?

A

/etc/shadow

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Is it possible to read /etc/passwd file as a regular user?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What type of info does /etc/shadow have?

A
  • username
  • encrypted password
  • last password change
  • min/max password age
  • warning period
  • inactivity period
  • expiration date
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to add a new user?

A

useradd

useradd [options] username

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Where is default configuration for useradd?

A

/etc/default/useradd

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how to delete a user?

A

userdel
userdel -r username (if you want to remove their home directory too)

userdel [options] username

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why might you not be able to delete a user?

A

User still have running processes. Kill processes under a user by:

pkill -u username

or you can forcefully delete user with:
userdel -fr username

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to ser password for a new user?

A

passwd username

Will prompt you to enter password twice. Nothing will be showed on the screen though

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to lock and unlock a user account?

A

sudo passwd -l username (for lock)
sudo passwd -u username (for unlock)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how to view groups a member is in?

A

groups username

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

how to create a new group?

A

groupadd

groupadd [options] group-name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

which folder has info about groups on your system?

A

/etc/group

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what does the sudo command do?

A

allows a user to temporarily perform tasks with elevated privileges

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

where are the configurations for sudo?

A

/etc/sudo.conf

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

where are the configurations for who can use sudo?

A

/etc/sudoers

a sudoers file will generally include a line that grants members of their the “sudo” group or “wheel” group sudo privileges. eg. %wheel ALL+(ALL) ALL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

what directory contains individual configuration files?

A

/etc/sudoers.d/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Why do we have sudo command? why not use root for everything/

A

Always want to give least privileges as possible so they can’t do what they shouldn’t do.

Accountability

Security. There’s add security for what hackers have to guess.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

who to add user to a group?

A

sudo usermod -aG group user

the “a” is for append. Will not overwrite other groups the user is in.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are the permissions?

A

r = read
w = write
x = execute
- = placeholder for permissions user does not have

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

format of what permissions looks like?

A

d rwx rw- r–

d = directory (file type)
then user permission
then group
then other

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What are the octal/numeric permissions?

A

4 = read
2 = write
1 = execute

eg.
chmod 644 filename
(still in user, group, other order)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

how to change file ownership?

A

chown

chown [options] user:group file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

how to set password expiry?

A

sudo usermod -e YYY-MM-DD user

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How to provide an enivornment similar to what the user would have if they had logged in directly?
su -l username
26
how many groups can a file belong to?
1
27
can a user belong to more than one group?
yes
28
What's a file descriptor?
unique, positive number used to identify a open file. When process makes successful request to open a file, the kernel returns the file descriptor. File descriptor points to file table, which has info about file permissions
29
how to view file descript?
`ls -l /proc//fd`
30
what are the 3 default file descriptors?
stdin (0) - defaults to input from keyboard, can be from tther files too stdout (1) - defaults to user's screen stderr (2) - defaults to user's screen
31
What's the "redirect standard out"?
> (clobber, will overwrite) >> (append, add it to the end)
32
What's the "redirect standard error"?
2> eg. find /etc -type f 2> errors (redirects it to a file called errors)
33
what's the special file that doesn't store information sent to it?
/dev/null eg. command 2> /dev/null
34
how to combine standard out and standard error?
&>
35
How to user stdin where input comes from a file instead of keyboard?
eg. sort < names input is coming from file called "names". Note this does not change the content of the file.
36
pipe
| pipe output of one command into another eg. command1 | command2 command2's stdin comes from command1's stfout eg. ls -l | grep "*.txt" | wc -l
37
what's the shebang?
#!/bin/bash Tells shell which interpreter to use when script is executed. Gives it the absolute path It's the first line of a shell script (or else it'll be interpreted as a comment instead cuz of the #)
38
how to run a file?
./file need the ./ if the directory it's in is not in your PATH
39
difference between single and double quotes?
Double: will expand anything (eg. variable expansion) single: it will be exactly what's in the single quote with no expansions
40
echo
writes arguments to standard out. there's a \n after the echo line is executed. It also preserves white space
41
when would you use printf instead of echo?
when you want specific formatting
42
how to declare a variable? How to expand it?
variable=content echo "$variable" with NO SPACES when you declare it. Need "" when you call it
43
what's a positional parameter?
it accesses arguments passed into the script when you run it
44
$0
name of bash script
45
$1, $2
bash script arguments
46
$$
process ID of the current shell/script
47
$#
total number of arguments passed to script
48
$@
gives the value of all arguments passed as an ARRAY of SEPARATE things
49
$?
exit status of last executed command
50
$!
process ID of last executed command
51
$*
gives value of all arguments treated as a SINGLE STRING
52
what's iteration
reprtition until a condition changes
53
test
Evaluate an expression. aka [ ] Returns a status code Indication if the expression is true or false? "if" conditional syntax is just another way of using test
54
how is the "if" statement written in bash?
if [[ test ]]; then ... elif [[ second test ]]; then ... else ... fi
55
how to test if one file is older than another?
if [[ file-one -ot file-two ]]; then ... fi
56
what are the 3 loops?
for, while, until
57
are bash arrays only 1-D? (i.e. you can't nest an array inside an array)
Yes, they're only 1 dimensional
58
how to create an indexed array?
declare -a arr=("1" "2" "3") or arr=("1" "2" "3") **notice they're separated by space, not comma! I think it will keep the whitespace if you have " " ?)
59
how to access array values by index?
arr=("1" "2" "3") echo "${arr[0]}"
60
how to find how many elements in indexed array?
arr=("1" "2" "3") echo "${#arr[@]}"
61
how to create associative array?
it's like a dictionary in python declare -A person person["name"]="Jan" person["age"]="31" **notice: - it's -A (not -a) - integer still has " "
62
how to print all values in associative array?
echo "${arr[@]}"
63
how to print all keys in associated array?
echo "${!arr[@]}"
64
how to print value by key in associative array?
echo "${arr["key1"]}"
65
syntax of for loop
list="1 2 3 4 5" for item in $list; do ... done
66
How to print 1-10 incrementing by 2?
for i in {01..10..2}; do echo $i done
67
while loop syntax
while [condition]; do ... done
68
how to read contents of a file line by line with a while loop?
file=/etc/passwd while read -r line; do echo $line done < "$file"
69
what does break do in a loop?
exit entire loop
70
what does continue do in a loop?
go to the next iteration of the loop
71
how to store output of a date in a variable?
now=$(date) (Note: this will store the current time and will not update)
72
What command to use for troubleshooting? Where do you put it in your script?
set -x put it right under the shebang
73
explain this code: if [[ -f ~/.bashrc ]]; then source ~/.bashrc fi
Checks if the .bashrc file exists in the user's home directory, and if it does, it executes the commands from that file within the current shell session
74
is .sh necessary for a shell script?
No, it's an older convention. Now, most shell scripts have no file extension
75
How many types of expansions are there? What are they?
7 types of expansions: - brace expansion - tilde expansion - parameter and variable expansion - command substitution - arithmetic expansion - word splitting - filename expansion
76
You have: file1 file2 file3 how do you list out all these files?
echo file{1..3}
77
what does tilde expansion do?
unquoted ~ expands to current user's home directory (/home/username)
78
parameter and variable expansion basic syntax?
${parameter}
79
first="Mickey" last="Mouse" how do you print out Mickey_Mouse?
echo "${first}_${last}"
80
what does this print? first="Mickey" last="Mouse" echo "$first_$last"
Mouse (b/c you don't have { }, so it doesn't expand the variables properly)
81
how to print length of the variable values? (how many letters)
"${#variable}"
82
arr_one=( 1 2 3 4 ) how to return all the elements of arr_one indexed array?
echo ${arr_one[@]}
83
arr_one=( 1 2 3 4 ) how to return length of the indexed array?
echo ${#arr_one[@]}
84
what does this do? What does it return? letters="abc" echo ${letters/abc/123}
It replaces characters. Returns 123 It will substitute the first occurrence of the pattern abc in the value of the variable letters with 123. /abc/ is the pattern to search for
85
Explain this. What does it print? var_name="some_variable" some_variable="Hello, world!" echo "${!var_name}"
prints Hello, world! ! is indicating indirect expansion. It's where you can use the value of a variable as the name of another variable and then expand it
86
What does this print? name="Nathan" echo "${name:0:2}" echo "${name:2:2}"
Na th
87
how to get extension of a file?
${file##*.} (file is a variable name)
88
now=$(date) what does this do? What kind of substitution is this?
it stores date in "now" This is a command substitution. Lets the output of a command to replace the command itself.
89
what is the arithmetic expansion syntax?
"$(( expression )) " eg. echo "$(( (3 + 4) * 5 ))" (returns 35)
90
what does this return? nums1=( $(echo "one two three") ) nums2=( "$(echo one two three)" ) echo ${#nums1[@]} ${#nums2[@]}
3 1
91
What's IFS?
Internal Field Separator. Default is whitespace (space, tab, newline) (IFS=$' \t\n')
92
What do these do? * ? [ ]
* matches any sequence of characters ? matches any single character [ ] matches any characters enclosed inside Note: They have to be un-quoted for it to be regarded as a pattern
93
How to make 2 folders inside of each other?
mkdir -p folder1/folder2
94
what do these do: ls d* ls dir?
ls d* will display any files and directories in the current directory that start with the letter d. ls dir? will display any files and directories that start with dir and has 1 more letter
95
case statements
alternative to if/then/else clauses
96
general syntax of case statement?
case EXPRESSION in pattern_1) statements ;; pattern_2) statements ;; *) statements ;; esac
97
How to prompt user to enter something and store it in a variable called var1?
read -p "Enter something" var1
98
how to prompt user to enter a letter and check if they entered A or B?
read -p "Enter a letter" character case "$character" in A) echo "You entered A" ;; B) echo "You entered B" ;; *) echo "You did not enter A or B" ;; esac
99
what is a shell function?
a compound command that has been given a name. It's a series of commands for later execution. Functions must be declared before they're called
100
general syntax of a function?
func_name() { function body }
101
Make a function "say_hi" that takes user input as the name to say hi to. Then call the function
say_hi() { echo "Hi $1" } say_hi "Bob" Note: the argument is NOT in brackets.
102
how to make a variable local?
use "local" keyword. Use the word "local" during variable declaration. eg. local func_result="some result"
103
What does this return? #!/usr/bin/env bash localretrn () { local func_result="some result" echo "$func_result" } func_result="$(localretrn)" echo $func_result
some result
104
Explain this: #!/usr/bin/env bash This will print arguments passed to the function printall() { local names=$@ for name in $names; do echo "arg to func: $name" done echo "Inside func: \$0 is still: $0" } printall $@
printall $@ -- passes all positional parameters to the printall function
105
How to move cursor in vim?
h - left j - down k - up l - right
106
vim - how to move to top and bottom of screen
H - top L - bottom
107
vim - how to go to first and last line of document
gg - first G - last
108
vim - how to replace a single character
r
109
vim - how to undo and redo?
u (small u) - undo ctrl+r - redo
110
vim - how to start visual mode
v (small v)
111
vim - how to copy a line
yy
112
vim - how to paste
p - paste after cursor P - paste before cursor
113
vim - how to delete a line
dd
114
vim - how to write/save a file
:w
115
vim - how to quit
:q will fail if there are unsaved changes
116
vim - how to append
must be in insert mode a - append after cursor A - append at end of line
117
how to make a file?
touch filename makes empty files if it doesn't exist. If it exists, it updates the modification time
118
how to search for manual page
man -k search-word
119
how to move up a directory
cd ..
120
how to move to root directory
cd /
121
what's pwd?
print working directory. gives you absolute path
122
how to copy files/directories from one location to another
cp [options] source destination -r copies recursively -i prompts before overwriting -u copies only when source file is newer -a preserves permissions, ownership, timestamp
123
how to move or rename files and directories?
mv [options] source destination
124
how to delete an empty directory?
rmdir
125
how to delete a file/directory that is not empty?
rm -rf file
126
how to search for a file?
locate file.txt use -i to ignore case sensitivity
127
how to read contents of a file?
cat file
128
how to display info about operating system?
uname
129
how to change permissions
chmod
130
how to change owner of file/dir?
chown [options] new_owner:new_group file
131
3 types of journaling in ext3?
journal (slowest) writeback (best performance; less consistent) ordered (default; does not update filesystem metadeta)
132
which filesystem is good for large file manipulation and high-end hardware performance?
XFS
133
What is a swap file?
A form of virtual memory. Generates temporary storage space by replacing a section of memory in the RAM of a paused program
134
how to find what type of filesystem your system uses?
df -T then look at column "Type"
135
which folder has stores configuration files?
/etc
136
which folder contains temporary files that are erased when a computer shuts down?
/tmp do not have to manually remove them
137
which folder contains files and utilities shared between users?
/usr
138
what's an example of a pseudo-filesystem?
proc filesystem
139
where do you find info about running processes?
`/proc/`
140
what's the logical "and" symbol?
&&
141
what's the logical "or" symbol?
||
142
what signal is sent when you try dividing by 0?
SIGILL
143
how to see a list of background processes?
jobs
144
how to bring a background job to the foreground?
`fg `
145
what (default) signal is send for the kill command?
SIGTERM
146
how to forcibly quit a process?
`kill -SIGKILL ` or `kill -9 `
147
how to find the processes taking the most resources?
top (the ones taking the most resources automatically float to the top)
148
where do you find group information?
/etc/group
149
how to view password expiration information?
`sudo chage -l `
150
how to change length of time for which user's password is valid
chage sudo chage -d 0 user sets days to password expiration to 0 (must change password on first login) `sudo chage -M <# of days> user ` max days before password expires
151
What's the minimum documentations in a program you write?
- author - date - description of command
152
test expressions/options for: - existence of file - regular file - directory
-e for existance -f for regular file (checks type of file) - d for directory
153
test expressions/options for integers: - equality inequality
-eq for equality - ne for inequality -gt for greater than -lt -ge for greater than or equals to -le
154
test expressions/options for strings: - quality/identical - inequality - if it's empty - if it's not empty - logical AND - logical OR
- quality/identical: = - inequality: != - if it's empty: -z - if it's not empty: -n - logical AND: -a - logical OR: -o
155
what does (( ... )) evaluate?
arithmetic expression.
156
what does -z do in if/else statement?
tests if variable is empty eg. read name if [[ -z $name ]] then .....
157
what does "read" do?
read text from standard input, then store it in a variable read [options] variable
158
how to match lowercase letters?
[[:lower]]
159
how to match letters h to o inclusive?
[h-o]
160
help
view all shell built-ins
161
system call interface
allows user-level processes to interact with the kernel
162
What does the kernel do
it's the lower level component that talks to hardware
163
What terminals do Linux OS' usually come with?
Gnome terminal (Gnome) Konsol (KDE)
164
What's a shell?
It's the shell that runs commands you type into the terminal. it's a command line envrionment
165
What's a SSH? What is it used for?
SSH = Secure Shell protocol It's a way for securely sending commands to a computer over an unsecured network. SSH uses cryptography to authenticate and encrypt connects between devices. Often used for controlling servers remotely, for managing infrastructure, and transferring files. It has 2 keys: public (on server) and private (on host)
166
What's a utility
It's a separate program. Usually a binary in /usr/bin. eg. mkdir
167
$PATH
Show your existing path. $PATH variable is an environment variable used to define the directories where the shell looks for executable files when you run a command.
168
What's a command
What you enter into the shell. Utility followed by arguments.
169
What is the wildcard?
*
170
How many Vim basic modes are there? What are the 4 common ones?
7 basic modes. Common ones: Normal, Insert, Visual, Command-line
171
How to search for a man page?
man -k search-word
172
What's a pager?
man pages are viewed on a separate utility called a pager. Most common pager is "less"
173
less
a pager that you can view man pages on
174
How to search for a word in less?
/search-word
175
Where is the vim configuration file
~/.vimrc
176
How to apply changes after you make a change for vim configuration?
:source ~/.vimrc
177
Where is the bash shell configuration file?
~/.bashrc
178
What is a filesystem?
Methods and data structures operating systems use to keep track of files. It's the way files are organized on the disk. Underlying rules for how data is stored.
179
Filesystem Hierarchy Standard
Most Linux distro have files and directories organized acoording to this
180
Where does the filesystem begin at?
It beings at the root directory / and branches out like a family tree
181
What's inside dev directory (/dev)?
Contains special or device files
182
What's inside the root directory (/)?
It's the start of the tree, the beginning of our directory structure. Inside are key directories used to store specific files on your system
183
What's inside bin directory (/bin)?
/bin is a symbolic link to /usr/bin Historically stored utilities needed to run in single user mode. Most binary files stored here.
184
What's inside proc directory (/proc)?
Contains a sub directory for each running process, which is named after the PID. Proc is a pseudo filesystem.
185
What's inside home directory (/home)?
Regular user home directories are stored here. eg. /home/bob
186
What's a pseudo filesystem?
A way to work with non-file data as though it were a file. Advantage is that organizing and interacting with data can be handled like how you work with files.
187
What is a symbolic link? (aka. softlink, symlink)
Indirect pointer to a file. The directory entry contains pathname of the pointed-to-file. A pointer to the hard link to the file. When you dereference a symbolic link, you follow the link to the target file. Can create a symbolic link with "ln -s"
188
how to create a symbolic link
ln -s [options] FILE LINK (eg. ln -s source_file symbolic_link)
189
How can you view links?
ls -l Option links will appear with an arrow pointing to the file they link to. (eg. bin -> usr/bin/
190
how to Find files based on file properties.
find [path] [options] [expression] eg. find /path/to/search -type d -name "docs"
191
how to execute a command on a file found by "find"
-exec -exec {} \; -exec {} + (used with "find") eg. find ./GFG -name smaple.txt -exec rm -i {} \;
192
what is -ctime (used with "find")
the time when the metadata of a file or directory (such as permissions, ownership, etc.) was last modified
193
-mtime (used with "find")
search for files based on their modification time. eg. -mtime +7 would search for files whose modification time is more than 7 days ago eg. -mtime 0 to search for files whose modification time is exactly zero days ago (i.e., files that were modified today)
194
Find patterns of text in a file. To search for text patterns within files or standard input, and then print lines that match those patterns.
grep Uses (eg.): look for file based on contents of the file. grep "search-word" file eg. grep "pattern" file1.txt file2.txt
195
Return found instances of search term within 2 lines above and below where search pattern was found (for context)
grep -C 2 search-word file
196
Count number of times "set" was found in .bashrc file
grep -c "set" ~/.bashrc
197
Return list of files that contain search pattern
grep -rl "search pattern" dir
198
fc -list | grep -i "nerd"
fc = list fonts available this means to find all of the nerd fonts you have installed.
199
What's a process?
Running instance of a program
200
display information about all running processes in the system.
ps -e
201
ps -e | grep vim
list all processes currently running on the system (ps -e), and then filter the output to only show lines that contain the word "vim" (grep vim).
202
echo $?
view exit code of last command
203
what's an exit code?
When process terminates for any reason, it returns an exit code
204
what does exit code 1 mean? 0?
0 = no error (success, did was it was told to do) non-0 = error
205
ctrl+r
"reverse history search" or "reverse incremental search." Lets you search through your command history for previously entered commands.
206
What is a signal used for?
a way for user (or another process) to communicate with a running process
207
SIGINT
Interrupts a foreground process. The signal sent when you press Ctrl+C
208
SIGTERM
Terminate a process. Graceful shutdown. Lets process finish but stops child processes.
209
SIGKILL
force quit immediately
210
kill vs pkill
kill is installed on almost every distro. You have to give it a PID pkill only needs a pattern that matches a process name. But it's not as precise
211
Orphan process
Can end up with these when you kill a process. Parent process killed but child process is left running and not adopted by parent process' immediate descendant. These are problematic b/c they're still taking up memory
212
ps -e | grep -i systemd
list all processes currently running on the system (ps -e), and then filter the output to only show lines that contain the case-insensitive word "systemd" (grep -i systemd)
213
ps -eo comm,pid,%cpu | grep -i systemd
list the command name, process ID, and CPU usage for all processes currently running on the system where the command name contains "systemd" (case-insensitive).
214
ps -H
hierarchical view of processes, showing their relationships in a tree-like structure
215
ps --forest
display a process tree in a hierarchical, tree-like format. This command is particularly useful for visualizing the parent-child relationships between processes and understanding the process hierarchy on the system. Has indentation and \_
216
top
To monitor system activity in real-time (it updates). It provides a dynamic, interactive view of system processes, CPU usage, memory usage, and other system resources. h for help q for quit shift+V to toggle tree view
217
htop
Gives overview of your system, like top, but easier to read and has colours.
218
"nice" value
helps determine how much CPU time to give to a process. Lower nice value = higher priority (more CPU time)
219
How many user and group owner(s) can a file have?
1 of each
220
What types of users are there?
Human System
221
Why is UID 0?
Root
222
What UID is reserved for system users?
UID 1-999
223
arr_one=( 1 2 3 4 ) how to echo all the elements of the array? how to echo the length of arr_one?
echo ${arr_one[@}} echo ${#arr_one[@]}
224
letter="abc" how to replace abc with 123 to echo 123?
echo ${letters/abc/123}
225
prefix_a=one prefix_b=two echo ${!prefix_*} What does this echo? What does the ! do?
it echoes: prefix_a prefix_b (matches patterns that start with prefix_) (NOTE: it prints the VARIABLE name) ! is indirect expansion (value of the variable is used as name of another variable to expand)
226
what's the difference between: $( ) and $(( ))
$( ) is command substitution $(( )) is arithmetic expansion
227
228