midterm Flashcards
Which directory is passwd in?
What kinds of information does the passwd file store?
/etc/passwd
Store stuff like:
username, password (x if it’s encrypted), UID, GID, location of home directory, location of login shell
Where is encrypted password stored?
/etc/shadow
Is it possible to read /etc/passwd file as a regular user?
Yes
What type of info does /etc/shadow have?
- username
- encrypted password
- last password change
- min/max password age
- warning period
- inactivity period
- expiration date
How to add a new user?
useradd
useradd [options] username
Where is default configuration for useradd?
/etc/default/useradd
how to delete a user?
userdel
userdel -r username (if you want to remove their home directory too)
userdel [options] username
Why might you not be able to delete a user?
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 to ser password for a new user?
passwd username
Will prompt you to enter password twice. Nothing will be showed on the screen though
How to lock and unlock a user account?
sudo passwd -l username (for lock)
sudo passwd -u username (for unlock)
how to view groups a member is in?
groups username
how to create a new group?
groupadd
groupadd [options] group-name
which folder has info about groups on your system?
/etc/group
what does the sudo command do?
allows a user to temporarily perform tasks with elevated privileges
where are the configurations for sudo?
/etc/sudo.conf
where are the configurations for who can use sudo?
/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
what directory contains individual configuration files?
/etc/sudoers.d/
Why do we have sudo command? why not use root for everything/
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.
who to add user to a group?
sudo usermod -aG group user
the “a” is for append. Will not overwrite other groups the user is in.
What are the permissions?
r = read
w = write
x = execute
- = placeholder for permissions user does not have
format of what permissions looks like?
d rwx rw- r–
d = directory (file type)
then user permission
then group
then other
What are the octal/numeric permissions?
4 = read
2 = write
1 = execute
eg.
chmod 644 filename
(still in user, group, other order)
how to change file ownership?
chown
chown [options] user:group file
how to set password expiry?
sudo usermod -e YYY-MM-DD user
How to provide an enivornment similar to what the user would have if they had logged in directly?
su -l username
how many groups can a file belong to?
1
can a user belong to more than one group?
yes
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
how to view file descript?
ls -l /proc/<pid>/fd
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
What’s the “redirect standard out”?
> (clobber, will overwrite)
> (append, add it to the end)
What’s the “redirect standard error”?
2>
eg.
find /etc -type f 2> errors
(redirects it to a file called errors)
what’s the special file that doesn’t store information sent to it?
/dev/null
eg.
command 2> /dev/null
how to combine standard out and standard error?
&>
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.
pipe
eg.
ls -l | grep “*.txt” | wc -l
pipe output of one command into another
eg.
command1 | command2
command2’s stdin comes from command1’s stfout
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 #)
how to run a file?
./file
need the ./ if the directory it’s in is not in your PATH
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
echo
writes arguments to standard out.
there’s a \n after the echo line is executed. It also preserves white space
when would you use printf instead of echo?
when you want specific formatting
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
what’s a positional parameter?
it accesses arguments passed into the script when you run it
$0
name of bash script
$1, $2
bash script arguments
$$
process ID of the current shell/script
$#
total number of arguments passed to script
$@
gives the value of all arguments passed as an ARRAY of SEPARATE things
$?
exit status of last executed command
$!
process ID of last executed command
$*
gives value of all arguments treated as a SINGLE STRING
what’s iteration
reprtition until a condition changes
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
how is the “if” statement written in bash?
if [[ test ]]; then
…
elif [[ second test ]]; then
…
else
…
fi
how to test if one file is older than another?
if [[ file-one -ot file-two ]]; then
…
fi
what are the 3 loops?
for, while, until
are bash arrays only 1-D? (i.e. you can’t nest an array inside an array)
Yes, they’re only 1 dimensional
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 “ “ ?)
how to access array values by index?
arr=(“1” “2” “3”)
echo “${arr[0]}”
how to find how many elements in indexed array?
arr=(“1” “2” “3”)
echo “${#arr[@]}”
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 “ “
how to print all values in associative array?
echo “${arr[@]}”
how to print all keys in associated array?
echo “${!arr[@]}”
how to print value by key in associative array?
echo “${arr[“key1”]}”
syntax of for loop
list=”1 2 3 4 5”
for item in $list; do
…
done
How to print 1-10 incrementing by 2?
for i in {01..10..2}; do
echo $i
done
while loop syntax
while [condition]; do
…
done
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”
what does break do in a loop?
exit entire loop
what does continue do in a loop?
go to the next iteration of the loop
how to store output of a date in a variable?
now=$(date)
(Note: this will store the current time and will not update)
What command to use for troubleshooting? Where do you put it in your script?
set -x
put it right under the shebang
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
is .sh necessary for a shell script?
No, it’s an older convention.
Now, most shell scripts have no file extension
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
You have:
file1
file2
file3
how do you list out all these files?
echo file{1..3}
what does tilde expansion do?
unquoted ~ expands to current user’s home directory (/home/username)
parameter and variable expansion basic syntax?
${parameter}
first=”Mickey”
last=”Mouse”
how do you print out Mickey_Mouse?
echo “${first}_${last}”
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)
how to print length of the variable values? (how many letters)
”${#variable}”
arr_one=( 1 2 3 4 )
how to return all the elements of arr_one indexed array?
echo ${arr_one[@]}
arr_one=( 1 2 3 4 )
how to return length of the indexed array?
echo ${#arr_one[@]}
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
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
What does this print?
name=”Nathan”
echo “${name:0:2}”
echo “${name:2:2}”
Na
th
how to get extension of a file?
${file##*.}
(file is a variable name)
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.
what is the arithmetic expansion syntax?
”$(( expression )) “
eg.
echo “$(( (3 + 4) * 5 ))”
(returns 35)
what does this return?
nums1=( $(echo “one two three”) )
nums2=( “$(echo one two three)” )
echo ${#nums1[@]} ${#nums2[@]}
3 1
What’s IFS?
Internal Field Separator.
Default is whitespace (space, tab, newline) (IFS=$’ \t\n’)