Unit L3 - Intermediate Linux

Download Report

Transcript Unit L3 - Intermediate Linux

Unit L5 - Advanced Linux
CIS-116
SUNY Ulster
Karl Wick
UNIX File Structure
Computer Holy Wars
Computer Holy Wars
Computer Holy Wars
Some Exercises
From Command Prompt
System Files in RAM (not on HD)





cd /proc
ls
(these are the system files in RAM)
cat cpuinfo
□ cat meminfo
System Resources
cat interrupts □ cat iomem
cat ioports | more □ cat dma
Hard Disk Information






Login as root
Type hdparm | more <enter>
You will see a list of options but it is not complete!
hdparm displays info to STDERR not to STDOUT.
Use redirect to read.
Type hdparm 2> /tmp/123 ;
more/tmp/123
Type hdparm /dev/had
The geometry line lets you compute drive size.
“sectors” x 512 = drive size in bytes.
Hard Disk Information

Type hdparm –I /dev/hda | more

Detailed technical information is displayed.
Type hdparm –t /dev/hda






Non-destructive read test minimizing the effect of
the disk cache.
Type hdparm –T /dev/hda
Non-destructive read test using the cache.
Does disk cache help?
Try –i –d and -g options
Locating the Kernel File


The Linux kernel is stored as a file on the hard or
boot disk.
Know its location is useful for


Creating a boot floppy
Modifying the boot load program

cd /boot

The first file (the largest) is probably the kernel.
There may be more than one Kernel.

□ ls –loS (Sort size rev)
Sample Kernel Files





RH 7.2  vmlinuz-2.4.7-10
RH 9.0  vmlinuz-2.4.20-6
RH 9.0  vmlinuz-2.4.20-6
RH 8.0 
802068 bytes
3193468 bytes
1122363 bytes
Check the boot loader (grub.conf or lilo.conf
to see which is used).
grub.conf
View of lilo.conf



cat /etc/lilo.conf
cat /etc/grub.conf
(Usually) the sixth line (exclusive of
comments) indicates the boot loader file.
Kernel Names




Kernels are NOT required to have specific
names. You can name one Colonel_Klink if
you wish.
You can test a file to see if it is a kernel by
using the file command.
file name
file /boot/vmlinuz-2.4.7-10
Creating a boot disk

Find the kernel name:



Insert a floppy into the drive
Copy the kernel file to the floppy




cd /boot ; ls –loS
dd if=/boot/filename of=/dev/fd0
this simplified command uses a block size of 512B
add bs=16384 to the end to speed up the copy.
Reboot with the floppy in the drive to test it.
Everything is a File

Enter these commands:






echo “Hello from tty2” > /dev/tty5
<ALT-F5> (login as student if asked)
echo “Hello to you” > /dev/tty2
<ALT-F2>
tty2 and tty5 are two of many user ports.
How many are there? Let us find out!
Users may be numerous

Enter the following commands:


Well, there can be at least ten users.





ls /dev/tty?
ls /dev/tty* | more
Hm … there appear to be hundreds. We try to restrict
ourselves to tty2  tty31.
Why not tty1? Type <ALT-F1> The screen is filled
with system messages from the GUI if it is running.
tty1 is reserved for the GUI.
Type <ALT-F2>
urandom (not uranium!)






Another special device is called urandom
Type cat /dev/urandom > /dev/tty5
Beeping is normal. Type <ALT-F5>
Type <ALT-F2> <CTRL-C> to end.
If your screen is messed up: su to student and
then su back to root to reset the screen.
Urandom spits out random numbers.
Looking at Memory Contents






The /proc directory holds a file called kcore that
is an image of RAM contents.
cd /proc ; ls –l kcore
Notice the large file size.
man strings
Read the first line
strings kcore
<CTRL-C> to end.
strings kcore | grep Linux |
more <CTRL-C> to end.
A More Complex Search

While in /proc type: strings kcore |
grep Linux | grep -i NET | less

This is a search for printable strings in kcore.
The first filter finds strings containing “Linux”
The second filter checks only those results and
finds strings containing “NET”.
Make the search case insensitive with the –i
option for grep.



Find




Find searches for files like locate but it looks in
the actual directories, not in a database.
find filename searches the current
directory tree.
Try find path filename.
find –nouser finds files without owners.
These files are a common leftover in systems
where users are deleted. If run from the root (/),
the message find: ./proc/n/fd/4: is normal.
More Find Options




find –user name finds files belonging to
a specific user.
find – group name finds files
belonging to a specific group.
find [directory] –size 1024k
finds files larger than 1024k c=bytes.
find [directory] –type l finds
link files.
find –type Arguments

d = directory
l = link
f = regular
b = block
s = socket

Check the man(ual) for a full list of options.




File Permissions and a Security Hole



If a user has write permission to a DIRECTORY,
he is able to delete ALL files in that directory,
even if they were created by others.
If a company’s policies require that all users
store files in a common directory we have a
security problem.
Enter the “sticky bit”
Special Permissions



We have been shown that permissions come
in three sections: User, Group, Others. each of
these has read, write and execute bits with
values of 4, 2 and 1 respectively if on.
There is a fourth section. Its is called “Special
Permissions” and does not show up in an ls –l
listing.
special | user | group | others
Special Permissions





Set User ID Set Group ID Sticky Bit
(like run as)
(protects files)
We will not discuss the first two here.
The sticky bit restricts access to files owned
by the user even if the user has execute
permission for the directory.
The sticky bit requires execute permission and
is set on the directory permission level …
A Sticky Bit example.



Create a directory called dir5 in student.
cd /home/student; mkdir dir5
run ls –l to see its permissions:



by default
run chmod o+t dir5
run ls –l to see its permissions:


drwxr-xr-x
drwxr-xr-t
the sticky bit is now on.
Test by building a file as Root, trying to delete as
student, removing sticky bit, trying to delete again.
The eject command (Do as Root)

Type mount to see what file systems are currently
mounted. If the CD-ROM is mounted, unmount it
umount /mnt/cdrom

Open the CDROM drive by pressing the button,
insert any CD but DO NOT close the tray!
Type eject –t /dev/cdrom



The CD file system may automatically register but
will NOT mount if you are in text mode.
Type mount to be sure. (Note: the GUI automounts)
The eject command





Type eject /dev/cdrom
The tray ejects.
Type eject –t /dev/cdrom
Type mount –t iso9660 /dev/cdrom
/mnt/cdrom. The first part is optional for
RedHat versions 7 and above.
Type mount and it will appear in the list.
(read only in most cases – why?)
The eject command





Switch to the CD ROM directory: cd
/mnt/cdrom.
Type eject /dev/cdrom
The tray does not eject.
Press the button on the tray. It does not eject.
Linux will not eject a removable device that
ANY user has for their current directory.
Low Level Formatting of Floppies

Insert a blank floppy and type
 fdformat /dev/fd0
OR




fdformat /dev/fd0u1440
The disk will format to 1440kB and will be windows
compatible.
You can also use u1680, u1680 and others. (Not
windows compatible.)
Type ls /dev/fd0* to see al of the options.
Adding the filesystem to the disk



Type mke2fs /dev/fd0
Read the messages.
Now mount the floppy filesystem:
 mount /dev/fd0 /mnt/floppy
OR

mount /dev/fd0
OR
mount –t ext2 /dev/fd0 /mnt/floppy
You can use the short version IF you have this entry in
the /etc/fstab file: /dev/fd0 /mnt/floppy ext2 defaults 0 0


A Useful Alias



What does this command do?
alias newfloppy=“fdformat
/dev/fd0u1440 ; mk2fs /dev/fd0 ;
mount /dev/fd0 /mnt/floppy”
It creates an alias that lets you format, make a
files ystem and mount a new disk with a single
command.
Wasted Space







Format and mount a floppy.
Go to its directory cd /mnt/floppy
Check the amount of free space df /dev/fd0
Read the messages. 1412 blocks, 13 used, 1327
available. Huh? Where are the rest?
List the directory ls
There should be no files and the lost+found subdirectory.
Remove lost+found
rmdir lost+found
Wasted Space

List the directory again ls

There should be no files and no sub-directories.
Check the amount of free space df /dev/fd0


Read the messages. 1412 blocks, 1 used, 1339
available. Huh? Can’t Linux count?
CD back to root then umount the floppy. cd

Write a new filesystem to the floppy


Type mke2fs /dev/fd0
Wasted Space



After writing a new filesystem to the floppy, read the
messages carefully. Look for a line that says “72 blocks
(5.00%) reserved for the super user”
Every filesystem reserves space for the superuser. This
is good in a hard drive but wasteful on a floppy.
Write a new filesystem to the floppy:
 mke2fs –m 0 /dev/fd0
Check the amount of free space:
 df /dev/fd0
1412-13 = 1399 Hooray!!!
 -m 0 is the percentage of space to reserve.

The fuser program

This program lets root see which users or daemons are
holding files open. This is also useful for debugging
programs.

Switch to vty session 3 <ALT-F3> ,login as root, then
create an empty file called sample touch sample.
Open the file and keep it open by using less:
less sample.
Switch to vty session 2 <ALT-F2> and login as
student, then create an empty file called sample2
touch sample2.


The fuser program (2)


Open the file and keep it open by using less: less
sample2.
Switch to vty session 1 <ALT-F1>. Make sure that
you are logged in as root.

Type fuser *

You will see a list of open files with pid numbers.
Example: sample
28902 (your numbers will vary)
Type ps 28902 (use your own number) to see what is
holding the file open.


kill

Using the pid numbers from the last exercise,
type kill pid for both instances of less.

Switch to sessions 2 and 3 and notice less has
been terminated for each.

<ALT-F2>
<ALT-F3>,
<ALT-F1> to return.
More User Information





Try these commands and compare their outputs:
w
who
whoami
who am i
w, who, whoami, who am i
History


In book
Additional info:






History is stored in a file called .bash_history
Current history is stored in RAM
View this with the command history
Look at the disk file with tail .bash_history
they are not the same.
The disk file is updated at logout.
History


Remember that HISTSIZE=1000 is the default
buffer size.
You can change this by adding the line
‘HISTSIZE=n’ to the file


Use an editor
OR
echo ‘HISTSIZE=5000’ >> .bashrc

It will update at the NEXT login.

echo “HISTCONTROL=ignoredups” >> .bashrc

(Do not log duplicate sequential commands)
Prompts





In the Book
More:
PS1 is the main bash prompt.
PS2 is the secondary prompt
Type echo $PS1 then echo $PS2

Does PS2 prompt look familiar?
Type echo ‘Hello <enter>

Type ‘ <enter>

Dealing with Broken Links and
ownerless files.

Add a new user to the system

useradd betty ; passwd betty (pw)

Logon as betty and create five files.

su – betty

betty]$ touch b1 b2 b3 b4 b5 ; ls –l
Dealing with Broken Links and
ownerless files.








Betty wants to remove read permissions from “others”.
chmod o-r * ; ls –l
Permissions are now –rw-re---Type exit <enter> (you are back to root)
ls –l /home/betty
Delete the user betty: userdel betty
ls –l /home/betty
Compare the outputs. The owner column now says 501
(some number) instead of betty.
Dealing with Broken Links and
ownerless files.

Type find /home –nouser


You will see a list of files formerly owned by betty.
Add a new user to the system
useradd tommy ; passwd tommy (pw)
Logon as tommy: su – tommy
Type ls –l /home/betty

Betty’s old files are now owned by tommy! Why?



Why ?

We deleted user #501 (Betty) but we did not delete
wither her home directory (/home/betty) or the files
inside of it.
User #501 is now available (users are added
sequentially beginning with #500 [RH 9])
The new user #501 inherits all of the files of the old
user #501. The files live in the OLD directory.
To find and delete un-owned files interactively, type:

find / -nouser –ok rm “{}” “;” <enter>



Dangling Links



Symbolic links point to files
Dangling links point to files that no longer exist.
Create a broken symbolic link:





touch home/tommy/hello
(make file)
ln –s /home/tommy/hello hello (make link)
rm /home/tommy/hello
(break link)
symlinks / -r / | grep dangling (find link)
symlinks –r –d /home
(delete all in /home)
Process priorities: nice and renice


The default priority for any process is 0.
The priority of any process or of any user’s
processed can be adjusted with the renice
command. n = priority number.



renice n –p pid
renice n –u user
Values for n: 0 = default, 19 = slowest, 20 =
run ONLY when nothing else is running.
negative = very high.
XWindow Performance

Open an XWindow session startx <enter>

Open a terminal window
Type x11perf | more to get a list of
options
run x11perf –all This will take many
minutes. You can use <CTRL-C> to end it.
In () is number of operations per second.



Basic Networking
ifconfig





The command ifconfig is your friend.
if config or ifconfig eth0 for status
ifconfig eth0 192.168.0.2
ifconfig eth0 netmask 255.255.255.240
ifconfig eth0 broadcast 192.168.0.15


(ifconfig gets the boradcast address wrong is the default
subnet mask for the class is not used.)
ifconfig eth0 up|down
Special Networking Commands





ifconfig cannot set a default gateway
the command route displays the routing table.
It can be used to add or del(ete) static routes
route add default gw 192.168.0.1
Spoofing a MAC address:




ifconfig eth0 down
ifconfig hw ether 00:00:0B:AD:F0:0D
ifconfig eth0 up
route add –net 192.56.76.0 netmask
255.255.255.0 dev eth0
Networking (3)





Many ethernet cards can run at either of two
speeds.
How do we know which speed we are using?
run mii-tool –v from a command prompt.
Note: The port must be up, running and have a
“link partner” before executing the test.
mii-tool –R resets the card to default config.
Networking (4)

mii-tool –w monitors the interface
 Enter the command: mii-tool -w





23:03:52 eth0: negotiated 100baseTx-FD, link ok
Unplug the ethernet cable
23:04:08 eth0: no link
Plug the cable back in
23:04:31 eth0: negotiated 100baseTx-FD, link ok
Web Sites



www.memtest86.com
www.pathname.com/fhs/FHS2.2.HTML
www.textuality.com/bonnie

www.textuality.com/bonnie/bonnie.tar.gz