linux運維実戦練習-2016年1月19日-2月3日課程作業
1、 centos6 ;
a,POST: ,ROM,BIOS
b,Boot seuquence: ,
c,Boot loader: , MBR ,GRUB
Boot loader : , , , 。
d,kernel: ,
( ram disk )
/sbin/init
2、 /etc/rc.d/sysinit ;
a,
b,
c, udev selinux
d, /etc/fstab
e,
f,
g, swap
h, /etc/sysctl.conf
i, lvm software raid
j,
k,
3、 sed awk ;
sed:
sed: Stream EDitor, :
:
sed [option]... 'script' inputfile...
script:
‘ ’
-n:
-e:
-f: /PATH/TO/SCRIPT_FILE:
-r:
-i:
(1) :
(2) :
#:
/patern/:
(3) #,#
#,+#
/pat1/,/pat2/
#,/pat1/
(4)~
1~2
2~2
d:
# sed '1,4d' /etc/fstab
p:
# sed -n '/^UUID/p' /etc/fstab
a \'text' :
# sed -n '/^UUID/p' /etc/fstab
i \'text': :
# sed '/^UUID/i \#hello sde.
welcom' /etc/fstab
c \'text'
# sed '/^UUID/c \#hello sde.
welcom' /etc/fstab
w /path/to/somefile:
#sed '/^UUID/w /tmp/fstab.txt' /etc/fstab
r:/path/to/somefile:
# sed '6r /etc/issue' /etc/fstab
=:
# sed '/^UUID/=' /etc/fstab
!:
# sed '/^UUID/!d' /etc/fstab
s///: , ,s@@@,S###
:
g:
# sed 's@^UUID@uuid@' /etc/fstab
p:
# sed -n '[email protected]@&er@p' /etc/passwd
w: /PATH/TO/SOMEFILE:
# sed '[email protected]@&er@' /etc/passwd
h:
H:
g:
G:
x:
n:
N:
d:
D:
# vim test.txt
# sed -n 'n;p' test.txt
2
4
6
# sed '1!G;h;$!d' test.txt
6
5
4
3
2
1
# sed '$!n;$!D' /etc/fstab
# sed '$!n;$!D' /etc/fstab
# sed 'G' test.txt
1
2
# sed 'g' test.txt
# sed '/^$/d;G' test.txt
# sed 'n;d' test.txt
# sed '1!G;h;$p' test.txt
# sed -n '2~2p' test.txt
# sed -n '2~2p' test.txt
wak
wak: ,
AWK: Aho, Weiberger,Kernighan ---New AWK , Nawk
GNU awk, gawk
gawk - pattern scanning and processing language
: gawk [options] 'program' FILE...
program: PATTERN{ACTION STATEMENT}
print, printf
:
-F:
-v: var=value:
1,print
print item1,item2,...
:
(1)
# tail -3 /etc/fstab | awk '{print $2,$4}'
(2) item , , , awk
# tail -3 /etc/fstab | awk '{print "hello" $2,$4,6}'
(3) item, print $0;
# tail -3 /etc/fstab | awk '{print }'
2,
FS:input field seperator,
# awk -v FS=':' '{print $1}' /etc/passwd
OFS:output field seperator,
# awk -v FS=':' -v OFS=':' '{print $1,$3,$7}' /etc/passwd
RS:input record seperator:
# awk -v RS='' '{print}' /etc/passwd
ORS: output record seperator:
# awk -v RS='' -v ORS='#' '{print}' /etc/passwd
NF: number of field,
# awk '{print NF}' /etc/fstab
{print NF}, {print $NF}
NR:number of record ,
# awk '{print NR}' /etc/fstab
FNR: :
# awk '{print FNR}' /etc/fstab /etc/issue
FILENAME:
# awk '{print FILENAME}' /etc/fstab /etc/issue
ARGC:
# awk 'BEGIN{print ARGC}' /etc/fstab /etc/issue
ARGV: ,
# awk 'BEGIN{print ARGV[0]}' /etc/fstab /etc/issue
(1) -v var=value
(2) program
# awk -v test='hello gawk' 'BEGIN{print test}' /etc/fstab
# awk 'BEGIN{test="hello gawk";print test}'
3,printf
:printf FORMAT, item1,item2,...
(1) FORMAT
(2) , ,
(3)FORMAT item
:
%c: ASCII
%d,%i:
%e,%E:
%f:
%g,%G:
%s:
%u:
%%: %
# awk -F: '{printf "Username:%s
",$1}' /etc/passwd
# awk -F: '{printf "Username:%s,UID:%d
",$1,$3}' /etc/passwd
:
#[.#]: , #
%3,1f
# awk -F: '{printf "Username:%15s,UID:%d
",$1,$3}' /etc/passwd
-:
# awk -F: '{printf "Username:%-15s,UID:%d
",$1,$3}' /etc/passwd
+:
4,
x+y ,x-y ,x*y ,x/y ,x^y, x%y,
-x
+x:
: ,
=, +=, -=, *=, /=, %=,^=,
++, --,
>, >=, < , <=, !=, ==
:
~:
!~:
&&
||
!
:
function_name(argu1,argu2,...)
selector?if-ture-exprssion: if-false-expression
# awk -F: '{$3>=1000?usertype="Common user":usertype="Sysadmin or Sysuser";printf "%15s:%-s
",$1,usertype}' /etc/passwd
5, PATTERN
(1)empty: ,
(2)/regular expression/: :
#awk '/^UUID/{print $1}' /etc/fstab
# awk '!/^UUID/{print $1}' /etc/fstab
(3)relational expression: : “ ”“ ”: “ ”
: 0 ,
# awk -F: '$3>=1000{print $1,$3}' /etc/passwd
# awk -F: '$3<1000{print $1,$3}' /etc/passwd
# awk -F: '$NF=="/bin/bash"{print $1,$NF}' /etc/passwd
(4)line ranges:
startlin,endline /pat1/,/pat2/
:
# awk -F: '(NR>=2&&NR<=10){print $1}' /etc/passwd
(5)BIGIN/END
BIGIN{}:
# awk -F: 'BEGIN{print" username uid
------------"}{print $1,$3}' /etc/passwd
END{}:
# awk -F: 'BEGIN{print" username uid
------------"}{print $1,$3}END{print "++++++++++++++++++
end "}' /etc/passwd
# awk -F: '{print" username uid
------------";print $1,$3}' /etc/passwd
6, action
(1) Expressions
(2)Control statements: if while...
(3)Compound statements:
(4)input statements
(5)output statements
7,
if (condition) {statments}
if(condition) {statments} else {statments}
while (condition) {statments}
do {statments} while (condition)
for(expr1,expr2,expr3,) {statments}
continue
delete array[index]
delete array
exit
{ statments }
7.1 if-else
: if (condition) {statments} [else statement]
# awk -F: '{if($3>=1000)print $1,$3}' /etc/passwd
else
# awk -F: '{if($3>=1000) {printf "Common user: %s
",$1} else {printf "root or Sysuser: %s
",$1}}' /etc/passwd
# awk -F: '{if($NF=="/bin/bash") print $1}' /etc/passwd
# awk '{if(NF>5) print $0}' /etc/fstab
# df -h | awk -F[%] '/^\/dev/{print $1}' | awk '{if($NF>=20) print $1}'
: awk
7.2 while
:while (condition) {statments}
“ ”, , “ ”
: :
# awk '/^[[:space:]]*linux16/{i=1;while(i<=NF) {print $i,length($i);i++}}' /etc/grub2.cfg
# awk '/^[[:space:]]*linux16/{i=1;while(i<=NF) {if(length($i)>=7) {print $i,length($i)};i++}}' /etc/grub2.cfg
73 do-while
:do statement while (condition)
:
7.4 for
: for(expr1,expr2,expr3,) {statments}
for(variable assignment; condition;iteration process) {for-body} # awk '/^[[:space:]]*linux16/{for(i=1;i<=NF;i++) {print $i,length($i)}}' /etc/grub2.cfg
: for (var in array) {for-body}
7.5 swith
:switch(expression) {case VALUE1 or /REGEXP/: statement; case VALUE2 or /REGEXP2/:statement;...
default: statement}
7.6 break continue
break [n]
continue
7.7 next
:
# awk -F: '{if($3%2!=0) next; print $1,$3}' /etc/passwd
8 array
:array [index-expression]
index-expression
(1) ,
(2) , ,awk , “ ”
, wget"index in array"
weekday[mon]="Monday"
# awk 'BEGIN{weekdays["mon"]="Monday";weekdays["tue"]="Tuesday";print weekdays["mon"]}'
, for
for(var in array) {for-body}
# awk 'BEGIN{weekdays["mon"]="Monday";weekdays["tue"]="Tuesday";for(i in weekdays) {print weekdays[i]}}'
:var array
state["LISTEN"]++
state["ESTABLISHED"]++
# netstat -tan | awk '/^tcp\>/{state[$NF]++}END{for(i in state) {print i,state[i]}}'
# awk '{ip[$1]++}END{for(i in ip) {print i,ip[i]}}' /var/log/httpd/access_log
1: /etc/fstab
# awk '/^UUID/{fs[$3]++}END{for(i in fs) {print i,fs[i]}}' /etc/fstab
2:
# awk '{for(i=1;i<=NF;i++){count[$i]++}}END{for(i in count) {print i,count[i]}}' /etc/fstab
9,
9.1
rand(): 0 1
:
length([s]):
sub(r,s,[t]): r t , s
gsub(r,s,[t]): r t , s
split(r,s,[t]): r s, a
# netstat -tan | awk '/^tcp\>/{split($5,ip,":");count[ip[1]]++}END{for (i in count) {print i,count[i]}}'
4、 , 10 , ( 2 )
#!/bin/bash
declare -a rand
declare -i max=0
for i in {0..9}; do
rand[$i]=$RANDOM
[ ${rand[$i]} -gt $max ] && max=${rand[$i]}
done
echo "Max: $max"
5、 1 5 /data/get_username.sh, /tmp/get_username.log ;
#crontab -e
5 1 * * 2 /tmp/zuoye/get_username.sh >> /tmp/zuoye/get_username.log
6、 : , ; , ;
#!/bin/bashif ls $1 &> /dev/null ;thenstat $1|head -2else mkdir -p $1fi
7、 , 9X9 ;
1,
#!/bin/bash
declare -i j=1
declare -i i=1
until [ $j -gt 9 ]; do
until [ $i -gt $j ]; do
echo -n -e "${i}X${j}=$[$i*$j]\t"
let i++
done
echo
let i=1
let j++
done
2,
#!/bin/bash
for((j=1;j<=9;j++)); do
for((i=1;i<=j;i++))do
echo -e -n "${i}X${j}=$[$i*$j]\t"
done
echo
done
8、 dns , dns , ip 。
DNS Domain Name Service (c/s, 53/udp, 53/tcp): :
BIND: Bekerley Internat Name Domain, ISC (www.isc.org)
TCP :
UDP:User Datagram Protocol
: hosts
/etc/hosts
%WINDOWS%system32/drives/etc/hosts
Top Level Domain: tld
com,edu,mil, gov, net, org, int
: , (,cn, .iq, .hk, .tw)
DNS :
:
:13
Name--> IP
IP--->Name
: ,
DNS
DNS
DNS
DNS
DNS : :
DNS : DNS DNS “ ”( )
: , : ,
:
: ,
: , ,
" "
:
:
:
DNS:
Domain:
:FQDN -->IP
:IP --> FQDN
FQDN: Full Qulified Domain Name:
Client --> hosts --> DNS Server
Local Cache -->DNS Server(recursion) --> Server Cache --> Iteration( ) -->
:
:
:Resource Record,RR
:A,PTR, SOA, NS, CNAME, MX AAAA
SOA:Start Of Authority: , SOA ,
A:internet Address: : FQDN--IP
AAAA: FQDN--IPV6
PTR:PoinTeR: IP -- FQDN
NS:Name Server: DNS
CNAME:Canonical Name:
MX:Mail eXchanger:
:
: name [TTL] IN rr_type value
:
(1)TTL
(2)@
(3) , DNS
(4) , :
SOA :
name: , “magedu.com.”
value:
(1) DNS FQDN, :@
(2) : @ , . , :linux.magedu.com
(3)( TTL)
:
magedu.com. 86400 IN SOA ns.magedu.com. nsadmin.magedu.com. (
2016011901 ;
2H ;
10M ;
1W ;
1D ; TTL
)
NS:
name:
value: DNS , :ms.magedu.com.:
: NS
:
magedu.com. IN NS ns1.magedu.com.
magedu.com. IN NS ns2.magedu.com.
:
(1) name ,
(2) NS , ns , A
MX:
name:
value: (smtp )
,MX , value (0-99), , � �
:
magedu.com. IN MX 10 mx1.magedu.com.
IN MX 10 mx2.magedu.com.
: MX , MX , A
A:
name: FQDN, : www.magedu.com.
value: IP
:
www.magedu.com. IN A 1.1.1.1
www.magedu.com. IN A 1.1.1.2
mx1.magedu.com. IN A 1.1.1.3
mx2.magedu.com. IN A 1.1.1.3
:
(1)*.magedu.com IN A 1.1.1.4
magedu.com IN A 1.1.1.4
,
AAAA :
name: FQDN
value: IPV6
PTR:
name: IP, : IP ,1.1.1.2 2.1.1.1, : in-addr.arpa. :2.1.1.1.in-addr.arpa
value: FQDN
:
2.1.1.1.in-addr.arpa IN PTR www.magedu.com
2 IN PTR www.magedu.com.
: ,
CNAME:
name: FQDN
value: FQDN
:
web.magedu.com. IN CNAME www.magedu.com.
: ,
tld:
.com. IN NS ns1.com.
.com. IN NS ns2.com.
ns1.com. IN A 2.2.2.1
ns2.com. IN A 2.2.2.2
magedu.com .com ,
magedu.com. IN NS ns1.magedu.com.
magedu.com. IN NS ns2.magedu.com.
magedu.com. IN NS ns3.magedu.com.
ns1.magedu.com. IN A 3.3.3.1
ns2.magedu.com. IN A 3.3.3.2
ns3.magedu.com. IN A 3.3.3.3
glue record:
: , ;godaddy
, ?
(1) , NS , A
BIND
dns , bind, :named
bind
bind-libs
bind-utils
bind-chroot: /var/named/chroot
bind:
: /etc/rc.d/init.d/named
: /etc/named.conf, /etc/named.rfc1912.zones, /etc/rndc.key
: /var/named/zone_name.zone
:(1)
(2) named.ca
(3) ( IPV6 , ) localhost
rndc: remote name domain controller: bind , 127.0.0.1 named ,
953/tcp
:
:options{}
:logging{}
: zone , zone
zone"ZONE_NAME" IN {}
: , IP
# cp -v /etc/named.conf{,.bak}
:
#vim /etc/namec.conf
options {
listen-on port 53 { 192.168.1.8; 127.0.0.1; };
/* listen-on-v6 port 53 { ::1; }; */
allow-query { any; };
# named-checkconf
dnssec:
dnssec
#vim /etc/namec.conf
/* dnssec-enable yes;
dnssec-validation yes;
Path to ISC DLV key
bindkeys-file "/etc/named.iscdlv.key";
managed-keys-directory "/var/named/dynamic";
pid-file "/run/named/named.pid";
session-keyfile "/run/named/session.key";
*/
DNS :
(1)
zone "zone_name" IN {
type;{master;slave; hint;forward}
file "zone_name.zone";
};
(2)
:
:
:
# vim /var/named/magedu.com.zone
$TTL 1D
$ORIGIN magedu.com.
@ IN SOA ns1.magedu.com. admin.magedu.com (
2016012001
1H
5M
7D
1D )
IN NS ns1
IN NS ns2
IN MX 10 mx1
IN MX 20 mx2
ns1 IN A 192.168.1.8
ns2 IN A 192.168.1.9
mx1 IN A 192.168.1.10
mx2 IN A 192.168.1.11
www IN A 192.168.1.12
# named-checkzone "magedu.com" /var/named/magedu.com.zone
# chown :named magedu.com.zone
# systemctl start named.service
# rndc status
: dig
dig [ -t type ] name [@SERVER] [ query options ]
dig dns , , host
# dig -t A www.magedu.com @192.168.1.8
+[no] trace:
+[no]recures:
dig -x IP @server
dig -t axfr ZONE_NAME @SERVER
# dig -t axfr magedu.com @192.168.1.8
host
host [-t type] name [server]
nslookup :
nslookup [-option] [name | -] [server]
:
nslookup>
server IP: DNS server
set q=RR_TYPE:
NAME:
: :in-addr.arpa.
192.168.1.-->1.168.192.in-addr.arpa.
(1)
zone "ZONE_NAME" IN {
type {master |slave | forward};
file " ".zone
};
(2)
: MX A, AAAA , PTR
# vim /etc/named.rfc1912.zones
zone "1.168.192.in-addr.arpa" IN {
type master;
file "192.168.1.zone";
};
#vim -o magedu.com.zone 192.168.1.zone
$TTL 1D
$ORIGIN 1.168.192.IN-ADDR.ARPA.
@ IN SOA ns1.magedu.com. admin.magedu.com. (
2016012101
1H
5M
7D
1D )
IN NS ns1.magedu.com.
IN NS ns2.magedu.com.
8 IN PTR ns1.magedu.com.
12 IN PTR www.magedu.com.
11 IN PTR mx1.magedu.com.
# chmod 640 192.168.1.zone
# chown :named 192.168.1.zone
# named-checkzone "1.168.192.in-addr.arpa" 192.168.1.zone
# named-checkconf
# systemctl reload named.service
# rndc status
# dig -t axfr magedu.com @192.168.1.8
1,
2, NS :
3, , , /var/named/slaves/
4,
5, , ntp
6,bindr , ,
zone "zone_named" IN {
type slave;
mastes { master_ip; };
file "slaves/zone_named.zone";
};
:192.168.1.9
# yum install bind -y
# vim /etc/named.conf
options {
listen-on port 53 { 192.168.1.9; 127.0.0.1; };
/* listen-on-v6 port 53 { ::1; }; */
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
allow-query { any; };
recursion yes;
dnssec-enable no;
dnssec-validation no;
/* Path to ISC DLV key */
// bindkeys-file "/etc/named.iscdlv.key";
// managed-keys-directory "/var/named/dynamic";
// pid-file "/run/named/named.pid";
// session-keyfile "/run/named/session.key";
};
# vim /etc/named.rfc1912.zones
zone "magedu.com" IN {
type slave;
masters { 192.168.1.8; };
file "slaves/magedu.com.zone";
}
rndc:
rndc --> rndc (953/tcp)
rndc COMMAND
COMMAND
reload:
reload zone:
retransfer zone: ,
notify zone:
reconfig:
querylog:
trace: debug
trace LEVEL:
DNS and BIND
:
:
ops.magedu.com. IN NS ns1.ops.magedu.com.
ops.magedu.com. IN NS ns2.ops.magedu.com.
ns1.ops.magedu.com. IN NS 1.1.1.1
ns2.ops.magedu.com. IN NS 1.1.1.2
fin.magedu.com. IN NS ns1.fin.magedu.com.
fin.magedu.com. IN NS ns2.fin.magedu.com.
ns1.fin.magedu.com. IN NS 2.1.1.1
ns2.fin.magedu.com. IN NS 2.1.1.2
:
: , ,
(1) : ,
options {
forward {first|only}
forwarders
}
(2) ,
zone "zone_name" IN {
type forward;
forward {first|only}
forwarders
}
: dnssec
dnssec-enable no;
dnssec-validation no;