Version 27 (modified by 8 years ago) ( diff ) | ,
---|
Linux Hands - On
Through this hands-on you will install Ubuntu Server on a vm and you will make additional two clones of the installed ubuntu server for your lab sessions in next two days.
Setting up virtualbox
Installation of virtualbox and Downloading Ubuntu iso image
- Download and install virtualbox 5.0.x
- Download Ubuntu image from the LEARN mirror
- Note down the location of downloaded iso file
Creating the VM
- Start virtualbox and Click on New button (at top-right) to create new virtual machine
- Enter name of the VM as: pc.'your domain'.ws.learn.ac.lk. You can get your domain from here
eg: linux.user1.ws.learn.ac.lk
- Select OS Type: Linux
- Select Version: Ubuntu (64-bit)
- Then click on Continue button
- Set VM's memory size to 1024MB and click on Continue button
- Set VM's hard disk option to Create a virtual hard disk now and then click on Continue
- Select disk type to VDI
- Select storage type to Fixed size and Continue
- Make sure virtual hard disk file name in following format pc.'your domain'.ws.learn.ac.lk
- adjust the disk size to 12.0GB and click on Create to create the VM
This might take couple of minutes
(Note down the location of vdi image file when virtualbox flashes it on the screen)
Setting up Network Interface
- Select the VM from left panel on Virtual box, right click and open Settings
- Click on the Network title
- On Adapter 1 While Enable Network Adapter selected choose Attached to be Bride Adapter. This virtual interface will work as the WAN port of the firewall (Can be connect from out side).
Setting up boot device and Booting
- Click on Storage title and select CD ROM icon with Caption "Empty" under the Controller:IDE, Click on CD ROM icon under the Attribute on the left side to select
- Choose Virtual Optical Disk File
- Locate the Ubuntu CD Image file you downloaded from the LEARN ftp. Press OK to close the settings window.
- Right click on VM and select Start to make a Normal Start. You should now see a separate window with Installation screen"
Installation
Initial Installation options
- Select English as language for the installation wizard
- Select Ubuntu Server
- Select English for VM's OS language
- Select location by Others->Asia->Sri Lanka
- Keep locales as default United States
- Press No for configure Keyboard
- Select English (US) for country of origin for the keyboard
Now it might take sometime to detect the hardware and load the necessary Linux modules
Note that it configure network with DHCP
- Type host name as: pc.'your domain'.ws.learn.ac.lk
- When it asked, add a User by entering Your Name, your username, password (Please give a strong password. This VM will be directly exposed to the internet)
- You may select No for not to encrypt home directory
- Select Yes confirm the time-zone
Disk Configuration
Your have 10GB hard drive previously configured for your VM
- Select Manual
- Select the Disk SCSI3(0,0,0) (sda) - 12.0 GB ATA VBOX HARDDISK and Select Yes to create new partition table
- Then select Free Space just below SCSI3(0,0,0) (sda) to create following partitions and mount them as follows.
- Select Create new partition
- Partition size: 512MB ---> Continue
- Type : Primary
- Location : Begining
- Use as : Ext4
- Change Mount point : /boot
- Bootable flag : on
- Select "Done setting up the partition"
- Again select FREE SPACE to create a primary partition for Linux SWAP (two times the RAM) SWAP 2GB
- Partition size:2GB
- Type : Primary
- Location : Begining
- Use as : swap area
- Select "Done setting up the partition"
- Then select Logical Volume Manager and press Yes.
- Select create a Volume Group and name as "linux". When asked select FREE SPACE device (most probably /dev/sda free #1) and select yes for the confirmation
- Go to create logical volumes, select linux, type logical volume name as root, size : 2GB
- Repeat the same Create Logical Volume procedure and make another two volumes as usr and var
- a logical volume name : usr, size : 4GB
- a logical volume name : var, size : rest of the space
These Logical Volumes can be used as partitions containing file system root(/), /usr and /var.
Now you will have 1 Volume Group and 3 Logical Volumes.
- Once you finish creating logical volumes select finish. You will be once again presented with Disk Partition Window.
- Then Select just below the LVM VG linux, LV root - 2.0 GB Linux device-mapper (linear) option and change Use as to Ext4 , Mount point : / and Done.
- Then repeat same for the previous step in usr Volume with following options
- Use as : Ext4
- Mount point : /usr
- Done
- Then repeat same for the previous step in var Volume with following options
- Use as : Ext4
- Mount point : /var
- Done
- Then finish the partitioning and confirm it to write to the disk
Now it might take sometime to install the OS base.
Final Configuration
- At the package manager, click Continue not to use any proxy settings
Again it might take times to install rest of the packages.
- Select No automatic updates
- Select only
- standard system utilities
- OpenSSH server (select by pressing space)
Wait until it finished the installation of software
- Finally install/setup the GRUB boot loader by selecting Yes
- Finish the installation of Ubuntu by selecting Continue
VM now should restart with the newly installed OS.
You may now login using your credentials
Create the Additional VMs
For the next two days of the workshop you will need another two linux boxes. Instead of Installing OS we can clone your VM and create new VMs.
You can easily create the second VM using VM cloning. This VM is for your Web Server which will be used for hands-on tomorrow.
- First Log in to the VM and enter
sudo init 0
- Right click on the VM to select Clone option
- Assign new name as www."your domain".ws.learn.ac.lk
- Check "Reinitialize the MAC address for all network cards
- And then click on Continue
- Select Full clone
This might take sometime to create the new virtual disk image (vdi) file. Note that your new VM is same as your previous one. You have to change host name, ip addresses, etc accordingly.
- You can follow the same steps and create the third VM to use as the ldap Server. Note the following changes.
- Assign new name as idp."your domain".ws.learn.ac.lk
Note: For this Hands-on you only need your pc(pc."your domain.ws.learn.ac.lk") VM. Please shut down the additional VM's
Setting UP IP Addresses
Edit /etc/network/interfaces files to include your IP addresses in your pc
- Login to the vm. change to root user
$sudo su
- Give your password
- Edit /etc/network/interfaces
#nano /etc/network/interfaces
- Find the following line
iface eth0 inet dhcp
Note: "eth0" is the name of your virtual interface. It could also be something like "enp0s3". If so you will use that instead of eth0
- Change it to
#iface eth0 inet dhcp
- Add the following lines (Get your IP Address from iptable)
iface eth0 inet static address "Your pc IP address" netmask 255.255.255.0 network 192.248.6.0 broadcast 192.248.6.255 gateway 192.248.6.254 dns-nameservers 192.248.1.161 dns-search yourdmain
When you completed the IP settings , restart the vm and then login to confirm correct IP settings.
$ ifconfig enp0s3 Link encap:Ethernet HWaddr 08:00:27:89:cb:37 inet addr:"your IP address" Bcast:192.248.6.255 Mask:255.255.255.0 inet6 addr: 2401:dd00:1:128:a00:27ff:fe89:cb37/64 Scope:Global inet6 addr: fe80::a00:27ff:fe89:cb37/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:6497 errors:0 dropped:794 overruns:0 frame:0 TX packets:151 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:405642 (405.6 KB) TX bytes:17754 (17.7 KB)
Also confirm whether you could reach your neighbor by pinging their IP Address, DNS and Google
ping <neighbor IP> ping 192.248.1.161 ping google.com
Remote Access
Windows
- Download Putty
- type your vm's IP address in the hostname text box. set the port as 22
- Click open and You will ask the username and Password of your vm. Provide them and You will be able to remote login to your linux machine
Mac
- Open a terminal
- Type ssh <your username>@<Your VM's IP address>
- Give password and you will have a remote login to your linux machine
Playing with Linux
Working with directories
A brief overview of the most common commands to work with directories: pwd, cd, ls, mkdir and rmdir. These commands are available on any Linux (or Unix) system.
- pwd : Print Working Directory (Will tell you the location you are currently working)
- cd : You can change your current directory with the cd command
- cd : shortcut to get back into your home directory. Just typing cd without a target directory, will put you in your home directory
- cd .. : To go to the parent directory (the one just above your current directory in the directory tree)
- cd - : To go to the previous directory
- ls : You can list the contents of a directory with ls
- ls -a : To show all files. Showing all files means including the hidden files. When a file name on a Linux file system starts with a dot, it is considered a hidden file and it doesn't show up in regular file listings.
- ls -l : to display the contents of the directory in different formats or to display different parts of the directory.
- ls -lh : shows the numbers (file sizes) in a more human readable format.
- mkdir : Create new directories
- mkdir -p: To create parent directories as needed
- rmdir: To remove the directory. (Directory has to be empty)
Some exercise
- Login to your VM. and Display your current directory
- Change to /etc directory and display current directory
- Go to root directory and list the contents
- List a long listing of the root directory
- Go to your home directory
- Make directory named 'test'
- make a directory inside test directory named 'one' and make a hidden directory inside 'one' directory named '.hidden'. Make a directory inside test directory named 'one' and make a hidden directory inside 'one' directory named 'unhidden'.
- Go to 'one' directory and list the content.
- Then list all contents
- Remove directory 'unhidden'
- Go to your home and try to remove directory 'test'
$ pwd /home/dilum $ cd /etc /etc$ pwd /etc $ cd .. /$ls bin etc lib media proc sbin sys var boot home lib64 mnt root snap tmp vmlinuz dev initrd.img lost+found opt run srv usr /$ ls -l total 85 drwxr-xr-x 2 root root 4096 Nov 18 13:08 bin drwxr-xr-x 4 root root 1024 Nov 18 13:15 boot drwxr-xr-x 20 root root 4280 Nov 18 13:53 dev drwxr-xr-x 89 root root 4096 Nov 18 13:16 etc drwxr-xr-x 3 root root 4096 Nov 18 13:14 home lrwxrwxrwx 1 root root 32 Nov 18 13:04 initrd.img -> boot/initrd.img-4.4.0-21-generic drwxr-xr-x 22 root root 4096 Nov 18 13:08 lib drwxr-xr-x 2 root root 4096 Nov 18 13:03 lib64 drwx------ 2 root root 16384 Nov 18 13:02 lost+found drwxr-xr-x 3 root root 4096 Nov 18 13:03 media drwxr-xr-x 2 root root 4096 Apr 21 2016 mnt drwxr-xr-x 2 root root 4096 Apr 21 2016 opt dr-xr-xr-x 134 root root 0 Nov 18 13:52 proc drwx------ 2 root root 4096 Nov 18 13:52 root drwxr-xr-x 23 root root 820 Nov 18 13:54 run drwxr-xr-x 2 root root 12288 Nov 18 13:15 sbin drwxr-xr-x 2 root root 4096 Apr 19 2016 snap drwxr-xr-x 2 root root 4096 Apr 21 2016 srv dr-xr-xr-x 13 root root 0 Nov 18 13:52 sys drwxrwxrwt 8 root root 4096 Nov 18 15:11 tmp drwxr-xr-x 11 root root 4096 Nov 18 13:03 usr drwxr-xr-x 14 root root 4096 Nov 18 13:10 var lrwxrwxrwx 1 root root 29 Nov 18 13:04 vmlinuz -> boot/vmlinuz-4.4.0-21-generic $ cd $ pwd /home/dilum $ mkdir test/one/.hidden mkdir: cannot create directory ‘test/one/.hidden’: No such file or directory $ mkdir -p test/one/.hidden $ mkdir -p test/one/unhidden $ cd test/one test/one$ ls unhidden test/one$ ls -a . .. .hidden unhidden test/one$ rmdir unhidden/ test/one$ ls -a . .. .hidden test/one$ cd $ rmdir test rmdir: failed to remove ‘test’: Directory not empty
File Editing
The vi editor is installed on almost every Unix. Linux will very often install vim (vi improved) which is similar. Every system administrator should know vi(m), because it is an easy tool to solve problems.
vi Commands and Tips
Open: vi filename (fn=filename) vi -r filename Recover a file from a crashed session vi + filename Place the cursor on last line of file. vi +n filename Place the cursor on line "n" of file. vi +/pat filename Place cursor on line with first occurrence of "pat"tern Close: :w Write the file to disk. Don't exit. :w! Write the file to disk even if read/only. :wq Write the file to disk and exit. :wq! Write the file to disk even if read/only and quit. :q Quit the file (only if no changes). :q! Quit the file even if changes. Movement: A Move to end of line, change to insert mode. h Move 1 space backwards (back/left arrow). j Move down 1 line (down arrow). k Move up 1 line (up arrow). l Move 1 space forwards (forward/right arrow) w Move cursor to start of next word. W Same as "w". b Move cursor to start of previous word. B Same as "b". :n Go to line number "n" in the file. Editing: i Enter in to input mode. o Add a line below cursor and enter in to input mode. x Delete character (del key in some cases). D Delete line from right of cursor to end of line. dd Delete entire line. u Undo last edit or restore current line. yy Yank current line. p Put yanked text before the cursor. Searching: /pattern Search for "pattern" in the file going forwards. ?pattern Search for "pattern" in the file going backwards. n Find the next occurrence of pattern found forwards. N Find next occurrence of pattern found backwards. Copy/Cut and Paste <NUM>yyp Copy n lines to buffer, paste below cursor <NUM>yyP Copy n lines to buffer, paste above cursor <NUM>ddp Cut n lines and copy to buffer, paste below cursor <NUM>ddP Cut n lines and copy to buffer, paste above cursor
Practice Using vi
Remember The vi editor uses "modes"
The easiest thing to do if you get confused in vi is to press the ESCape key a couple of times and start over with what you were doing. Log in to your VM and...
$ cd $ vi temp.txt
vi wil create the file “temp.txt” for you. Press the "i" key to switch to input mode.
Type something like, "VI is great! I think I'll be using vi from now on instead of Word” Press <ENTER> to add lines. Type some more text
Save the file that you are in. To do this do:
Press the ESCape key for command mode Type “:wq” then hit Enter to save and quit the file (notice the “:” before the “wq”).
Copy a large file to your home directory so that you can play around with some more vi commands. We'll copy over your /etc/sysctl.conf file for this exercise. To do this do:
$ cd $ cp /etc/sysctl.conf sysctl.conf.bak
Edit the file, but let's start at the bottom of the file:
$ vi + sysctl.conf.bak
Go to the first line of the file. Notice the colon (“:”) before the “1”.
:1 <ENTER>
Go to line 10, add a new line, and add in some text:
:10 <ENTER> Press the “i” key
Add the following text:
## ## A sample comment ##
Delete the three lines you just created:
Move to the first line of new text Press the ESCape key Press “dd” to delete a line, repeat until the text is gone
Save the file, but don’t exit.
:w press <ENTER>
Practice copying and pasting text.
Go to line 12, copy 3 lines of text, go to the bottom of the file, place the text there:
ESC (go to command mode) :12 <ENTER> (go to line 12 of the file) 3yy (“yank” 3 lines of text and place in copy buffer) G (go to the end of the file) p (place the contents of the copy buffer here)
If want to undo this you would type (in command mode):
u
Go to the top of the file, replace all occurrences of “ipv4” with “ipv6”, but prompt for each change:
ESC :1 <ENTER> :%s/ipv4/ipv6/gc
Say “yes” or “no” to a few prompts then escape from this mode by pressing ctrl-c and .
Go to line 1, search for “kernel”, move to the end of the line, add some text:
ESC :1 <ENTER> /kernel SHIFT-A “text here” ESC
Now let’s exit from the file and not save the few changes we’ve made.
:q! <ENTER>
Working with Files
Files on Linux (or any Unix) are case sensitive. This means that FILE1 is different from file1, and /etc/hosts is different from /etc/Hosts (the latter one does not exist on a typical Linux computer). In Linux everything is considered as a file even a directory is a special kind of file. A small overview of some basic and important file handling commands
- file: The file utility determines the file type. Linux does not use extensions to determine the file type. The command line does not care whether a file ends in .txt or .pdf. As a system administrator, you should use the file command to determine the file type.
- cp: To copy a file, use cp with a source and a target argument.
- cp -r: To copy complete directories, use cp -r (the -r option forces recursive copying of all files in all subdirectories).
- cp -i: To prevent cp from overwriting existing files, use the -i (for interactive) option.
- mv: Use to rename a file or to move the file to another directory.
- rm: Use to remove files *rm -i:To prevent yourself from accidentally removing a file. *rm -rf:rm -r will not remove non-empty directories. However rm accepts several options that will allow you to remove any directory. The rm -rf will erase anything
Extremely dangerous Command
When you are logged on as root, be very careful with rm -rf (the f means force and the r means recursive) since being root implies that permissions don't apply to you. You can literally erase your entire file system by accident.
- less:The less command is useful for displaying files that take up more than one screen
- head/tail:You can use head to display the first ten lines of a file. and tail to display the last ten lines of a file. you can use both commands with -n and specify the number of lines
- cat:The cat command is one of the most universal tools, yet all it does is copy standard input to standard output.
Exercise
- Create numbers.txt (Containing ten lines of numbers one to ten)file with cat command.
$ cat > numbers.txt
Add the lines with enter key at the end. After inserting all the lines press Ctrl + D
one two . . ten
- View the file you created using less and cat
cat
$ cat numbers.txt one two three four five six seven eight nine ten
less
$ less numbers.txt
Press q to exit from less
- View first four lines and last four lines using head and tail commands
$ head -n 4 numbers.txt one two three four $ tail -n 4 numbers.txt seven eight nine ten
- Create another file numbers2.txt(with lines contain eleven to fifteen)
$ cat > numbers2.txt eleven twevelve thirteen fourteen fifteen
- Combine numbers.txt and numbers2.txt and create numbers3.txt. and view the file.
$ cat numbers.txt numbers2.txt > numbers3.txt $ cat numbers3.txt one two three four five six seven eight nine ten eleven twevelve thirteen fourteen fifteen
- Check the file format of the newly created file
$ file numbers3.txt numbers3.txt: ASCII text
- Copy all the created files to the test directory. and verify
$ cp numbers.txt numbers2.txt numbers3.txt test/ $ cd test $ ls numbers2.txt numbers3.txt numbers.txt one
- Make a copy of test directory as newtest and rename it to numbers
$ cd $ cp -r test/ newtest $ mv newtest/ numbers
- Delete the test directory
$ rm -rf test
File Permission
- Commands preceded with "$" imply that you should execute the command as a general user - not as root.
- Commands preceded with "#" imply that you should be working as root with "sudo"
- Commands with more specific command lines (e.g. "RTR-GW>" or "mysql>") imply that you are executing commands on remote equipment, or within another program.
chmod: Change file read write permission chown: Change the owner of the file chgrp: Change the group of the file
Reference
If you look at files in a directory using "ls -al" you will see the permissions for each file and directories. Here is an example:
drwxrwxr-x 3 root root 4096 Feb 25 09:49 directory -rwxr--r-- 12 root root 4096 Feb 16 05:02 file
So, the directory has r (read), w (write), x (execute) access for the User and Group. For Other it has r (read) and x (execute) access. The file has read/write/execute access for User and read only access for everyone else (Group and Other).
To change permissions you use the chmod command. chmod uses a base eight (octal) system to configure permissions. Or, you can use an alternate form to specify permissions by column (User/Group/Other) at a time.
Permissions have values like this:
Letter Permission Value R read 4 W write 2 X execute 1 - none 0
Thus you can give permissions to a file using the sum of the values for each permission you wish to give for each column. Here is an example:
Letter Permission Value --- none 0 --x execute 1 -w- write only (rarely used) 2 -wx write and execute (rare) 3 r-- read only 4 r-x read and execute 5 rw- read and write 6 rwx read, write, and execute 7
This is just one column. Since we have three areas of permissions (User, Group, Other), it looks like this will all 3 sets:
Permissions Numeric Description equivalent -rw------- 600 User has read & write permission. -rw-r--r-- 644 User has read & write. Group and Other have read permission. -rw-rw-rw- 666 Everyone (User, Group, Other) have read & write permission (dangerous?) -rwx------ 700 User has read, write, & execute permission. -rwxr-xr-x 755 User has read, write, & execute permission. Rest of the world (Other) has read & execute permission (typical for web pages or 644). -rwxrwxrwx 777 Everyone has full access (read, write, execute). -rwx--x--x 711 User has read, write, execute permission. Group and world have execute permission. drwx------ 700 User only has access to this directory. Directories require execute permission to access. drwxr-xr-x 755 User has full access to directory. Everyone else can see the directory. drwx--x--x 711 Everyone can list files in the directory, but Group and Other need to know a filename to do this.
Exercise
- Go to the numbers directory and get a detailed list
$ cd numbers $ ls -al total 24 drwxr-xr-x 3 dilum dilum 4096 Nov 17 13:53 . drwxrwxrwx 50 dilum dilum 4096 Nov 17 13:10 .. -rw-r--r-- 1 dilum dilum 42 Nov 17 13:08 numbers2.txt -rw-r--r-- 1 dilum dilum 91 Nov 17 13:08 numbers3.txt -rw-r--r-- 1 dilum dilum 49 Nov 17 13:08 numbers.txt drwxr-xr-x 3 dilum dilum 4096 Nov 17 13:08 one
- Change file permission as follows
$ chmod 044 numbers.txt
Now you have remove read privilege try view the file using cat
$ chmod 444 numbers.txt
Now you have set privilege as read only. Open the file via vi editor and try to edit the file
- Switch to root user
$ sudo su
- Change the ownership and group of the numbers2.txt to root and make it read only for all the other users.
- Change the ownership and group of the numbers3.txt to root and remove all the privileges from all the other users.
- Switch back to your user and try to view numbers3.txt and try to edit numbers2.txt
# chown root numbers2.txt # chgrp root numbers2.txt # chown root numbers3.txt # chgrp root numbers2.txt # chmod 700 numbers3.txt # chmod 744 numbers2.txt # su 'Your username' $ cat numbers3.txt cat: numbers3.txt: Permission denied $ cat > numbers2.txt bash: numbers2.txt: Permission denied
More Linux Commands
- who: who command will give you information about who is logged on the system.
$ who root pts/0 2014-10-10 23:07 (10.104.33.101) paul pts/1 2014-10-10 23:30 (10.104.33.101) laura pts/2 2014-10-10 23:34 (10.104.33.96)
- whoami command tells you your username
$ whoami paul
- whoami command tells you your username
- df -dh: report file system disk space usage
$ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 451G 141G 287G 33% / udev 10M 0 10M 0% /dev tmpfs 774M 19M 755M 3% /run .. ..
- man: Will give you man pages (also called manual pages) on your Unix or Linux computer
$man ls $man cp
- date: Will give you the date and time of the system
$ date Thu Nov 17 16:01:52 IST 2016
- ln: Use to create links between files. Frequently use with -s to create symbolic links
$ ln -s file2 symlink_to_file2
- ps: report a snapshot of the current processes. frequently use with -aux.
$ ps -aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.0 111476 3400 ? Ss 07:39 0:01 /sbin/init root 2 0.0 0.0 0 0 ? S 07:39 0:00 [kthreadd] root 3 0.3 0.0 0 0 ? S 07:39 1:40 [ksoftirqd/0]
Final Step (Needed for the next Hands-on)
You will use This PC (pc."your domain".ws.ac.lk) in your next pfSense hands-on as a client machine. In the next step we are going to install an GUI for linux PC. It will make things easy for you to carry on your pfSense Hands-on.
sudo apt-get update
Note:If your system sesolves to IPv6 try the following command
sudo apt-get update -o Acquire::ForceIPv4=true
This command will update your repositories.
sudo apt-get install xubuntu-desktop
This will install Xfce Desktop Environment.
sudo vi /etc/network/interfaces
edit interfaces file to change back to dhcp. Find following lines
iface enp0s3 inet static address 'Your IP address' netmask 255.255.255.0 network 192.248.6.0 broadcast 192.248.6.255 gateway 192.248.6.254 dns-nameservers 192.248.1.161 dns-search 'your domain'
Change it to following or add the following
#iface enp0s3 inet static # address 'Your IP address' # netmask 255.255.255.0 # network 192.248.6.0 # broadcast 192.248.6.255 # gateway 192.248.6.254 # dns-nameservers 192.248.1.161 # dns-search 'your domain'
Find the following line
#iface enp0s3 inet dhcp
Change it or add the following line
iface enp0s3 inet dhcp