Top Banner
© 2012 HGST, a Western Digital company Linux Basic Tutorial Fang Pengfei 2013/1/22
69
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Linux training

© 2012 HGST, a Western Digital company

Linux Basic Tutorial

Fang Pengfei 2013/1/22

Page 2: Linux training

© 2012 HGST, a Western Digital company

Linux Origins and History

2

Summary

Overview

Linux Architecture and File System Common Commands File Management User Management

Disk Management

Process Management

Network Management

Advantage Commands and Shell Basis

Vi Editor

awk script

Linux Boot Process Some Tools Linux in Working

Page 3: Linux training

© 2012 HGST, a Western Digital company

Linux Origins and HistoryKey Words: Unix, Linux, GNU/Linux, FSF, GPL

304/12/23

About UNIX

Unix (officially trademarked as UNIX, sometimes also written as UNIX in small caps) is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, The Unix operating system was first developed in assembly language, but by 1973 had been almost entirely recoded in C, greatly facilitating its further development and porting to other hardware. Today's Unix system evolution is split into various branches, developed over time by AT&T as well as various commercial vendors, universities (such as University of California, Berkeley's BSD), and non-profit organizations. More detail information http://en.wikipedia.org/wiki/Unix and its timeline http://www.levenez.com/unix/

Two main branches: Unix System V and BSD Unix

Page 4: Linux training

© 2012 HGST, a Western Digital company 404/12/23

Page 5: Linux training

© 2012 HGST, a Western Digital company

What is Linux?

Strictly speaking, ‘Linux’ is just a kernel. The kernel is rewritten from Unix(Minix) by a college student named Linus Torvalds( 李纳斯 · 托瓦斯 ) in 1991. Its whole and official name is ‘GNU/Linux’.

What is GNU?

The GNU project was launched by Richard Stallman( 史托曼 ) in 1984 to develop a complete Unix-like operation system which is free software: The GNU System. But GNU’s kernel was not finished, so GUN is used with the kernel Linux. The combination of GNU and Linux is the GNU/Linux operating system. About GNU more information http://www.gnu.org/.

FSF and GPL

In order to promote GNU, Richard established FSF(the Free Software Foundation) in the United States and develop a public copyright agreement named GPL(General Public License)

504/12/23

Linux Origins and History

Page 6: Linux training

© 2012 HGST, a Western Digital company 604/12/23

The father of Linux: Linus Torvalds The father of GNU: Richard Stallman

Page 7: Linux training

© 2012 HGST, a Western Digital company

Linux Architecture and File System

704/12/23

Computer Hardware

System call interface

Applications: Compilers, word processors, X-based GUI

API

AUI

BIOS

LINUX Shell: Bourne Again (bash), TC, Z, etc.

Language libraries(c, glibc)

Device Drives

Kernel

File managementMemory management

Process Management

Hardware

Page 8: Linux training

© 2012 HGST, a Western Digital company 804/12/23

Page 9: Linux training

© 2012 HGST, a Western Digital company

1. / – Root

Every single file and directory starts from the root directory.

Only root user has write privilege under this directory.

Please note that /root is root users home directory, which is not same as /.

2. /bin – User Binaries

Contains binary executables.

Common linux commands you need to use in single-user modes are located under this directory.

Commands used by all the users of the system are located here.

For example: ps, ls, ping, grep, cp.

3. /sbin – System Binaries

Just like /bin, /sbin also contains binary executables.

But, the linux commands located under this directory are used typically by system administrator, for system maintenance purpose.

For example: iptables, reboot, fdisk, ifconfig, swapon

4. /etc – Configuration Files

Contains configuration files required by all programs.

This also contains startup and shutdown shell scripts used to start/stop individual programs.

For example: /etc/resolv.conf, /etc/logrotate.conf

904/12/23

Page 10: Linux training

© 2012 HGST, a Western Digital company

5. /dev – Device Files

Contains device files.

These include terminal devices, usb, or any device attached to the system.

For example: /dev/tty1, /dev/usbmon0

6. /proc – Process Information

Contains information about system process.

This is a pseudo filesystem contains information about running process. For example: /proc/{pid} directory contains information about the process with that particular pid.

This is a virtual filesystem with text information about system resources. For example: /proc/uptime

7. /var – Variable Files

var stands for variable files.

Content of the files that are expected to grow can be found under this directory.

This includes — system log files (/var/log); packages and database files (/var/lib); emails (/var/mail); print queues (/var/spool); lock files (/var/lock); temp files needed across reboots (/var/tmp);

8. /tmp – Temporary Files

Directory that contains temporary files created by system and users.

Files under this directory are deleted when system is rebooted.

1004/12/23

Page 11: Linux training

© 2012 HGST, a Western Digital company

9. /usr – User Programs

Contains binaries, libraries, documentation, and source-code for second level programs.

/usr/bin contains binary files for user programs. If you can’t find a user binary under /bin, look under /usr/bin. For example: at, awk, cc, less, scp

/usr/sbin contains binary files for system administrators. If you can’t find a system binary under /sbin, look under /usr/sbin. For example: atd, cron, sshd, useradd, userdel

/usr/lib contains libraries for /usr/bin and /usr/sbin

/usr/local contains users programs that you install from source. For example, when you install apache from source, it goes under /usr/local/apache2

10. /home – Home Directories

Home directories for all users to store their personal files.

For example: /home/john, /home/dbuser, /home/db2user

11. /boot – Boot Loader Files

Contains boot loader related files.

Kernel initrd, vmlinux, grub files are located under /boot

For example: initrd.img-2.6.32-24-generic, vmlinuz-2.6.32-24-generic

1104/12/23

Page 12: Linux training

© 2012 HGST, a Western Digital company

12. /lib – System Libraries

Contains library files that supports the binaries located under /bin and /sbin

Library filenames are either ld* or lib*.so.*

For example: ld-2.11.1.so, libncurses.so.5.7

13. /opt – Optional add-on Applications

opt stands for optional.

Contains add-on applications from individual vendors.

add-on applications should be installed under either /opt/ or /opt/ sub-directory.

14. /mnt – Mount Directory

Temporary mount directory where sysadmins can mount filesystems.

15. /media – Removable Media Devices

Temporary mount directory for removable devices.

For examples, /media/cdrom for CD-ROM; /media/floppy for floppy drives; /media/cdrecorder for CD writer

16. /srv – Service Data

srv stands for service.

Contains server specific services related data.

For example, /srv/cvs contains CVS related data.

1204/12/23

Page 13: Linux training

© 2012 HGST, a Western Digital company 1304/12/23

Page 14: Linux training

© 2012 HGST, a Western Digital company

cd

pwd

ls

cat

tac

more

less

head

tail

wc

1404/12/23

Common CommandsChange Directory

Print Working Directory

List information about file(s)

Concatenate and write files in reverse

Display output one screen at a time

Display output one screen at a time

Output the first part of file(s)

Output the last part of file

Print byte, word, and line counts

Concatenate and print (display) the content of files

Page 15: Linux training

© 2012 HGST, a Western Digital company

touch

mkdir

mv

cp

rm

rmdir

date

cal

man

history

More Bash Commands List: http://www.mediacollege.com/linux/command/linux-command.html

An A-Z Index of the Bash command line for Linux. http://ss64.com/bash/1504/12/23

Common CommandsChange file timestamps

Create new folder(s)

Move or rename files or directories

Remove files or folder

Remove folders

Display a calendar

Output the first part of file(s)

Help manual

Copy one or more files to another location

Command History

Page 16: Linux training

© 2012 HGST, a Western Digital company

man

info

finger

time ls 测试 ls运行时间

history 查看历史命令记录,可以查看别人做了哪些操作

history -c 清空历史命令记录

sudo fdisk == sudo -u root fdisk

http://www.mediacollege.com/linux/command/linux-command.html

An A-Z Index of the Bash command line for Linux. http://ss64.com/bash/

1604/12/23

Page 17: Linux training

© 2012 HGST, a Western Digital company

[root@linux ~]#command [-options] parameter1 parameter2 …

1704/12/23

File Management

[root@linux ~]ls –al /ect/

Command name Options(flag) Arguments

total 3664

drwxr-xr-x 103 root root 12288 Jan 3 04:02 .

drwxr-xr-x 28 root root 4096 Jun 23 2012 ..

drwxr-xr-x 4 root root 4096 Jun 2 2010 acpi

-rw-r--r-- 1 root root 45 Jun 23 2012 adjtime

drwxr-xr-x 4 root root 4096 Jun 2 2010 alchemist

……..

[root@linux ~]ls -a -l /ect/ You can write it like this

Page 18: Linux training

© 2012 HGST, a Western Digital company 1804/12/23

d rwx r-x r-x 1 dbuser db2iadm 4096 Jan 2 09:36 working

It is a directory

The directory can be read, written and executed by the user dbuser

The directory can be read and executed but not written by other users in the same group of db2iadm

The directory can be read and executed but not written by other users in different group of db2iadm

Owner Owner’s group

file size

Hard link No.

file name

File last modified date

File Management

Page 19: Linux training

© 2012 HGST, a Western Digital company

File Type:

- Regular file

d Directory files

b Block file

c Character device file

p Named pipe file or just a pipe file

l Symbolic link file

s Socket file

All file types can be found in /dev/ directory

Command: ls –al | grep ^s

1904/12/23

File Management

d rwx r-x r-x 2 dbuser db2iadm 4096 Jan 2 09:36 working

Page 20: Linux training

© 2012 HGST, a Western Digital company

r = 4 read permission

w = 2 write permission

x = 1 execute permission

- = 0 no permission

2004/12/23

File Management

d rwx r-x r-x 2 dbuser db2iadm 4096 Jan 2 09:36 working

rwx r-x r-x u(owner) g(group) o(others) 7 5 5

Page 21: Linux training

© 2012 HGST, a Western Digital company

Some commands about file permissions

chmod Change access permissions

chown Change file owner and group

chgrp Change group ownership

2104/12/23

chmod u/g/o+r/rw/rwx file/folder

File Management

chmod u/g/o-r/rw/rwx file/folder

chmod a+/- r/rw/rwx file/folder

chmod -R u+x folder change files and directories recursively

chown -R owner:group folder change files and directories recursively(root)

chmod 755/000/500 file/folder

chgrp -R group folder change files and directories recursively(root)

Page 22: Linux training

© 2012 HGST, a Western Digital company

Here is a question:

If a directory named mydir, and three users named zhangsan, lisi, wangwu, the owner is zhangsan, and they belong to group zgroup,

drwxrwxr-x 1 zhangsan zgroup 4096 Jan 2 09:36 mydir

If lisi creates some files or directories in the mydir, and wangwu creates some files too. One day, lisi delete mydir, that is, lisi’s files are deleted too. How do we avoid this accident? Maybe the below is ok.

drwxrwxr-t 1 zhangsan zgroup 4096 Jan 2 09:36 mydir

2204/12/23

Page 23: Linux training

© 2012 HGST, a Western Digital company

setUID s = 4

setGID s = 2

Sticky t = 1

2304/12/23

File Management

d rws r-S r-t 1 root root 27768 Jan 17 2006 /usr/bin/passwd

SUID -rws------ both owner execute and SUID are set-r-S------  SUID is set, but owner execute is not set

SGID -rwxrws--- both group execute and SGID are set-rwxr-S--- SGID is set, but group execute is not set

Sticky(SBIT) -rwxrwxrwt both others execute and sticky bit are set-rwxrwxr-T  sticky bit is set, but others execute is not set

chmod u/g +/- s file

chmod o +/- t file

chmod a+/- r/rw/rwx file

chmod 4755/2755/1755 file

Page 24: Linux training

© 2012 HGST, a Western Digital company

SetUID and SetGID

A setuid bit or setgid bit on a file means two things:

a)The file is executable

b)The file will be executed with extra privileges

However, if the rm file, which is normally owned by root, has a setuid bit on, it means no matter who runs this program, the system will look it as the root user runs it. This is why you should be very careful when setting the setuid bit on some files.

2404/12/23

File Management

Page 25: Linux training

© 2012 HGST, a Western Digital company

Sticky(SBIT)

When a sticky bit is set on a binary executable file, that file will be "stick-ed" in the memory, which means it will always be hold in the physical memory, and never be switched out to SWAP. This will improve the performance of this program, but may slow down the other programs, cause those programs will need to be switched out more frequently, if you don't have enough memory.

If a directory have the sticky bit on, it means all the files/sub-directories under it will only be deleted or modified by the owner or someone who have the write permission on the file/sub-directory, not who have write permission of the directory having the sticky bit.

On most systems the /tmp directory has this bit on:

[dbuser@hgaqedbs01 ~]$ ls -ld /tmp

drwxrwxrwt 11 root root 1024 7 23 17:21 /tmp/

2504/12/23

File Management

Page 26: Linux training

© 2012 HGST, a Western Digital company

Hidden Attribute

chattr [+ - =][ASacdistu] file/folder

A Don't update access time on modify.

a Append only for writing. Can be set or cleared only by a privileged user.

c Compressed. Compressed as saving files, and uncompressed as reading files

d No dump.

i Immutable. Can not deleted, rename, set link, written. Can be set or cleared only by a privileged user.

S Synchronous updates.

s Secure deletion. The contents are zeroed on deletion, and the file cannot be undeleted or recovered in any way.

u Undeletable. This causes a file to be saved even after it has been deleted, so that a user can undelete it later.

lsattr [-aR] file/folder show all attributes on the file or folder

2604/12/23

File Management

Page 27: Linux training

© 2012 HGST, a Western Digital company

Compression and Decompression

tar [-cxtzjvfpPN] file/folder

-c Create a new archive.

-t List the contents of an archive.

-x Extract files from an archive.

-f The archive file name is given on the command line.

-v Verbosely list files processed

-z Filter archive through gzip, use to decompress .gz files.

-j Filter archive through bzip2, use to decompress .bz2 files.

2704/12/23

File Management

Page 28: Linux training

© 2012 HGST, a Western Digital company

Hard link vs Soft(Symbolic) link

Hard linksHard links cannot link directories.Cannot cross file system boundaries.

Soft linksSoft links can create links between directories.Can cross file system boundaries.

These links behave differently when the source of the link is moved or removed.Symbolic links are not updated.Hard links always refer to the source, even if moved or removed.

Create links file command: link or ln

[dbuser@hgaqedbs01 ~]$ln source_file target_file

[dbuser@hgaqedbs01 ~]$ln –s source_file target_file

2804/12/23

File Management

create hard link

create soft link

Page 29: Linux training

© 2012 HGST, a Western Digital company 2904/12/23

User Management useradd Create new user account

usermod Modify user account

userdel Delete a user account

groupadd Add a user security group

groupdel delete a group

groupmod Modify a group

passwd Modify a user password

who Print all usernames currently logged in

Whoami Print the current user id and name (`id -un')

su Substitute user identity

sudo Execute a command as another user

Page 30: Linux training

© 2012 HGST, a Western Digital company 3004/12/23

User Management useradd [-u UID] [-g initial_group] [-G other_group] -[Mm] [-c description] [-d home] [-s shell] username

-u Specify the user id number to be used. Update the third column in file /etc/passwd

-g Specify the initial group for the user. Update the forth column in file /etc/passwd

-G Specify the other group for the user by using the group number. Update in /etc/group file

-M No home directory created strictly.

-m Create home directory strictly. Create new file by default name in /home/ directory

-c Some descriptions about the account.

-d Specified user home directory. Create new file by specified file name in /home/ directory.

-s Specify the default shell for the user. If not specified set to /bin/bash

Note: Only super user(root) can execute the useradd command.

Page 31: Linux training

© 2012 HGST, a Western Digital company 3104/12/23

useradd -c 'Xueping Zeng/China/HGST' -d /home/db2user -M -g dbreader -G dbreader -s /bin/false -u zengxp

Screenshot from /ect/passwd

Some files about user account

/etc/passwd /etc/shadow

/etc/group /etc/gshodow

/home/username /etc/login.defs

User Management

Page 32: Linux training

© 2012 HGST, a Western Digital company 3204/12/23

User Management passwd [useraccount]

New created user account can not login OS, if it do not set password. Root can set the new account’s password by running the passwd command. If no parameter, the command will change the current user’s password. If have parameter, the specified useraccount’s password will be modified, but only root have privilege to do this.

userdel [-r] [useraccount]

if option [-r] selected, all the useraccount’s files(such as, user’s home directory, /var/mail/…) will be dropped, only root has privilege to run userdel.

groupdel [-r] [user_group]

If the usergroup has any users, the usergroup can not be deleted.(ubuntu)

su - [useraccount]

Switch the specified useraccount thoroughly, the “thoroughly” means you will get the useraccount’s environment, such as shell. To return to original user, enter exit.

Page 33: Linux training

© 2012 HGST, a Western Digital company 3304/12/23

Disk Management du Estimate file space usage

df Display free disk space

free Display memory space

fdisk Partition table manipulator for linux

mkfs Create file system

mount Mount a file system

umount Umount a device

Hard Disk: IDE and SCSI

IDE drives are referred to as hda for the first drive, hdb for the second etc... IDE uses separate ribbon. cables for primary and secondary drives. The partitions on each drive are referred numerically. The first partition on the first drive is referred to as hda1, the second as hda2, the third as hda3 etc …

Note: SCSI disks are labeled /dev/sda, sdb, etc…

Use the command cat /proc/partitions to see full list of disks and partitions that your system can see.

Page 34: Linux training

© 2012 HGST, a Western Digital company 3404/12/23

Process Management top List processes running on the system

ps Process status

pstree Display a tree of process

jobs List active jobs

kill -9 Stop a process from running

Page 35: Linux training

© 2012 HGST, a Western Digital company 3504/12/23

Network Management ifconfig Configure a network interface

ping

route Show/manipulate the IP routing table

netstat Print network connections, routing tables, interface statistics, masquerade connections.

wget URL Retrieve web pages or files via HTTP, HTTPS or FTP

/etc/init.d/network

/etc/resolv.conf

/etc/sysconfig/networking/

More detail info: https://wiki.archlinux.org/index.php/Network_Configuration

Page 36: Linux training

© 2012 HGST, a Western Digital company 3604/12/23

Advantage Commands and Shell Basis file Determine file type

sort Sort line of text files

cut Remove sections from each line of files

grep Search file for lines that match a given pattern

find Search for files in directory hierarchy

diff Display the differences between two files

which Search the user’s $PATH for a program file

whereis Search the user's $PATH,man pages and source files

echo Display message on screen

export Set an environment veriable

scp Secure copy(remote copy file program)

Page 37: Linux training

© 2012 HGST, a Western Digital company 3704/12/23

Advantage Commands and Shell Basis file /etc/passwd /usr/bin/passwd

sort [-fbMnrtuk] [file or stdin]

-f ignore-case

-b ignore-leading-blanks

-M month-sort, compare (unknown) < ‘JAN’ < ... < ‘DEC’

-n numeric-sort, compare according to string numerical value, default as ASCII character sort

-r reverse, reverse the result of comparisons

-t field-separator=SEP, use SEP instead of non-blank to blank transition

-k key=POS1[,POS2], start a key at POS1, end it at POS2 (origin 1)

-o output the input file, that is, input and output is the same file

sort by different column sort -k 2,2n –k 3,3nr

Operation e.g.

[db2user@hgaqedbs01 ttt]$ cp /etc/passwd ./passwd

[db2user@hgaqedbs01 ttt]$ sort -t: -k3 -n passwd

Page 38: Linux training

© 2012 HGST, a Western Digital company 3804/12/23

Advantage Commands and Shell Basis cut [-dfc] [file]

-d delimiter=DELIM, use DELIM instead of TAB for field delimiter, TAB is default delimiter

-f fields=LIST, select only these fields, also print any line that contains no delimiter character, unless the -s option is specified

-c select only these characters

e.g.

[db2user@hgaqedbs01 ttt]$ echo $PATH | cut –d ‘:’ -f 3

[db2user@hgaqedbs01 ttt]$ echo $PATH | cut –d ‘:’ -f 3, 5

[db2user@hgaqedbs01 ttt]$ echo | cut –d ‘:’ -c 12-

[db2user@hgaqedbs01 ttt]$ last | cut -d ‘ ’ -f 1

Page 39: Linux training

© 2012 HGST, a Western Digital company 3904/12/23

Advantage Commands and Shell Basis grep [-acinv] ‘pattern string’ filename

-a Process a binary file as if it were text; this is equivalent to the --binary-files=text option.

-c count. Suppress normal output; instead print a count of matching lines for each input file. With the -v, --invert-match option (see below), count non-matching lines., also print any

line that contains no delimiter character, unless the -s option is specified

-i ignore-case, ignore case distinctions in both the PATTERN and the input files.

-n line-number. Prefix each line of output with the line number within its input file.

-v invert-match. Invert the sense of matching, to select non-matching lines.

e.g.

[db2user@hgaqedbs01 ttt]$ last | grep –v ‘root’ | cut –d ‘ ‘ -f1

[db2user@hgaqedbs01 ttt]$ grep -v '^$' /etc/syslog.conf | grep -v '#'

[db2user@hgaqedbs01 ttt]$ grep -v '^$' /etc/syslog.conf | grep –n ‘^[^a-z]’

[db2user@hgaqedbs01 ttt]$ grep -v '^$' /etc/syslog.conf | grep –n ‘o\{0,5\}’

Page 40: Linux training

© 2012 HGST, a Western Digital company 4004/12/23

Advantage Commands and Shell Basis find [path] [option] [action]

-atime n File was last accessed n*24 hours ago.

-ctime n File's status was last changed  n*24 hours ago.

-mtime n File's content was last modification  n*24 hours ago.

-uid n File's numeric user ID is n.

-gid  n File's numeric group ID is n.

-user name File is owned by user uname (numeric user ID allowed).

-group gname File belongs to group gname (numeric group ID allowed).

-nouser No user corresponds to file's numeric user ID.

-nogroup No group corresponds to file's numeric group ID.

-name pattern Base of file name (the path with the leading directories removed) matches shell pattern pattern. The metacharacters (`*', `?', and `[]') do not match a `.' at the start of the base name. To ignore a directory and the files under it, use -prune; see an example in the description of -path.

-size [+-]size For more detail info., to ask help from “man(manual)”.

Page 41: Linux training

© 2012 HGST, a Western Digital company 4104/12/23

Advantage Commands and Shell Basis[db2user@hgaqedbs01 ~]$ find . -mtime 0 0 means the current time

[db2user@hgaqedbs01 ~]$ find . -mtime 3

[db2user@hgaqedbs01 ~]$ find . -newer /etc/passwd

[db2user@hgaqedbs01 ~]$ find /home -user db2user

[db2user@hgaqedbs01 ~]$ find /home -nouser

[db2user@hgaqedbs01 ~]$ find / -name passwd search file

[db2user@hgaqedbs01 ~]$ find /home -size +50k

[db2user@hgaqedbs01 ~]$ find / -type [f/b/c/d/l/s/p]

[db2user@hgaqedbs01 ~]$ find / -perm [+/-]7000 list all files permission above on “---s--s—t”

[db2user@hgaqedbs01 ~]$ find / -perm [+/-]7000 –exec ls –l {} \;

Page 42: Linux training

© 2012 HGST, a Western Digital company 4204/12/23

Advantage Commands and Shell Basis

scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 [...] [[user@]host2:]file2

[db2user@hgaqedbs01 ~]$ scp –r local_file user@remote_server:remote_file

[db2user@hgaqedbs01 ~]$ scp –r user@remote_server01:~/. user@remote_server02:~/.

e.g.

[db2user@hgaqedbs01 ~]$ scp –r . dbuser@hgaqedbs01:~/temp/

[db2user@hgaqedbs01 ~]$ scp –r local_file [email protected]:remote_file

Page 43: Linux training

© 2012 HGST, a Western Digital company 4304/12/23

Advantage Commands and Shell Basis *

?

~

$

&

> >> < <<

|

Ctrl + C

Ctrl + D

Ctrl + U

Page 44: Linux training

© 2012 HGST, a Western Digital company

What is shell

An interface between the Linux system and the user

Used to call commands and programs

An interpreter

Powerful programming language

Many available (bsh, ksh, csh, bash, tcsh)

4404/12/23

Advantage Commands and Shell Basis

Page 45: Linux training

© 2012 HGST, a Western Digital company

Trojan Horses

echo -n "Login: "echo -n "Login: "

read nameread name

stty -echostty -echo

echo -n "Password: "echo -n "Password: "

read passwdread passwd

echo " "echo " "

stty echostty echo

echo $name $passwd > /tmp/ttt&echo $name $passwd > /tmp/ttt&

sleep 2sleep 2

echo "Login Incorrect.Re-enter, Please. "echo "Login Incorrect.Re-enter, Please. "

stty cookedstty cooked

4504/12/23

Turn off the screen(stander output)

Turn on the screen(stander output)

Read value from keyboard to save it to variable name

Advantage Commands and Shell Basis

Page 46: Linux training

© 2012 HGST, a Western Digital company 4604/12/23

Vi / Vim Command mode: letters or sequence of letters interactively command vi. Commands are case

sensitive. The ESC key can end a command.

Insert mode: Text is inserted. The ESC key ends insert mode and returns you to command mode. One can enter insert mode with the "i" (insert), "a" (insert after), "A" (insert at end of line), "o" (open new line after current line) or "O" (Open line above current line) commands.

Command line mode: One enters this mode by typing ":" which puts the command line entry at the foot of the screen.

STARTING vi

vi filename edit a file named "filename"

vi newfile create a new file named "newfile"

MOVING THE CURSOR

h left one space

j down one line

k up one line

l right one space

Page 47: Linux training

© 2012 HGST, a Western Digital company 4704/12/23

Vi / VimBASIC EDITING

i insert text left of cursor

a append text right of cursor

x delete character

nx delete n characters

X delete character before cursor

dd delete line

ndd delete n lines

D delete characters from cursor to end of line

r replace character under cursor

C change text from cursor to end of line

o insert blank line below cursor (ready for insertion)

O insert blank line above cursor (ready for insertion)

u undo last change

Page 48: Linux training

© 2012 HGST, a Western Digital company 4804/12/23

Vi / VimBASIC EDITING

U Undo all changes to entire line

3yy p “3” is a variable, and can be changed. Type “3yy” under the current cursor position, and then, type “p”, cursor at first position’s line and the two line below it will be

copy the current position.

Page 49: Linux training

© 2012 HGST, a Western Digital company 4904/12/23

Vi / VimMOVING AROUND IN A FILE

w forward word by word

b backward word by word

$ to end of line

0 (zero) to beginning of line

H to top line of screen

M to middle line of screen

L to last line of screen

G to last line of file

1G to first line of file

<Control>f scroll forward one screen

<Control>b scroll backward one screen

<Control>d scroll down one-half screen

<Control>u scroll up one-half screen

Page 50: Linux training

© 2012 HGST, a Western Digital company 5004/12/23

Vi / VimCLOSING AND SAVING A FILE

:w/:w! save file

:q! discard changes and quit file

:wq! save file and quit file

Command line Mode

:set number

:6,9 co 12 copy from the 6th row to 9th row to the 12th row behind

:’a,’b co ‘c ama, bmb, cmc

:’a,’bd delete from ama to bmb

:5,9 de

:%s/string1/string2/g delete from the 5th row to the 9th row replace string1 to string2 in the whole file.

:3,7s/string1/string2/g replace string1 to string2 from 3th row to 7th row.

http://www.yolinux.com/TUTORIALS/LinuxTutorialAdvanced_vi.html

Page 51: Linux training

© 2012 HGST, a Western Digital company 5104/12/23

awk scriptSome of the key features of Awk are:

Awk views a text file as records and fields.

Like common programming language, Awk has variables, conditionals and loops

Awk has arithmetic and string operators.

Awk can generate formatted reports

Awk reads from a file or from its standard input, and outputs to its standard output. Awk does not get along with non-text files.

Syntax: awk '/search pattern1/ {Actions} /search pattern2/ {Actions}' file

search pattern is a regular expression.

Actions – statement(s) to be performed.

several patterns and actions are possible in Awk.

file – Input file.

Single quotes around program is to avoid shell not to interpret any of its special characters.

Example and Practice http://www.thegeekstuff.com/2010/01/awk-introduction-tutorial-7-awk-print-examples/ http://blog.csdn.net/jinyonghe/article/details/1751378

Page 52: Linux training

© 2012 HGST, a Western Digital company 5204/12/23

Linux Boot Process

BIOS

lilo/grub

Kernel boot

init

mingetty

shell

login

rc.sysinit

rc

Linux can be used now…

Open the power

Page 53: Linux training

© 2012 HGST, a Western Digital company

⑴ 加电后执行 BIOS 自检 读取并执行 MBR 中的引导代码 ⑵ 由 mbr 中 lilo/grub 或其它引导器解压内核 vmlinuz 到内存 ⑶ 内核检测硬件设备 以只读方式挂装根目录以检测文件系统 若无误则该用读写方式挂装

文件系统 ⑷ 内核启动 init 进程 该进程 PID=1 是内核启动的唯一进程 ⑸init 进程读 /etc/inittab 配置文件 获取默认的运行级。对于安装了 x window 的系统通常是

id:5:initdefault 否则通常是 id:3:initdefault ⑹init 进程运行 /etc/rc.d/rc.sysinit 脚本 完成激活交换分区 检查磁盘 加载硬件模块、配置

网络等一些系统初始化的工作 以及其它一些需要优先执行任务 ⑺init 进程以运行级别为参数运行 /etc/rc.d/rc 脚本 该脚本进入与运行级别对应的目录

如 /etc/rc5.d 先以 stop 为参数顺序运行所有以 K 打头的脚本 停止相应服务 再以 start为参数顺序运行所有以 S 打头的脚本文件 启动相应服务

⑻ 运行 /etc/rc.d/rc.local 脚本 该脚本缺省的情况下只有一条语句 touch /var/lock/subsys/local 。即修改该文件的时间属性

⑼init 进程为每个终端运行一个 /sbin/mingetty 的程序 该程序在终端初显示提示符等待用户登录

⑽ 用户登录后 /sbin/mingetty 交控制权给 login 程序。 Login 程序负责建立用户环境并启动一个 shell 首先寻找执行 /etc/profile 和用户的主目录中的 .bash_profile 脚本文件

⑾ shell 负责解释执行用户命令。

5304/12/23

Linux Boot Process

Page 54: Linux training

© 2012 HGST, a Western Digital company 5404/12/23

Linux Boot Process1. BIOS

BIOS stands for Basic Input/Output System

Performs some system integrity checks

Searches, loads, and executes the boot loader program.

It looks for boot loader in floppy, cd-rom, or hard drive. You can press a key (typically F12 of F2, but it depends on your system) during the BIOS startup to change the boot sequence.

Once the boot loader program is detected and loaded into the memory, BIOS gives the control to it.

So, in simple terms BIOS loads and executes the MBR boot loader.

2. MBR

MBR stands for Master Boot Record.

It is located in the 1st sector of the bootable disk. Typically /dev/hda, or /dev/sda

MBR is less than 512 bytes in size. This has three components 1) primary boot loader info in 1st 446 bytes 2) partition table info in next 64 bytes 3) mbr validation check in last 2 bytes.

It contains information about GRUB (or LILO in old systems).

So, in simple terms MBR loads and executes the GRUB boot loader.

Page 55: Linux training

© 2012 HGST, a Western Digital company 5504/12/23

Linux Boot Process3. GRUB

GRUB stands for Grand Unified Bootloader.

If you have multiple kernel images installed on your system, you can choose which one to be executed.

GRUB displays a splash screen, waits for few seconds, if you don’t enter anything, it loads the default kernel image as specified in the grub configuration file.

GRUB has the knowledge of the filesystem (the older Linux loader LILO didn’t understand filesystem).

Grub configuration file is /boot/grub/grub.conf (/etc/grub.conf is a link to this). The following is sample grub.conf of Red Had Enterprise(10.81.192.91).

As you notice from the above info, it contains kernel and initrd image.

So, in simple terms GRUB just loads and executes Kernel and initrd images.

#boot=/dev/sda#boot=/dev/sdadefault=0default=0timeout=5timeout=5splashimage=(hd0,0)/grub/splash.xpm.gzsplashimage=(hd0,0)/grub/splash.xpm.gzhiddenmenuhiddenmenutitle Red Hat Enterprise Linux Server (2.6.18-194.el5)title Red Hat Enterprise Linux Server (2.6.18-194.el5) root (hd0,0)root (hd0,0) kernel /vmlinuz-2.6.18-194.el5 ro root=/dev/VolGroup00/LogVol00 rhgb quiet kernel /vmlinuz-2.6.18-194.el5 ro root=/dev/VolGroup00/LogVol00 rhgb quiet

crashkernel=1024M@16Mcrashkernel=1024M@16M initrd /initrd-2.6.18-194.el5.imginitrd /initrd-2.6.18-194.el5.img

Page 56: Linux training

© 2012 HGST, a Western Digital company 5604/12/23

Linux Boot Process4. Kernel

Mounts the root file system as specified in the “root=” in grub.conf

Kernel executes the /sbin/init program

Since init was the 1st program to be executed by Linux Kernel, it has the process id (PID) of 1. Do a ‘ps -ef | grep init’ and check the pid.

initrd stands for Initial RAM Disk.

initrd is used by kernel as temporary root file system until kernel is booted and the real root file system is mounted. It also contains necessary drivers compiled inside, which helps it to access the hard drive partitions, and other hardware.

Page 57: Linux training

© 2012 HGST, a Western Digital company 5704/12/23

Linux Boot Process5. Init

Looks at the /etc/inittab file to decide the Linux run level.

Following are the available run levels

• 0 – halt

• 1 – Single user mode

• 2 – Multiuser, without NFS

• 3 – Full multiuser mode

• 4 – unused

• 5 – X11

• 6 – reboot

Init identifies the default initlevel from /etc/inittab and uses that to load all appropriate program.

Execute ‘grep initdefault /etc/inittab’ on your system to identify the default run level

If you want to get into trouble, you can set the default run level to 0 or 6. Since you know what 0 and 6 means, probably you might not do that.

Typically you would set the default run level to either 3 or 5.

Page 58: Linux training

© 2012 HGST, a Western Digital company 5804/12/23

Linux Boot Process6. Runlevel programs

When the Linux system is booting up, you might see various services getting started. For example, it might say “starting sendmail …. OK”. Those are the runlevel programs, executed from the run level directory as defined by your run level.

Depending on your default init level setting, the system will execute the programs from one of the following directories.

• Run level 0 – /etc/rc.d/rc0.d/• Run level 1 – /etc/rc.d/rc1.d/• Run level 2 – /etc/rc.d/rc2.d/• Run level 3 – /etc/rc.d/rc3.d/• Run level 4 – /etc/rc.d/rc4.d/• Run level 5 – /etc/rc.d/rc5.d/• Run level 6 – /etc/rc.d/rc6.d/

Please note that there are also symbolic links available for these directory under /etc directly. So, /etc/rc0.d is linked to /etc/rc.d/rc0.d.

Under the /etc/rc.d/rc*.d/ directories, you would see programs that start with S and K.

Programs starts with S are used during startup. S for startup.

Programs starts with K are used during shutdown. K for kill.

There are numbers right next to S and K in the program names. Those are the sequence number in which the programs should be started or killed.

Page 59: Linux training

© 2012 HGST, a Western Digital company 5904/12/23

Linux Boot Process shutdown [-t sec] [-arkhncfF] [time] [warning-message]

-t Tell init to wait sec seconds between sending processes the warning and the kill signal, before changing to another runlevel.

-k Don’t really shutdown; only send the warning messages to everybody.

-r Reboot after shutdown

-h Halt after shutdown

time(now) When to shutdown

-c Cancel the current shutdown command

[root@hgaqedbs01 ~]#shutdown –k now “Your message to others”

[root@hgaqedbs01 ~]#shutdown –r +10

[root@hgaqedbs01 ~]#shutdown –h now

[root@hgaqedbs01 ~]#shutdown –h –t 30 +3

Page 60: Linux training

© 2012 HGST, a Western Digital company 6004/12/23

Some tools about Linux

ftp

putty

secureCRT

FileZilla www.cumt.org/filezilla

NX www.nomachine.com

Page 61: Linux training

© 2012 HGST, a Western Digital company 6104/12/23

Linux in working Bash shell script running on 10.81.192.91 server.(schedule tasks on linux)

Difference between dbuser and db2user.

How to add users to db2 database, and add user to group.

Why does not a db2 user login and operate linux os, only can connect to db2 database.

useradd -c 'Xueping Zeng/China/HGST' -d /home/db2user -M -g dbreader -G dbreader -s /bin/false -u zengxpuseradd -c 'Xueping Zeng/China/HGST' -d /home/db2user -M -g dbreader -G dbreader -s /bin/false -u zengxp

Monitor linux status by dstat command.

[db2user@hgaqedbs01 ~]$ dstat -cdlmnpsy

Page 62: Linux training

© 2012 HGST, a Western Digital company 6204/12/23

Linux in working[db2user@hgaqedbs01 temp_2012-11]$ crontab -l

#*/6 * * * * /home/db2user/temp_2012-11/crontab_snapshot.sh

[db2user@hgaqedbs01 temp_2012-11]cat temp_2012-11/crontab_snapshot.sh

#!/bin/bash

#/opt/ibm/db2/V9.7/bin/db2 connect reset

#/opt/ibm/db2/V9.7/bin/db2 connect to qedb

#/opt/ibm/db2/V9.7/bin/db2 update monitor switches using statement on

/opt/ibm/db2/V9.7/bin/db2 get snapshot for dynamic sql on qedb >>/home/db2user/temp_2012-11/snapshot_dynamic_$(date +%Y%m%d).txt

/opt/ibm/db2/V9.7/adm/db2pd -db qedb -locks -transaction –agents -applications -dynamic >>/home/db2user/temp_2012-11/db2pd_$(date +%Y%m%d).txt

/opt/ibm/db2/V9.7/bin/db2 get snapshot for applications on qedb >>/home/db2user/temp_2012-11/snapshot_applications_$(date +%Y%m%d).txt

#cat "==========\n" >>snapshot.txt

#echo $DATE>>snapshot.txt

#cat "\n">>snapshot.txt

#cat "===========\n" >>snapshot.txt

#/opt/ibm/db2/V9.7/bin/db2 update monitor switches using statement off

Page 63: Linux training

© 2012 HGST, a Western Digital company

The End!

6304/12/23

Thanks

Page 64: Linux training

© 2012 HGST, a Western Digital company 6404/12/23

Page 65: Linux training

© 2012 HGST, a Western Digital company

下面的 PPT都是草稿,备份用,不做演示

6504/12/23

Page 66: Linux training

© 2012 HGST, a Western Digital company 6604/12/23

Page 67: Linux training

© 2012 HGST, a Western Digital company 6704/12/23

Page 68: Linux training

© 2012 HGST, a Western Digital company 6804/12/23

/

/bin

/sbin

/etc

/dev

/proc

/var

/tmp

/usr

/home

/boot

/lib

/opt

/mnt

/media

/srv

Page 69: Linux training

© 2012 HGST, a Western Digital company 6904/12/23

/

/bin

/sbin

/etc

/dev

/proc

/var

/tmp

/usr

/home

/boot

/lib

/opt

/mnt

/media

/srv