Professional Documents
Culture Documents
Lesson 10 Exercises
10.1 - Learning Basic Scripting Syntax (answer each question, highlight answers in YELLOW) (4 marks)
2) In the script you just created, the top line has a special meaning, what is it? #!/bin/bash
#!/bin/bash will tell the system to use the bash shell to interpret the script while it's in execution.
# 3a. What do you enter to run it? Give your answer here:
./hello.sh
# 3b. Is there another way to do it without making the file have execute permissions (x) – yes or no? If yes, enter your line here:
Yes, I can run it without making the file have execute permissions by using /bin/bash hello.sh
4) Looking at the script, it seems pretty useless because typing this will do the same thing: echo Hello World
Yes, We can write only echo Hello World because others we add are the comments and by default bash shell will be used to
interpret the code.
10.2 - Variables & Basic Script Operations (highlight answers in YELLOW) (10 marks)
NOTE: Since the Bourne Again Shell (Bash) is a superset of sh (the Bourne Shell), all sh commands will also work in bash - but not vice
versa. (tldp.org)
For each, try your answer first in a script, then paste it in here. A user should be able to take your lines and have it run perfectly on
the Linux command line.
NOTE: You do not have to include the #!/bin/bash line in the Q1 - 5 answers.
1.) How do you define a variable called x and assign it a value of 10 and print it on screen?
sudo vim hello.sh
In file hello.sh I entered x=10
echo $x
To run this code: /bin/bash hello.sh
2) How do you define a variable called xn and assign it “Linux” and print it on screen?
xn=”Linux”
echo $xn
To run this code: /bin/bash hello.sh
3) How do you print the sum of two numbers, let’s say 6 and 3?
a=6
b=3
sum=$(( $a + $b))
echo $sum
To run this code: /bin/bash hello.sh
4) How do you define two variables x=20, y=5 then print the division of x and y (i.e. x/y)?
x=20
y=5
divide=$(( $x/ $y ))
echo $divide
To run this code: /bin/bash hello.sh
6) Write a script to see current date, time, username and current directory.
#!/bin/bash
#!/bin/bash
echo `date +”%D”`
echo `date +”%T”`
echo `whoami`
echo `pwd`
To run this code: /bin/bash hello.sh
7) Using the above question, create a menu which allows the user to select either the current date, time, username or current
directory. After the script performs the option, it ends. Put your script here:
#!/bin/bash
printf “Menu:\n
1.Current Date\n
2.Time\n
3.Username\n
4.Current Directory\n\n
Enter option (1 - 4):”
read numbers
if test “$numbers” = “1”
then echo `date +”%D”`
elif test “$numbers” = “2”
then echo `date +”%T”`
elif test “$numbers” = “3”
then echo `whoami`
else test “$numbers” = “4”
echo `pwd`
fi
To run this code: /bin/bash hello.sh
8) Write a script that asks a user what their login name is and writes it to a file called loginname_file. Put your script here:
#!/bin/bash
echo “Please enter your login name: ”
read username
cat>loginname_file
$username
9) For the following, create a script using the code below. Give a screenshot of its execution.
#!/bin/bash
for i in 1 2 3 4 5
do
echo "looping .... number $i"
done
10) For the following, create a script using the code below. Run it, enter 2 strings before quitting. Give a screenshot of its
execution.
#!/bin/bash
INPUT_STRING=hello
while [ "$INPUT_STRING" != "bye" ]
do
echo "Please type something in (bye to quit)"
read INPUT_STRING
echo "You typed: $INPUT_STRING"
done
11) For the following, create a script using the code below. Run it with 5 different inputs. Give a screenshot of its execution.
#!/bin/bash
while read f
do
case $f in
hello) echo English ;;
howdy) echo American ;;
ciao) echo Italian ;;
bonjour) echo French ;;
*) echo Unknown Language: $f ;;
esac
done < myfile
1a. Create a cron job that displays the current time to every user’s console at 1 hour intervals. Details: set the first execution time to
4 minutes from now; ex.: if the time now is 1.16 pm, use 1.20 pm, 2nd message comes at 2.20 pm, 3rd one at 3.20 pm and so on
(every hour). Look at the ‘wall’ command. To test your entry:
- open 3 consoles and login with 3 different users
- use mesg command on each console to enable messaging
- create your cron job
- verify at the specified time that everyone got the message (the current time)
Grab screenshots from 2 of your consoles showing the same message sent as a result of your cron job; paste them here:
Example:
2a. Create a Cron job that checks for invalid login (user login with wrong password) attempts every 10 minutes.
The attempts are logged in /var/log/auth.log. When an invalid attempt is found, copy the message from the log to a file under your
home directory. To verify your job works: try 2 invalid login attempts from 2 different users, spaced 15 minutes apart. Paste any log
messages you get, right here:
Note: The cron daemon does not know your screen display name - ex.: if your display name is /dev/pts0, cron is not told that, so
won't display things to your screen. It is better to write output to a file for viewing later. The question does not require you to display
to screen - the message written to a file is good enough.
b. Put your cron job entry here:
• 2. Create Partitions
• 3. Create physical volumes
• 4. Create volume group
• 5. Create Logical Volumes
• 6. Create File system on logical volumes
• 7. Edit /etc/fstab
• 7.1. Mount logical volumes
• 8. Extend logical volume
• 9. Remove logical volume
This article describes basic logic behind a Linux logical volume manager by showing real examples of
configuration and usage. Although Debian Linux will be used for this tutorial, you can also apply the same
command line syntax with other Linux distributions such as Red Hat, Mandriva, SuSe Linux and others.
This is what we are going to do
Create Partitions
For this Linux lvm example you need an unpartitioned hard disk /dev/sdb. First you need to create physical
volumes. To do this you need partitions or a whole disk. It is possible to run pvcreate command on /dev/sdb,
but I prefer to use partitions and from partitions I later create physical volumes. (Normally you would
physically add another hard drive or add one virtually in VMware or Virtualbox but that is not possible with the
cloud so use one of the 5 hard drives that you have and note the device number (e.g., such as /dev/sdc).
Use your preferred partitioning tool to create partitions. In this example I have used cfdisk. (You may use ‘fdisk’
to create the partitions now. Make them 1.8 and 3.2 gb as shown above.).
Partitions are ready to use.
To include both partitions at once you can use this command: (skip this one and do the ‘vgextend’ below)
# vgcreate mynew_vg /dev/sdb1 /dev/sdb2
Feel free to add new physical volumes to a volume
group by using the vgextend command.
# vgextend mynew_vg /dev/sdb2
Q2. Run ‘vgextend’ and ‘vgdisplay’ and paste your output here:
Q3. Run ‘lvdisplay’ and paste your output for both logical volumes here:
Note: the -m option specifies the percentage reserved for the super-user, set this to 0 if you wish not to waste
any space, the default is 5%.
Edit /etc/fstab
Add an entry for your newly created logical volume into /etc/fstab
The command above does not actually increase the physical size of volume, to do that you need to:
# resize2fs /dev/mynew_vg/vol01
Look at the figure below to see what problems you may encounter when extending a volume:
Q6. Paste your ‘resize2fs’ command and its output:
Q7. Paste output here proving that ‘root’ can write to your new directory:
Q8. Paste your ‘lvremove’ and ‘lvdisplay’ commands and their output:
You are done part 10.4. Capture your history file of commands done in this part and paste it here.
Lesson 11 Exercises
This exercise requires 2 Linux VMs – they will be referred to as ’VM1’ and ‘VM2’. Do all your work on
‘VM1’ except where noted. Answer the questions in yellow.
NOTE: It is always safest for your computer, when running multiple VMs, to allow one VM to complete
the startup, before starting up the next VM – start up your VMs one after the other now.
‘Iptables’ is the implementation of a firewall in Linux. There are a couple of ways to add rules to iptables.
Execute this command (as ‘root’) to list the current rules: iptables -L
Take a look here: https://help.ubuntu.com/community/IptablesHowTo for some information on the options that
you can use for iptables.
4. Which protocols are involved in the rule? UDP, TCP and all
Next, verify that you can login to VM1 from VM2 using SSH. Then verify you can access a website using the browser. We
will do both these steps using the rules below. Do not move on until both these services are working.
This will allow any input coming on port 22 (ssh) by using the tcp protocol to be accepted.
We would probably like for ping to work as well so add that rule:
iptables -L
5. Test with VM2 whether ssh allows you to login to VM1 using the VM1 hostname – ex.: ssh jack@mylinux-vm -
Remember that Ctrl-C can be used to terminate a process. Can you login? [yes/no]
So far, we’ve accepted these services: ssh, http, and icmp... all others have been dropped. DNS is a service at the
‘Application’ layer of the TCP/IP Network model. That means it has been dropped as well. DNS is needed to do ‘name
resolution’ - take a name like ‘eagle-vm’ or ‘sheridancollege.ca’ and resolve it to its corresponding IP address. In this
step, DNS was not available, so ‘ssh’ reported the error: ‘Could not resolve hostname...’
7. Now try login using VM1’s IP address – ex.: ssh jack@192.168.33.7 - explain the result you got:
9. Sheridan’s IP address is 142.55.7.60. Enter it at the browser and explain the result:
Now modify the /etc/iptables-save file and remove the entry for port 22.
As a second option for adding rules, you can modify that file, add/remove entries and then restore it to memory with:
Add the rule back (make sure that the rule is listed above the “-A INPUT -j DROP” in the iptables-save file). Do not forget
to restore the iptables configuration to memory.
Let's explore some more advanced options now.
What will the following commands do? For help, see the man page and also
http://manpages.ubuntu.com/manpages/xenial/man8/iptables-extensions.8.html to get an understanding of “-m”
option.
In the next line replace the ‘eth1’ interface with your first interface’s name, then run it:
!!! REMOVE THE IPTABLES RULES BEFORE PROCEEDING TO WORK ON THE REST OF THE ASSIGNMENT: iptables -F
INPUT (then reboot your Linux VM).
NOTE: It is always safest for your computer, when running multiple VMs, to allow one VM to complete the startup,
before starting up the next VM – start up your VMs one after the other now.
Verify: you should be able to ‘ssh’ from VM2 into VM1 (get on VM2, try to login to VM1). On your Linux VM1, add the
Linux VM2 IP addresses to ‘/etc/hosts.deny’ with the line: ALL: xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx is the ip address of
your Linux VM2 – for multiple addresses, separate them with a comma).
Try to connect from your Linux VM2 to your Linux VM1 with ‘ssh’.
13. You should not be allowed to login. Do not move on until you get the error appearing. Consult your teacher if you
are stuck here. Put a screenshot of your ‘ssh’ command and its output:
On your Linux VM1, add all the Linux VM2 IPs to /etc/hosts.allow with the line: sshd: xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx
is the ip of your VM2 – use a comma-separated list if needed)
Try to connect from your Linux VM2 to your Linux VM1 again.
You are done. Highlight answers in YELLOW and submit this document to Slate.