20 KiB
20 KiB
Linuxfun exercises
Taken from this pdf
12.9. practice: commands and arguments
- How many arguments are in this line (not counting the command itself).
touch '/etc/cron/cron.allow' 'file 42.txt' "file 33.txt"
- Is
tac
a shell builtin command ? - Is there an existing alias for
rm
? - Read the man page of
rm
, make sure you understand the-i
option of rm. Create and remove a file to test the -i option. - Execute:
alias rm='rm -i'
. Test your alias with a test file. Does this work as expected? - List all current aliases.
- Create an alias called
city
that echoes your hometown. 7. Use your alias to test that it works. - Execute
set -x
to display shell expansion for every command. - Test the functionality of
set -x
by executing your city and rm aliases. - Execute
set +x
to stop displaying shell expansion. - Remove your city alias.
- What is the location of the
cat
and thepasswd
commands? - Explain the difference between the following commands:
echo
/bin/echo
- Explain the difference between the following commands:
echo Hello
echo -n Hello
- Display A B C with two spaces between B and C.
- (optional) Complete the following command (do not use spaces) to display exactly the following output:
4+4 =8
10+14 =24
- Use
echo
to display the following exactly:??\\
13.9. practice: control operators
- Each question can be answered by one command line!
- When you type
passwd
, which file is executed? - What kind of file is that?
- Execute the
pwd
command twice. (remember 0.) - Execute
ls
aftercd /etc
, but only ifcd /etc
did not error. - Execute
cd /etc
aftercd etc,
but only ifcd etc
fails. - Echo
it worked
whentouch test42
works, and echoit failed
when the touch failed. All on one command line as a normal user (not root). Test this line in your home directory and in/bin/
. - Execute
sleep 6
, what is this command doing ? - Execute
sleep 200
in background (do not wait for it to finish). - Write a command line that executes
rm file55
. Your command line should printsuccess
if file55 is removed, and printfailed
if there was a problem. - (optional) Use echo to display
"Hello World with strange' characters \ * [ } ~ \ \ ."
(including all quotes)
14.13. practice: shell variables
- Use echo to display
Hello
followed by yourusername
. (use a bash variable!) - Create a variable
answer
with a value of 42. - Copy the value of
$LANG
to$MyLANG
. - List all current shell variables.
- List all exported shell variables.
- Do the
env
andset
commands display your variable? - Destroy your answer variable.
- Create two variables, and export one of them.
- Display the exported variable in an interactive child shell.
- Create a variable, give it the value
Dumb
, create another variable with valuedo
. Useecho
and the two variables toecho Dumbledore
. - Find the list of backslash escaped characters in the manual of bash. Add the time to your PS1 prompt.
15.3. practice: shell embedding
- Find the list of shell options in the man page of bash. What is the difference between
set -u
andset -o nounset
? - Activate
nounset
in your shell. Test that it shows an error message when using non-existing variables. - Deactivate
nounset
. - Execute
cd /var
andls
in an embedded shell. The echo command is only needed to show the result of the ls command. Omitting will result in the shell trying to execute the first file as a command. - Create the variable
embvar
in an embedded shell and echo it. Does the variable exist in your current shell now ? - Explain what
set -x
does. Can this be useful ? - (optional) Given the following screenshot, add exactly four characters to that command line so that the total output is
FirstMiddleLast
.[paul@RHEL4b ~]$ echo First; echo Middle; echo Last
- Display a long listing (
ls -l
) of thepasswd
command using thewhich
command inside an embedded shell.
16.12. practice: shell history
- Issue the command
echo The answer to the meaning of life, the universe and everything is 42.
. - Repeat the previous command using only two characters (there are two solutions!)
- Display the last 5 commands you typed.
- Issue the long echo from question 1 again, using the line numbers you received from the command in question 3.
- How many commands can be kept in memory for your current shell session?
- Where are these commands stored when exiting the shell?
- How many commands can be written to the history file when exiting your current shell session?
- Make sure your current bash shell remembers the next 5000 commands you type.
- Open more than one console (by press Ctrl-shift-t in gnome-terminal, or by opening an extra putty.exe in MS Windows) with the same user account. When is command history written to the history file ?
17.7. practice: shell globbing
- Create a test directory and enter it.
- Create the following files : (the last one has 6 characters including a space)
file1
file10
file11
file2
File2
File3
file33
fileAB
filea
fileA
fileAAA
file(
file 2
- List (with ls) all files starting with file
- List (with ls) all files starting with File
- List (with ls) all files starting with file and ending in a number.
- List (with ls) all files starting with file and ending with a letter
- List (with ls) all files starting with File and having a digit as fifth character.
- List (with ls) all files starting with File and having a digit as fifth character and nothing else.
- List (with ls) all files starting with a letter and ending in a number.
- List (with ls) all files that have exactly five characters.
- List (with ls) all files that start with f or F and end with 3 or A.
- List (with ls) all files that start with f have i or R as second character and end in a number.
- List all files that do not start with the letter F.
- Copy the value of
$LANG
to$MyLANG
. - Show the influence of
$LANG
in listing A-Z or a-z ranges. - You receive information that one of your servers was cracked, the cracker probably replaced the
ls
command. You know that theecho
command is safe to use. Can echo replace ls ? How can you list the files in the current directory with echo? - Is there another command besides
cd
to change directories?
18.9. practice: input/output redirection
- Activate the
noclobber
shell option. - Verify that
noclobber
is active by repeating anls
on/etc/
with redirected output to a file. - When listing all shell options, which character represents the
noclobber
option ? - Deactivate the
noclobber
option. - Make sure you have two shells open on the same computer. Create an empty tailing.txt file. Then type
tail -f tailing.txt
. Use the second shell to append a line of text to that file. Verify that the first shell displays this line. - Create a file that contains the names of five people. Use
cat
and output redirection to create the file and use a here document to end the input.
19.13. practice: filters
- Put a sorted list of all bash users in
bashusers.txt
. - Put a sorted list of all logged on users in
onlineusers.txt
. - Make a list of all filenames in
/etc
that contain the stringconf
in their filename. - Make a sorted list of all files in
/etc
that contain the case insensitive stringconf
in their filename. - Look at the output of
/sbin/ifconfig
. Write a line that displays only ip address and the subnet mask. - Write a line that removes all non-letters from a stream.
- Write a line that receives a text file, and outputs all words on a separate line.
- Write a spell checker on the command line. (There may be a dictionary in
/usr/share/dict/
.)
20.11. practice: basic Unix tools
- Explain the difference between these two commands. This question is very important. If you don't know the answer, then look back at the shell chapter.
find /data -name "*.txt"
find /data -name *.txt
- Explain the difference between these two statements. Will they both work when there are 200 .odf files in /data ? How about when there are 2 million .odf files ?
find /data -name "*.odf" > data_odf.txt
find /data/*.odf > data_odf.txt
- Write a find command that finds all files created after January 30th 2010.
- Write a find command that finds all
*.odf
files created in September 2009. - Count the number of
*.conf
files in /etc and all its subdirs. - Here are two commands that do the same thing: copy
*.odf
files to /backup/ . What would be a reason to replace the first command with the second ? Again, this is an important question.cp -r /data/*.odf /backup/
find /data -name "*.odf" -exec cp {} /backup/ \;
- Create a file called
loctest.txt
. Can you find this file withlocate
? Why not? How do you make locate find this file ? - Use
find
and-exec
to rename all .htm files to .html. - Issue the
date
command. Now display the date in YYYY/MM/DD format. - Issue the
cal
command. Display a calendar of 1582 and 1752. Notice anything special ?
22.19. practice: vi(m)
- Start the
vimtutor
and do some or all of the exercises. You might need to runaptitude install vim
on xubuntu. - What 3 key sequence in command mode will duplicate the current line.
- What 3 key sequence in command mode will switch two lines' place (line five becomes line six and line six becomes line five).
- What 2 key sequence in command mode will switch a character's place with the next one.
- vi can understand macro's. A macro can be recorded with q followed by the name of the macro. So qa will record the macro named a. Pressing q again will end the recording. You can recall the macro with @ followed by the name of the macro. Try this example: i 1 'Escape Key' qa yyp 'Ctrl a' q 5@a (Ctrl a will increase the number with one).
- Copy
/etc/passwd
to your~/passwd
. Open the last one in vi and press Ctrl v. Use the arrow keys to select a Visual Block, you can copy this with y or delete it with d. Try pasting it. - What does dwwP do when you are at the beginning of a word in a sentence ?
23.9. practice: introduction to scripting
- Give each script a different name, keep them for later!
- Write a script that outputs the name of a city.
- Make sure the script runs in the bash shell.
- Make sure the script runs in the Korn shell.
- Create a script that defines two variables, and outputs their value.
- The previous script does not influence your current shell (the variables do not exist outside of the script). Now run the script so that it influences your current shell.
- Is there a shorter way to source the script ?
- Comment your scripts so that you know what they are doing.
24.7. practice: scripting tests and loops
- Write a script that uses a for loop to count from 3 to 7.
- Write a script that uses a for loop to count from 1 to 17000.
- Write a script that uses a while loop to count from 3 to 7.
- Write a script that uses an until loop to count down from 8 to 4.
- Write a script that counts the number of files ending in .txt in the current directory.
- Wrap an if statement around the script so it is also correct when there are zero files ending in .txt.
25.7. practice: parameters and options
- Write a script that receives four parameters, and outputs them in reverse order.
- Write a script that receives two parameters (two filenames) and outputs whether those files exist.
- Write a script that asks for a filename. Verify existence of the file, then verify that you own the file, and whether it is writable. If not, then make it writable.
- Make a configuration file for the previous script. Put a logging switch in the config file, logging means writing detailed output of everything the script does to a log file in
/tmp
.
26.6. practice : more scripting
- Write a script that asks for two numbers, and outputs the sum and product (as shown here).
Enter a number: 5
Enter another number: 2
Sum: 5 + 2 = 7
Product: 5 x 2 = 10
- Improve the previous script to test that the numbers are between 1 and 100, exit with an error if necessary.
- Improve the previous script to congratulate the user if the sum equals the product.
- Write a script with a case insensitive case statement, using the shopt nocasematch option. The nocasematch option is reset to the value it had before the scripts started.
- If time permits (or if you are waiting for other students to finish this practice), take a look at Linux system scripts in
/etc/init.d
and/etc/rc.d
and try to understand them. Where does execution of a script start in/etc/init.d/samba
? There are also some hidden scripts in~
, we will discuss them later.
27.15. practice: introduction to users
- Run a command that displays only your currently logged on user name.
- Display a list of all logged on users.
- Display a list of all logged on users including the command they are running at this very moment.
- Display your user name and your unique user identification (userid).
- Use
su
to switch to another user account (unless you are root, you will need the password of the other account). And get back to the previous account. - Now use
su -
to switch to another user and notice the difference. Note thatsu -
gets you into the home directory of Tania. - Try to create a new user account (when using your normal user account). this should fail. (Details on adding user accounts are explained in the next chapter.)
- Now try the same, but with
sudo
before your command.
28.13. practice: user management
- Create a user account named serena, including a home directory and a description (or comment) that reads Serena Williams. Do all this in one single command.
- Create a user named venus, including home directory, bash shell, a description that reads Venus Williams all in one single command.
- Verify that both users have correct entries in
/etc/passwd
,/etc/shadow
and/etc/group
. - Verify that their home directory was created.
- Create a user named einstime with
/bin/date
as his default logon shell. - What happens when you log on with the einstime user ? Can you think of a useful real world example for changing a user's login shell to an application ?
- Create a file named
welcome.txt
and make sure every new user will see this file in their home directory. - Verify this setup by creating (and deleting) a test user account.
- Change the default login shell for the serena user to
/bin/bash
. Verify before and after you make this change.
29.10. practice: user passwords
- Set the password for serena to hunter2.
- Also set a password for venus and then lock the venus user account with
usermod
. Verify the locking in/etc/shadow
before and after you lock it. - Use
passwd -d
to disable the serena password. Verify the serena line in/etc/shadow
before and after disabling. - What is the difference between locking a user account and disabling a user account's password like we just did with
usermod -L
andpasswd -d
? - Try changing the password of serena to serena as serena.
- Make sure serena has to change her password in 10 days.
- Make sure every new user needs to change their password every 10 days.
- Take a backup as root of
/etc/shadow
. Usevi
to copy an encrypted hunter2 hash from venus to serena. Can serena now log on with hunter2 as a password ? - Why use
vipw
instead ofvi
? What could be the problem when usingvi
orvim
? - Use
chsh
to list all shells (only works on RHEL/CentOS/Fedora), and compare tocat /etc/shells
. - Which
useradd
option allows you to name a home directory? - How can you see whether the password of user serena is locked or unlocked ? Give a solution with
grep
and a solution withpasswd
.
30.9. practice: user profiles
- Make a list of all the profile files on your system.
- Read the contents of each of these, often they source extra scripts.
- Put a unique variable, alias and function in each of those files.
- Try several different ways to obtain a shell (su, su -, ssh, tmux, gnome-terminal, Ctrl- alt-F1, ...) and verify which of your custom variables, aliases and function are present in your environment.
- Do you also know the order in which they are executed?
- When an application depends on a setting in
$HOME/.profile
, does it matter whether$HOME/.bash_profile
exists or not?
31.10. practice: groups
- Create the groups tennis, football and sports.
- In one command, make venus a member of tennis and sports.
- Rename the football group to foot.
- Use
vi
to add serena to the tennis group. - Use the
id
command to verify that serena is a member of tennis. - Make someone responsible for managing group membership of foot and sports. Test that it works.
32.4. practice: standard file permissions
- As normal user, create a directory
~/permissions
. Create a file owned by yourself in there. - Copy a file owned by root from
/etc/
to your permissions dir, who owns this file now ? - As root, create a file in the users
~/permissions
directory. - As normal user, look at who owns this file created by root.
- Change the ownership of all files in
~/permissions
to yourself. - Make sure you have all rights to these files, and others can only read.
- With
chmod
, is 770 the same as rwxrwx--- ? - With
chmod
, is 664 the same as r-xr-xr-- ? - With
chmod
, is 400 the same as r-------- ? - With
chmod
, is 734 the same as rwxr-xr-- ? - Display the
umask
in octal and in symbolic form. 11. Set theumask
to 077, but use the symbolic format to set it. Verify that this works. - Create a file as root, give only read to others. Can a normal user read this file ? Test writing to this file with vi.
- Create a file as normal user, give only read to others. Can another normal user read this file ? Test writing to this file with
vi
. 13. Can root read this file ? Can root write to this file withvi
? - Create a directory that belongs to a group, where every member of that group can read and write to files, and create files. Make sure that people can only delete their own files.
33.5. practice: sticky, setuid and setgid bits
- Set up a directory, owned by the group sports.
- Members of the sports group should be able to create files in this directory.
- All files created in this directory should be group-owned by the sports group.
- Users should be able to delete only their own user-owned files.
- Test that this works!
- Verify the permissions on
/usr/bin/passwd
. Remove thesetuid
, then try changing your password as a normal user. Reset the permissions back and try again. - If time permits (or if you are waiting for other students to finish this practice), read about file attributes in the man page of
chattr
andlsattr
. Try setting the i attribute on a file and test that it works.
35.6. practice: links
- Create two files named
winter.txt
andsummer.txt
, put some text in them. - Create a hard link to
winter.txt
namedhlwinter.txt
. - Display the inode numbers of these three files, the hard links should have the same inode.
- Use the
find
command to list the two hardlinked files - Everything about a file is in the inode, except two things : name them!
- Create a symbolic link to
summer.txt
calledslsummer.txt
. - Find all files with inode number 2. What does this information tell you ?
- Look at the directories
/etc/init.d/
/etc/rc2.d/
/etc/rc3.d/
... do you see the links ? - Look in
/lib
withls -l
... - Use
find
to look in your home directory for regular files that do not(!) have one hard link.