LINUX學習

9.14課後練習:
1、安裝asianux server 4系統,具體要求以下。
一、/boot分區,大小爲100M;根分區和swap分區建立在LVM邏輯捲上,其中swap分區的名稱爲lv_swap,大小爲2048M;根分區的名稱爲lv_root,大小爲剩餘空間。
2、設置超級管理員root密碼爲123456.
3、選擇「基本服務器」套件包,並選擇安裝開發工具包,ftp服務器包,dns服務器軟件包。

2、建立/root/dir1目錄,設置dir1目錄的權限爲屬有者和工做組具備全部權限,其它用戶具備只讀權限,屬有者爲bin,工做組爲root.
mkdir /root/dir1
chmod 774 /root/dir1
chown bin.root /root/dir1
或
mkdir -m 774 /root/dir1
chown bin.root /root/dir1


3、建立/root/file.txt文件,設置權限爲全部用戶都具備讀寫權限。
touch /root/file.txt
chmod 666 /root/file.txt


4、將/etc/目錄的全部文件打包壓縮後備份到/backup目錄下,並設置文件爲etc-20150914.tar.xz
mkdir /backup
tar -cJf /backup/etc-20150914.tar.xz /etc

-J xz
-j bzip2
-z gzip
-c 打包
-x 解包
-C 解壓到指定目錄


5、查找/root目錄下全部空文件和空目錄,並將其顯示結果保存到/root/empty.txt文件中
find /root -empty >>/root/empty.txt

>  覆蓋
>> 追加

6、查找權限爲644且屬有root用戶的文件,並將其顯示結果保存到/root/root.txt文件中。
find . -perm 644 -a -user root >> /root/root.txt


7、將/backup/etc-20150914.tar.xz文件解壓到/root/dir1目錄下。
tar -xJf /backup/etc-20150914.tar.xz -C /root/dir1

8、複製/var/log目錄下的全部文件到/root/dir1目錄下。並改log目錄更名爲logic。
cp -rap /var/log /root/dir1
mv /root/dir1/log /root/dir1/logic

9、經過快速查找命令locate,查找file.txt文件的位置。
updatedb
locate file.txt

10、建立/root/dir2目錄,設置sgid權限,屬有者爲root,工做組爲bin。
mkdir /root/dir2
chmod g+s /root/dir2
chown root.bin /root/dir2

9.15課後練習:
1、vim編輯器練習。
掛載光驅到/media/目錄,並將/media/Packages目錄的文件名寫入到/root/install.sh文件中。
修改/root/install.sh,將內容修改爲如下模版。
#!/bin/bash
mount /dev/cdrom /media
rpm -ivh /media/Packages/389-ds-base-1.2.11.15-33.AXS4.x86_64.rpm --force --nodeps
rpm -ivh /media/Packages/389-ds-base-libs-1.2.11.15-33.AXS4.i686.rpm --force --nodeps
rpm -ivh /media/Packages/389-ds-base-libs-1.2.11.15-33.AXS4.x86_64.rpm --force --nodeps
rpm -ivh /media/Packages/ConsoleKit-0.4.1-3.AXS4.x86_64.rpm --force --nodeps


[root@asianux4 ~]# mount /dev/cdrom  /media/
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@asianux4 ~]# cd /media/Packages/
[root@asianux4 Packages]# ls >/root/install.sh
[root@asianux4 Packages]# vim /root/install.sh
:1,$ s #^#rpm -ivh /media/Packages/#g
:1,$ s /$/ --force --nodeps/g

[root@asianux4 Packages]# chmod +x /root/install.sh
[root@asianux4 Packages]# sh /root/install.sh


2、建立用戶alex,用戶ID號爲2345,密碼爲123321
useradd -u 2345 alex
echo 123321 |passwd --stdin alex 或 passwd alex

3、建立用戶user1,用戶ID號1000,初始組爲bin,添加到root,postfix工做組中。自家目錄爲/tmp/user1,shell爲/bin/bash.
useradd -u 1000 -g bin -G root,postfix -d /tmp/user1 -s /bin/bash user1

4、建立用戶user2,要求只容許訪問受權的資源,不容許管理linux系統。
useradd -s /sbin/nologin user2

5、建立用戶admin,要求具備root同樣的權限。
useradd -u 0 -o -g 0 admin
useradd -u 0 -o -g 0 -c "root" -d /root -s /bin/bash admin

6、建立work1工做組,並將alex,user1,user2,admin用戶添加以work1工做組。其中admin爲work1工做組的管理員。
groupadd work1
gpasswd -M alex,user1,user2,admin work1
gpasswd -A admin work1

7、設置grub的加密密碼爲123456,超時時間爲5秒。設置運行級別爲級別3。
vim /boot/grub/grub.conf
password --md5 <grub-md5-crypt>  寫在標題的上方。
timeout=5
vim /etc/inittab
id:3:initdefault:

8、每週六02:01以root身份執行備份/var/log日誌到/backup目錄下,備份文件名的格式爲log-20150915.tar.bz2.
[root@asianux4 ~]# vim /backup/backup_log.sh
#!/bin/bash
tar -cjf /backup/log-$(date +%Y%m%d).tar.bz2 /var/log

[root@asianux4 ~]# chmod +x /backup/backup_log.sh
[root@asianux4 ~]# crontab -e -u root
01 02 * * 6 /backup/backup_log.sh

9、經過yum安裝gcc,tomcat6軟件包。並將gcc,tomcat軟件包的版本信息寫入到/root/rpm.txt文件中。
[root@asianux4 ~]# vim /etc/yum.repos.d/mycdrom.repo
[mycdrom]
name=mycdrom
baseurl=file:///media
enabled=1
gpgcheck=0

[root@asianux4 ~]# yum clean all
[root@asianux4 ~]# yum repolist
[root@asianux4 ~]# yum install gcc tomcat6 -y
[root@asianux4 ~]# rpm -qa |grep -E 'gcc|tomcat6' >> /root/rpm.txt


10、設置光盤開機時自動掛載。
[root@asianux4 ~]# vim /etc/fstab
/dev/sr0    /media        iso9660        defaults    0 0
或
[root@asianux4 ~]# echo "/dev/sr0 /media/ iso9660 defaults 0 0" >> /etc/fstab


9.16課後練習:
1、對/dev/sdb磁盤作分區,要求主分區大小爲1G,linux系統類型;擴展分區大小爲剩餘空間,在擴展分區上建立兩個邏輯分區,第一個邏輯分區大小爲2G,LVM系統類型;第二個邏輯分區大小剩餘空間,swap系統類型。將/dev/sdb1格式化爲ext4,並設置開機時自動掛載到/mnt/sdb1目錄上。
關機linux系統,在vmware workstation上,添加/dev/sdb硬盤,啓動linux系統。
fdisk /dev/sdb
 建立分區:n-->p-->分區號(1-4),輸入1--->開始柱面,回車--->結束柱面,+1G-->p
           n-->e-->分區號(1-4),輸入2--->開始柱面,回車--->結束柱面,回車-->p
      n-->l-->開始柱面,回車--->結束柱面,+2G-->p
           n-->l-->開始柱面,回車--->結束柱面,回車-->p
 改變分區系統ID號:
      t-->5-->8e
           t-->6-->82
           p
           w


2、打開user10的VNC服務器端口:3.並在windows上使用vncviewer工具測試經過。
查看vncserver是否安裝
[root@asianux4 ~]# rpm -qa |grep -i vnc
tigervnc-1.1.0-8.0.2.AXS4.x86_64
tigervnc-server-1.1.0-8.0.2.AXS4.x86_64

若是沒有安裝,須要安裝vnc服務器包。
[root@asianux4 ~]# yum install tigervnc-server -y

[root@asianux4 ~]# useradd user10
[root@asianux4 ~]# su - user10
[user10@asianux4 ~]$ vncserver :3,輸入vnc的密碼。
[user10@asianux4 ~]$ vim /home/user10/.vnc/xstartup (將twm更改gnome-session)
[user10@asianux4 ~]$ vim /etc/sysconfig/vncservers (在文件末尾添加如下行)
VNCSERVERS="3:user10"
[user10@asianux4 ~]$ exit
[root@asianux4 ~]# service vncserver restart
[root@asianux4 ~]# netstat -atnup|grep -i vnc

方法一:關閉iptables防火牆
[root@asianux4 ~]# service iptables stop 關閉防火牆
[root@asianux4 ~]# chkconfig iptables off 設置iptables防火牆開機不自動運行

方法二:開啓iptables防火牆
[root@asianux4 ~]# iptables -I INPUT 3 -p tcp --dport 5903 -j ACCEPT
[root@asianux4 ~]# iptables -I IPNUT 3 -p tcp --dport 6003 -j ACCEPT
[root@asianux4 ~]# iptables -nL

在windows上,使用vncviewer鏈接

3、openssh配置實驗,要求實現如下功能。
1、打開root用戶遠程登陸功能
2、採用DSA密鑰對方式登陸,打開本地密碼登陸。
3、設置ssh的監聽端口爲2222。

[root@asianux4 ~]# vim /etc/ssh/sshd_config
更改port 2222選項。默認#port 22.
port 2222
[root@asianux4 ~]# service sshd restart
[root@asianux4 ~]# ssh-keygen -t dsa 建立dsa密鑰。
[root@asianux4 ~]# cd /root/.ssh/
[root@asianux4 ~]# cp id_dsa.pub authorized_keys

4、設置eth0網卡的第二個IP地址爲192.168.x.100/24,並設置開機時自動激活。
[root@asianux4 ~]# vim /etc/rc.local 在文件末尾添加如下行。
ifconfig eth0:1 192.168.232.100/24

5、安裝proftpd服務器,並將/var/log目錄的備份到/var/ftp/log.tar.gz文件中,並經過ftp下載到windows上。
[root@asianux4 ~]# tar -zxf proftpd-*.tar.gz -C /opt
[root@asianux4 ~]# cd /opt/proftpd-*
[root@asianux4 ~]# ./configure --prefix=/usr/local/proftpd
[root@asianux4 ~]# make;make install

[root@asianux4 ~]# mkdir /var/ftp
[root@asianux4 ~]# tar -zcf /var/ftp/log.tar.gz /var/log

[root@asianux4 ~]# groupadd nogroup
[root@asianux4 ~]# echo "192.168.232.128   asianux4" >> /etc/hosts
[root@asianux4 ~]# /usr/local/proftpd/sbin/proftpd
[root@asianux4 ~]# netstat -atnup|grep :21


6、設置到達202.1.2.3主機的全部請求包,經過eth0轉發。
[root@asianux4 ~]# route add -host 202.1.2.3 dev eth0


9.17課後練習
1、在VMware workstation中添加sdc,sdd,sde,sdf虛擬硬盤,將/dev/sdc,sdd,sde,sdf作成RAID5,sdf作爲熱備盤。校驗位爲128K,建立raid設備名稱爲/dev/md0。再將/dev/md0設備添加到vg00卷組上,並在vg00卷組上建立lv01和lv02兩個邏輯卷。設置lv01的大小爲1G,文件系統爲ext4,設置開機掛載到/mnt/lv01目錄;lv02大小爲2G,文件系統爲ext3,設置開機掛載到/mnt/lv02目錄上。

在vmware workstation上添加5塊硬盤,啓動linux系統。
[root@asianux4 ~]# fdisk -l 查看5塊硬盤的態況。
[root@asianux4 ~]# mdadm -Cv /dev/md0 -l5 -n3 -x1 -c128 /dev/sd{c,d,e,f}
[root@asianux4 ~]# cat /proc/mdstat
[root@asianux4 ~]# pvcreate /dev/md0
[root@asianux4 ~]# vgcreate vg00 /dev/md0
[root@asianux4 ~]# lvcreate -L 1g -n lv01 vg00
[root@asianux4 ~]# lvcreate -L 2G -n lv02 vg00
[root@asianux4 ~]# mkfs.ext4 /dev/mapper/vg00-lv01
[root@asianux4 ~]# mkfs.ext3 /dev/mapper/vg00-lv02

[root@asianux4 ~]# mkdir /mnt/lv01
[root@asianux4 ~]# mkdir /mnt/lv02
[root@asianux4 ~]# mount /dev/mapper/vg00-lv01 /mnt/lv01
[root@asianux4 ~]# mount /dev/mapper/vg00-lv02 /mnt/lv02

[root@asianux4 ~]# echo "/dev/mapper/vg00-lv01 /mnt/lv01 ext4 defaults 0 0" >> /etc/fstab
[root@asianux4 ~]# echo "/dev/mapper/vg00-lv02 /mnt/lv02 ext3 defaults 0 0" >> /etc/fstab


2、在線對lv01邏輯卷擴容,擴容到4G。同時將/dev/sdb5擴容到vg00組中。
[root@asianux4 ~]# lvextend -L 4g /dev/mapper/vg00-lv01
[root@asianux4 ~]# resize2fs /dev/mapper/vg00-lv01

[root@asianux4 ~]# pvcreate /dev/sdb5
[root@asianux4 ~]# vgextend vg00 /dev/sdb5

3、忘記root密碼和grub密碼時,須要如何破解。
進入救援模式,一路回車,chroot /mnt/sysimage-->vi /boot/grub/grub.conf將password行刪除,並保存-->passwd root更改root密碼-->exit--->reboot重啓。


4、備份/boot分區和mbr主引導記錄信息,執行rm /boot/* -rf命令後重啓,並恢復故障。
[root@asianux4 ~]# dd if=/dev/sda1 of=/backup/boot_bak.dd 
[root@asianux4 ~]# dd if=/dev/sda of=/backup/mbr_bak.dd bs=512 count=1
[root@asianux4 ~]# rm /boot/* -rf
[root@asianux4 ~]# reboot
進入救援模式,一路回車,chroot /mnt/sysimage-->umount /dev/sda1-->dd if=/backup/boot_bak.dd of=/dev/sda1-->exit-->reboot


5、將ssh服務器的debug及以上的錯誤日誌信息,寫入到/var/log/sshd.log文件。設置天天轉儲,轉儲365次,轉儲時壓縮,大小到達1M時自動轉儲。

[root@asianux4 ~]# vim /etc/rsyslog.conf 在文件末尾添加如下行
authpriv.debug        /var/log/sshd.log
[root@asianux4 ~]# service rsyslog restart

[root@asianux4 ~]# vim /etc/logrotate.d/sshd.log
/var/log/sshd.log {
    daily
    rotate 365
    compress
    minsize 1M
}



補充網卡eth0沒有顯示,真實網卡顯示成eth1的故障。
解決辦法:
[root@asianux4 ~]# mv /etc/sysconfig/network-scripts/ifcfg-eth0{,.bak}
[root@asianux4 ~]# rm /etc/udev/rules.d/70-persistent-net.rules -rf
[root@asianux4 ~]# reboot
[root@asianux4 ~]# setup    從新設置IP地址。
9.19-課後練習彙總

 

linux的命令:
語法:
shell_command options arg1 arg2 
ls -l -a /home/ /var/log
ls -la /home /var/log

shell的命
1、目錄相關的命令:
ls    顯示指定目錄的內容
dir    顯示指定目錄的內容    
cd    切換目錄
pwd    顯示絕對路徑
clear    清屏,ctrl+L(小寫)
mkdir    建立目錄
rm    刪除目錄或文件
touch    建立空文件,更改文件的時間
cp     複製
mv    移動,重命名
ln    建立符號連接,快捷方式

選項:
-l 顯示詳細列表 ls -l
-a 顯示隱藏文件 ls -al
-d 顯示目錄內容 ls -ld /root
-h 人性化顯示 ls -lh
-R 遞歸顯示。連同子目錄一併顯示。 ls -lR
-i 顯示i節點,文件的物理位置。 ls -li


[root@server4 ~]# ls -l 顯示當前目錄的詳細信息。
總用量 100
-rw-------. 1 root root  1559 9月  14 10:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 47519 9月  14 10:17 install.log
-rw-r--r--. 1 root root 10033 9月  14 10:11 install.log.syslog
drwxr-xr-x  2 root root  4096 9月  14 10:28 公共的
drwxr-xr-x  2 root root  4096 9月  14 10:28 模板
drwxr-xr-x  2 root root  4096 9月  14 10:28 視頻
drwxr-xr-x  2 root root  4096 9月  14 10:28 圖片
drwxr-xr-x  2 root root  4096 9月  14 10:28 文檔
drwxr-xr-x  2 root root  4096 9月  14 10:28 下載
drwxr-xr-x  2 root root  4096 9月  14 10:28 音樂
drwxr-xr-x  2 root root  4096 9月  14 10:28 桌面
第一列:文件類型和權限
  文件類型:
    - 表示普通文件
    d 表示目錄
    b 表示塊設備block
    c char字符設備
    s socket設備
    l link符號連接
    p pipo管道文件

   文件的權限 rwx r-x r-x     r:讀,w:寫,x:執行 s:suid,sgid, t:粘貼位
    第一列:用戶的權限rwx 
     第二列:工做組的權限r-x
    第三列:其它用戶的權限r-x

第二列:硬連接數
第三列:用戶名
第四列:工做組
第五列:文件大小
第六列:建立或修改時間
第七列:文件名

[root@server4 ~]# ls -l /tmp/ -d
drwxrwxrwt. 11 root root 4096 9月  14 10:29 /tmp/
[root@server4 ~]# ls -l /bin/ping
-rwsr-xr-x. 1 root root 40760 9月  30 2013 /bin/ping
[root@server4 ~]# ls -l /dev/sda1
brw-rw---- 1 root disk 8, 1 9月  14 10:27 /dev/sda1
[root@server4 ~]# ls -l /dev/tty0 
crw--w---- 1 root tty 4, 0 9月  14 10:27 /dev/tty0
[root@server4 ~]# ls -l /dev/cdrom 
lrwxrwxrwx 1 root root 3 9月  14 10:27 /dev/cdrom -> sr0
[root@server4 ~]# ls -l /usr/bin/ssh-agent
-rwxr-sr-x. 1 root nobody 125000 2月  26 2014 /usr/bin/ssh-agent

[root@server4 ~]# ls -a    顯示隱藏文件(以點開頭)
.                .cshrc     .gstreamer-0.10     .pulse               模板
..               .dbus      .gtk-bookmarks      .pulse-cookie        視頻
.abrt            .dmrc      .gvfs               .recently-used.xbel  圖片
anaconda-ks.cfg  .esd_auth  .ICEauthority       .ssh                 文檔
.bash_logout     .gconf     .imsettings.log     .tcshrc              下載
.bash_profile    .gconfd    install.log         .viminfo             音樂
.bashrc          .gnome2    install.log.syslog  .Xauthority          桌面
.cache           .gnote     .local              .xsession-errors
.config          .gnupg     .nautilus           公共的
[root@server4 ~]# ls
anaconda-ks.cfg  install.log.syslog  模板  圖片  下載  桌面
install.log      公共的              視頻  文檔  音樂
[root@server4 ~]# 

[root@server4 ~]# alias 
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@server4 ~]# alias canway='ls -la'
[root@server4 ~]# canway 
總用量 224

[root@server4 ~]# ls -l /root/
總用量 100
-rw-------. 1 root root  1559 9月  14 10:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 47519 9月  14 10:17 install.log
-rw-r--r--. 1 root root 10033 9月  14 10:11 install.log.syslog
drwxr-xr-x  2 root root  4096 9月  14 10:28 公共的
drwxr-xr-x  2 root root  4096 9月  14 10:28 模板
drwxr-xr-x  2 root root  4096 9月  14 10:28 視頻
drwxr-xr-x  2 root root  4096 9月  14 10:28 圖片
drwxr-xr-x  2 root root  4096 9月  14 10:28 文檔
drwxr-xr-x  2 root root  4096 9月  14 10:28 下載
drwxr-xr-x  2 root root  4096 9月  14 10:28 音樂
drwxr-xr-x  2 root root  4096 9月  14 10:28 桌面
[root@server4 ~]# ls -l /root/ -d
dr-xr-x---. 25 root root 4096 9月  14 10:33 /root/
[root@server4 ~]# pwd
/root
[root@server4 ~]# 


[root@server4 ~]# ll -il
總用量 100
794636 -rw-------. 1 root root  1559 9月  14 10:17 anaconda-ks.cfg
783363 -rw-r--r--. 1 root root 47519 9月  14 10:17 install.log
783364 -rw-r--r--. 1 root root 10033 9月  14 10:11 install.log.syslog
794658 drwxr-xr-x  2 root root  4096 9月  14 10:28 公共的
794657 drwxr-xr-x  2 root root  4096 9月  14 10:28 模板
794662 drwxr-xr-x  2 root root  4096 9月  14 10:28 視頻
794661 drwxr-xr-x  2 root root  4096 9月  14 10:28 圖片
794659 drwxr-xr-x  2 root root  4096 9月  14 10:28 文檔
794656 drwxr-xr-x  2 root root  4096 9月  14 10:28 下載
794660 drwxr-xr-x  2 root root  4096 9月  14 10:28 音樂
794655 drwxr-xr-x  2 root root  4096 9月  14 10:28 桌面
[root@server4 ~]# 

cd    切換目錄
pwd    顯示絕對路徑
clear    清屏,ctrl+L(小寫)
mkdir    建立目錄
rm    刪除目錄或文件
touch    建立空文件,更改文件的時間
cp     複製
mv    移動,重命名
ln    建立符號連接,快捷方式


[root@server4 ~]# pwd
/root
[root@server4 ~]# cd /var/  絕對路徑,linux是單根系統,/目錄。
[root@server4 var]# cd log/ 相對路徑
[root@server4 log]# cd      返回自家目錄  cd ~ 或 cd
[root@server4 ~]# pwd
/root
[root@server4 ~]# 

[root@server4 ~]# cd /usr/share/doc/
[root@server4 doc]# cd /var/log/samba/
[root@server4 samba]# cd -
/usr/share/doc
[root@server4 doc]# pwd
/usr/share/doc
[root@server4 doc]# cd -  返回上一次目錄
/var/log/samba
[root@server4 samba]# pwd
/var/log/samba
[root@server4 samba]# 


mkdir    建立目錄
rm    刪除目錄或文件
touch    建立空文件,更改文件的時間
cp     複製
mv    移動,重命名
ln    建立符號連接,快捷方式

[root@server4 ~]# ls
anaconda-ks.cfg  install.log.syslog  模板  圖片  下載  桌面
install.log      公共的              視頻  文檔  音樂
[root@server4 ~]# mkdir canway    建立canway目錄
[root@server4 ~]# ls
anaconda-ks.cfg  install.log         公共的  視頻  文檔  音樂
canway           install.log.syslog  模板    圖片  下載  桌面
[root@server4 ~]# ll canway/ -d    顯示canway目錄
drwxr-xr-x 2 root root 4096 9月  14 11:02 canway/
[root@server4 ~]# umask     查看系統的默認umask值
0022
[root@server4 ~]# 建立目錄時: 777-umask=777-022=755^C
[root@server4 ~]# touch file.txt建立file.txt文件
[root@server4 ~]# ll file.txt 
-rw-r--r-- 1 root root 0 9月  14 11:04 file.txt
[root@server4 ~]# 建立文件時: 666-umask=666-022=644^C
[root@server4 ~]# mkdir -m 777 dir1    建立權限爲777的dir1目錄
[root@server4 ~]# ll dir1/ -d
drwxrwxrwx 2 root root 4096 9月  14 11:05 dir1/
[root@server4 ~]# mkdir canway/a/b/c
mkdir: 沒法建立目錄"canway/a/b/c": 沒有那個文件或目錄
[root@server4 ~]# mkdir -p canway/a/b/c    建立多級目錄
[root@server4 ~]# ls -lR canway/
canway/:
總用量 4
drwxr-xr-x 3 root root 4096 9月  14 11:06 a

canway/a:
總用量 4
drwxr-xr-x 3 root root 4096 9月  14 11:06 b

canway/a/b:
總用量 4
drwxr-xr-x 2 root root 4096 9月  14 11:06 c

canway/a/b/c:
總用量 0
[root@server4 ~]# mkdir a{1,2,3,4,5,6,7,8,9,10}    建立多個目錄
[root@server4 ~]# ls
a1   a3  a6  a9               dir1         install.log.syslog  視頻  下載
a10  a4  a7  anaconda-ks.cfg  file.txt     公共的              圖片  音樂
a2   a5  a8  canway           install.log  模板                文檔  桌面
[root@server4 ~]# 
[root@server4 ~]# mkdir aa bb cc 
[root@server4 ~]# ll -d aa bb cc
drwxr-xr-x 2 root root 4096 9月  14 11:09 aa
drwxr-xr-x 2 root root 4096 9月  14 11:09 bb
drwxr-xr-x 2 root root 4096 9月  14 11:09 cc
[root@server4 ~]# 


rm    刪除目錄或文件
touch    建立空文件,更改文件的時間
cp     複製
mv    移動,重命名
ln    建立符號連接,快捷方式

-r 刪除目錄
-f 直接刪除,不用提示用戶。
-i 刪除前,詢問用戶。

[root@server4 ~]# rm -i file.txt 
rm:是否刪除普通空文件 "file.txt"?n
[root@server4 ~]# ll file.txt 
-rw-r--r-- 1 root root 0 9月  14 11:04 file.txt
[root@server4 ~]# rm file.txt 
rm:是否刪除普通空文件 "file.txt"?y
[root@server4 ~]# ll file.txt
ls: 沒法訪問file.txt: 沒有那個文件或目錄
[root@server4 ~]# ls
a1   a3  a6  a9               bb      dir1                公共的  圖片  音樂
a10  a4  a7  aa               canway  install.log         模板    文檔  桌面
a2   a5  a8  anaconda-ks.cfg  cc      install.log.syslog  視頻    下載

[root@server4 ~]# rm -rf a{1,2,3,4,5,6,7,8,9,10} aa bb cc canway/ 
[root@server4 ~]# ls
anaconda-ks.cfg  install.log         公共的  視頻  文檔  音樂
dir1             install.log.syslog  模板    圖片  下載  桌面
[root@server4 ~]# 

cp     複製
mv    移動,重命名
ln    建立符號連接,快捷方式

[root@server4 ~]# ls
anaconda-ks.cfg  install.log         公共的  視頻  文檔  音樂
dir1             install.log.syslog  模板    圖片  下載  桌面
[root@server4 ~]# cp /var/log/messages /root/    複製/var/log/messages文件到/root目錄
[root@server4 ~]# ls messages 
messages
[root@server4 ~]# ls
anaconda-ks.cfg  install.log         messages  模板  圖片  下載  桌面
dir1             install.log.syslog  公共的    視頻  文檔  音樂
[root@server4 ~]# cp -rpa /var/log/ . 複製/var/log目錄到當前目錄

-r 複製目錄
-p 複製時權限不發生變化
-a 複製全部,包含隱藏文件
-v 顯示覆制過程(****)

[root@server4 ~]# ls
anaconda-ks.cfg  install.log         log       公共的  視頻  文檔  音樂
dir1             install.log.syslog  messages  模板    圖片  下載  桌面
[root@server4 ~]# 
[root@server4 ~]# cp -ravp /var/log/ . 
"/var/log/" -> "./log"
"/var/log/prelink" -> "./log/prelink"
"/var/log/cron" -> "./log/cron"
"/var/log/sa" -> "./log/sa"


mv    移動,重命名
ln    建立符號連接,快捷方式

[root@server4 ~]# ls
anaconda-ks.cfg  install.log         log       公共的  視頻  文檔  音樂
dir1             install.log.syslog  messages  模板    圖片  下載  桌面
[root@server4 ~]# mv log /logic  移動並更名
[root@server4 ~]# ls /logic/ -d
/logic/
[root@server4 ~]# ls
anaconda-ks.cfg  install.log         messages  模板  圖片  下載  桌面
dir1             install.log.syslog  公共的    視頻  文檔  音樂
[root@server4 ~]# mv /logic/ .  移動到當前目錄
[root@server4 ~]# ls
anaconda-ks.cfg  install.log         logic     公共的  視頻  文檔  音樂
dir1             install.log.syslog  messages  模板    圖片  下載  桌面
[root@server4 ~]# 
[root@server4 ~]# 


ln    建立符號連接,快捷方式

-s  建立符號連接
-f  若是源目錄不存在,符號鏈接也一併建立。

[root@server4 ~]# ls
anaconda-ks.cfg  install.log         logic     公共的  視頻  文檔  音樂
dir1             install.log.syslog  messages  模板    圖片  下載  桌面
[root@server4 ~]# ln -sf /var/log/ 桌面/
[root@server4 ~]# ls 桌面/
log
[root@server4 ~]# ls 桌面/ -l
總用量 0
lrwxrwxrwx 1 root root 9 9月  14 11:20 log -> /var/log/
[root@server4 ~]# 
9.14_1
linux的命令:
語法:
shell_command options arg1 arg2 
ls -l -a /home/ /var/log
ls -la /home /var/log

shell的命令
1、目錄相關的命令:
ls    顯示指定目錄的內容
dir    顯示指定目錄的內容    
cd    切換目錄
pwd    顯示絕對路徑
clear    清屏,ctrl+L(小寫)
mkdir    建立目錄
rm    刪除目錄或文件
touch    建立空文件,更改文件的時間
cp     複製
mv    移動,重命名
ln    建立符號連接,快捷方式

選項:
-l 顯示詳細列表 ls -l
-a 顯示隱藏文件 ls -al
-d 顯示目錄內容 ls -ld /root
-h 人性化顯示 ls -lh
-R 遞歸顯示。連同子目錄一併顯示。 ls -lR
-i 顯示i節點,文件的物理位置。 ls -li


[root@server4 ~]# ls -l 顯示當前目錄的詳細信息。
總用量 100
-rw-------. 1 root root  1559 9月  14 10:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 47519 9月  14 10:17 install.log
-rw-r--r--. 1 root root 10033 9月  14 10:11 install.log.syslog
drwxr-xr-x  2 root root  4096 9月  14 10:28 公共的
drwxr-xr-x  2 root root  4096 9月  14 10:28 模板
drwxr-xr-x  2 root root  4096 9月  14 10:28 視頻
drwxr-xr-x  2 root root  4096 9月  14 10:28 圖片
drwxr-xr-x  2 root root  4096 9月  14 10:28 文檔
drwxr-xr-x  2 root root  4096 9月  14 10:28 下載
drwxr-xr-x  2 root root  4096 9月  14 10:28 音樂
drwxr-xr-x  2 root root  4096 9月  14 10:28 桌面
第一列:文件類型和權限
  文件類型:
    - 表示普通文件
    d 表示目錄
    b 表示塊設備block
    c char字符設備
    s socket設備
    l link符號連接
    p pipo管道文件

   文件的權限 rwx r-x r-x     r:讀,w:寫,x:執行 s:suid,sgid, t:粘貼位
    第一列:用戶的權限rwx 
     第二列:工做組的權限r-x
    第三列:其它用戶的權限r-x

第二列:硬連接數
第三列:用戶名
第四列:工做組
第五列:文件大小
第六列:建立或修改時間
第七列:文件名

[root@server4 ~]# ls -l /tmp/ -d
drwxrwxrwt. 11 root root 4096 9月  14 10:29 /tmp/
[root@server4 ~]# ls -l /bin/ping
-rwsr-xr-x. 1 root root 40760 9月  30 2013 /bin/ping
[root@server4 ~]# ls -l /dev/sda1
brw-rw---- 1 root disk 8, 1 9月  14 10:27 /dev/sda1
[root@server4 ~]# ls -l /dev/tty0 
crw--w---- 1 root tty 4, 0 9月  14 10:27 /dev/tty0
[root@server4 ~]# ls -l /dev/cdrom 
lrwxrwxrwx 1 root root 3 9月  14 10:27 /dev/cdrom -> sr0
[root@server4 ~]# ls -l /usr/bin/ssh-agent
-rwxr-sr-x. 1 root nobody 125000 2月  26 2014 /usr/bin/ssh-agent

[root@server4 ~]# ls -a    顯示隱藏文件(以點開頭)
.                .cshrc     .gstreamer-0.10     .pulse               模板
..               .dbus      .gtk-bookmarks      .pulse-cookie        視頻
.abrt            .dmrc      .gvfs               .recently-used.xbel  圖片
anaconda-ks.cfg  .esd_auth  .ICEauthority       .ssh                 文檔
.bash_logout     .gconf     .imsettings.log     .tcshrc              下載
.bash_profile    .gconfd    install.log         .viminfo             音樂
.bashrc          .gnome2    install.log.syslog  .Xauthority          桌面
.cache           .gnote     .local              .xsession-errors
.config          .gnupg     .nautilus           公共的
[root@server4 ~]# ls
anaconda-ks.cfg  install.log.syslog  模板  圖片  下載  桌面
install.log      公共的              視頻  文檔  音樂
[root@server4 ~]# 

[root@server4 ~]# alias 
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@server4 ~]# alias canway='ls -la'
[root@server4 ~]# canway 
總用量 224

[root@server4 ~]# ls -l /root/
總用量 100
-rw-------. 1 root root  1559 9月  14 10:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 47519 9月  14 10:17 install.log
-rw-r--r--. 1 root root 10033 9月  14 10:11 install.log.syslog
drwxr-xr-x  2 root root  4096 9月  14 10:28 公共的
drwxr-xr-x  2 root root  4096 9月  14 10:28 模板
drwxr-xr-x  2 root root  4096 9月  14 10:28 視頻
drwxr-xr-x  2 root root  4096 9月  14 10:28 圖片
drwxr-xr-x  2 root root  4096 9月  14 10:28 文檔
drwxr-xr-x  2 root root  4096 9月  14 10:28 下載
drwxr-xr-x  2 root root  4096 9月  14 10:28 音樂
drwxr-xr-x  2 root root  4096 9月  14 10:28 桌面
[root@server4 ~]# ls -l /root/ -d
dr-xr-x---. 25 root root 4096 9月  14 10:33 /root/
[root@server4 ~]# pwd
/root
[root@server4 ~]# 


[root@server4 ~]# ll -il
總用量 100
794636 -rw-------. 1 root root  1559 9月  14 10:17 anaconda-ks.cfg
783363 -rw-r--r--. 1 root root 47519 9月  14 10:17 install.log
783364 -rw-r--r--. 1 root root 10033 9月  14 10:11 install.log.syslog
794658 drwxr-xr-x  2 root root  4096 9月  14 10:28 公共的
794657 drwxr-xr-x  2 root root  4096 9月  14 10:28 模板
794662 drwxr-xr-x  2 root root  4096 9月  14 10:28 視頻
794661 drwxr-xr-x  2 root root  4096 9月  14 10:28 圖片
794659 drwxr-xr-x  2 root root  4096 9月  14 10:28 文檔
794656 drwxr-xr-x  2 root root  4096 9月  14 10:28 下載
794660 drwxr-xr-x  2 root root  4096 9月  14 10:28 音樂
794655 drwxr-xr-x  2 root root  4096 9月  14 10:28 桌面
[root@server4 ~]# 

cd    切換目錄
pwd    顯示絕對路徑
clear    清屏,ctrl+L(小寫)
mkdir    建立目錄
rm    刪除目錄或文件
touch    建立空文件,更改文件的時間
cp     複製
mv    移動,重命名
ln    建立符號連接,快捷方式


[root@server4 ~]# pwd
/root
[root@server4 ~]# cd /var/  絕對路徑,linux是單根系統,/目錄。
[root@server4 var]# cd log/ 相對路徑
[root@server4 log]# cd      返回自家目錄  cd ~ 或 cd
[root@server4 ~]# pwd
/root
[root@server4 ~]# 

[root@server4 ~]# cd /usr/share/doc/
[root@server4 doc]# cd /var/log/samba/
[root@server4 samba]# cd -
/usr/share/doc
[root@server4 doc]# pwd
/usr/share/doc
[root@server4 doc]# cd -  返回上一次目錄
/var/log/samba
[root@server4 samba]# pwd
/var/log/samba
[root@server4 samba]# 


mkdir    建立目錄
rm    刪除目錄或文件
touch    建立空文件,更改文件的時間
cp     複製
mv    移動,重命名
ln    建立符號連接,快捷方式

[root@server4 ~]# ls
anaconda-ks.cfg  install.log.syslog  模板  圖片  下載  桌面
install.log      公共的              視頻  文檔  音樂
[root@server4 ~]# mkdir canway    建立canway目錄
[root@server4 ~]# ls
anaconda-ks.cfg  install.log         公共的  視頻  文檔  音樂
canway           install.log.syslog  模板    圖片  下載  桌面
[root@server4 ~]# ll canway/ -d    顯示canway目錄
drwxr-xr-x 2 root root 4096 9月  14 11:02 canway/
[root@server4 ~]# umask     查看系統的默認umask值
0022
[root@server4 ~]# 建立目錄時: 777-umask=777-022=755^C
[root@server4 ~]# touch file.txt建立file.txt文件
[root@server4 ~]# ll file.txt 
-rw-r--r-- 1 root root 0 9月  14 11:04 file.txt
[root@server4 ~]# 建立文件時: 666-umask=666-022=644^C
[root@server4 ~]# mkdir -m 777 dir1    建立權限爲777的dir1目錄
[root@server4 ~]# ll dir1/ -d
drwxrwxrwx 2 root root 4096 9月  14 11:05 dir1/
[root@server4 ~]# mkdir canway/a/b/c
mkdir: 沒法建立目錄"canway/a/b/c": 沒有那個文件或目錄
[root@server4 ~]# mkdir -p canway/a/b/c    建立多級目錄
[root@server4 ~]# ls -lR canway/
canway/:
總用量 4
drwxr-xr-x 3 root root 4096 9月  14 11:06 a

canway/a:
總用量 4
drwxr-xr-x 3 root root 4096 9月  14 11:06 b

canway/a/b:
總用量 4
drwxr-xr-x 2 root root 4096 9月  14 11:06 c

canway/a/b/c:
總用量 0
[root@server4 ~]# mkdir a{1,2,3,4,5,6,7,8,9,10}    建立多個目錄
[root@server4 ~]# ls
a1   a3  a6  a9               dir1         install.log.syslog  視頻  下載
a10  a4  a7  anaconda-ks.cfg  file.txt     公共的              圖片  音樂
a2   a5  a8  canway           install.log  模板                文檔  桌面
[root@server4 ~]# 
[root@server4 ~]# mkdir aa bb cc 
[root@server4 ~]# ll -d aa bb cc
drwxr-xr-x 2 root root 4096 9月  14 11:09 aa
drwxr-xr-x 2 root root 4096 9月  14 11:09 bb
drwxr-xr-x 2 root root 4096 9月  14 11:09 cc
[root@server4 ~]# 


rm    刪除目錄或文件
touch    建立空文件,更改文件的時間
cp     複製
mv    移動,重命名
ln    建立符號連接,快捷方式

-r 刪除目錄
-f 直接刪除,不用提示用戶。
-i 刪除前,詢問用戶。

[root@server4 ~]# rm -i file.txt 
rm:是否刪除普通空文件 "file.txt"?n
[root@server4 ~]# ll file.txt 
-rw-r--r-- 1 root root 0 9月  14 11:04 file.txt
[root@server4 ~]# rm file.txt 
rm:是否刪除普通空文件 "file.txt"?y
[root@server4 ~]# ll file.txt
ls: 沒法訪問file.txt: 沒有那個文件或目錄
[root@server4 ~]# ls
a1   a3  a6  a9               bb      dir1                公共的  圖片  音樂
a10  a4  a7  aa               canway  install.log         模板    文檔  桌面
a2   a5  a8  anaconda-ks.cfg  cc      install.log.syslog  視頻    下載

[root@server4 ~]# rm -rf a{1,2,3,4,5,6,7,8,9,10} aa bb cc canway/ 
[root@server4 ~]# ls
anaconda-ks.cfg  install.log         公共的  視頻  文檔  音樂
dir1             install.log.syslog  模板    圖片  下載  桌面
[root@server4 ~]# 

cp     複製
mv    移動,重命名
ln    建立符號連接,快捷方式

[root@server4 ~]# ls
anaconda-ks.cfg  install.log         公共的  視頻  文檔  音樂
dir1             install.log.syslog  模板    圖片  下載  桌面
[root@server4 ~]# cp /var/log/messages /root/    複製/var/log/messages文件到/root目錄
[root@server4 ~]# ls messages 
messages
[root@server4 ~]# ls
anaconda-ks.cfg  install.log         messages  模板  圖片  下載  桌面
dir1             install.log.syslog  公共的    視頻  文檔  音樂
[root@server4 ~]# cp -rpa /var/log/ . 複製/var/log目錄到當前目錄

-r 複製目錄
-p 複製時權限不發生變化
-a 複製全部,包含隱藏文件
-v 顯示覆制過程(****)

[root@server4 ~]# ls
anaconda-ks.cfg  install.log         log       公共的  視頻  文檔  音樂
dir1             install.log.syslog  messages  模板    圖片  下載  桌面
[root@server4 ~]# 
[root@server4 ~]# cp -ravp /var/log/ . 
"/var/log/" -> "./log"
"/var/log/prelink" -> "./log/prelink"
"/var/log/cron" -> "./log/cron"
"/var/log/sa" -> "./log/sa"


mv    移動,重命名
ln    建立符號連接,快捷方式

[root@server4 ~]# ls
anaconda-ks.cfg  install.log         log       公共的  視頻  文檔  音樂
dir1             install.log.syslog  messages  模板    圖片  下載  桌面
[root@server4 ~]# mv log /logic  移動並更名
[root@server4 ~]# ls /logic/ -d
/logic/
[root@server4 ~]# ls
anaconda-ks.cfg  install.log         messages  模板  圖片  下載  桌面
dir1             install.log.syslog  公共的    視頻  文檔  音樂
[root@server4 ~]# mv /logic/ .  移動到當前目錄
[root@server4 ~]# ls
anaconda-ks.cfg  install.log         logic     公共的  視頻  文檔  音樂
dir1             install.log.syslog  messages  模板    圖片  下載  桌面
[root@server4 ~]# 
[root@server4 ~]# 


ln    建立符號連接,快捷方式

-s  建立符號連接
-f  若是源目錄不存在,符號鏈接也一併建立。

[root@server4 ~]# ls
anaconda-ks.cfg  install.log         logic     公共的  視頻  文檔  音樂
dir1             install.log.syslog  messages  模板    圖片  下載  桌面
[root@server4 ~]# ln -sf /var/log/ 桌面/
[root@server4 ~]# ls 桌面/
log
[root@server4 ~]# ls 桌面/ -l
總用量 0
lrwxrwxrwx 1 root root 9 9月  14 11:20 log -> /var/log/
[root@server4 ~]# 

使用windows的SecureCRT工具遠程鏈接linux的方法
1、在網上鄰居上查看本地鏈接2的網卡IP地址。
二、在vmware workstation上,按ctrl+d,將網卡設置成vmnet1
3、在linux系統上執行如下命令。
[root@server4 ~]# ifconfig eth0 x.x.x.10/24(x表示window上的網段)
[root@server4 ~]# service iptables stop  中止防火牆
[root@server4 ~]# service iptables off   開機不運行
[root@server4 ~]# useradd aa 建立用戶aa
[root@server4 ~]# passwd aa  密碼用戶aa的密碼。輸入密碼時沒有提示。
4、打開securecrt工具,輸入linux系統的IP地址,輸入用戶名aa,輸入密碼便可登陸。
五、登陸到linux系統後,再執行su -命令切換到root用戶。
[aa@server4 ~]$ su -
[root@server4 ~]# 


2、幫助類的命令
man  查看命令的幫助、配置文件的幫助、函數的幫助。shell_command --help
info shell_command
/usr/share/man/*
/usr/share/doc/*

[root@asianux4 Packages]# man -a open
[root@asianux4 Packages]# man ls
[root@asianux4 Packages]# man nsswitch.conf

3、權限控制命令
suid/sgid/t高級權限。
chmod    控制文件權限的命令
chown    控制文件的屬有者和工做組的命令
setfacl    設置用戶的ACL訪問控制列表
getfacl    查看用戶的ACL訪問控制列表

chmod命令的語法:
chmod -R 7777 file|dir
或
chmod -R [ugoa][=-+][rwxst] file|dir
-R 遞歸(*****)  

eg:
chmod 755 1.tar.gz
chmod u=rwx,g=rx,o=rx 1.tar.gz
chmod u=rwx,go=rx 1.tar.gz

7777
第一個7:高級權限,suid,sgid,t==>111
第二個7:用戶的權限
第三個7:工做組的權限
第四個7:其它用戶的權限

-rwsr-xr-x   100 111 101 101 ==> 4755 (s小寫,表示x執行位存在)
drwxrwxrwt   001 111 111 111 ==> 1777
-rwxrwsr-x   010 111 111 101 ==> 2775
-rwSr--r--   100 110 100 100 ==> 4644  (S大寫,表示x執行位不存在)

-rwxrwsr-x  
chmod 2775 file
chmod u=rwx,g=rws,o=rx file


suid:普通用戶在執行具備suid權限的文件時,是以屬有者的身份執行。
[root@asianux4 ~]# ll /bin/ping
-rwsr-xr-x. 1 root root 40760 9月  30 2013 /bin/ping
普通用戶在執行ping命令時,是以root身份執行。

sgid:在具備sgid權限的目錄下建立文件或目錄時,系統會自動的繼承父目錄的工做組。
[root@asianux4 ~]# mkdir dir1
[root@asianux4 ~]# ll -d dir1/
drwxr-xr-x 2 root root 4096 9月  14 14:38 dir1/
[root@asianux4 ~]# chmod g+s dir1/
[root@asianux4 ~]# ll -d dir1/
drwxr-sr-x 2 root root 4096 9月  14 14:38 dir1/
[root@asianux4 ~]# chown .bin dir1
[root@asianux4 ~]# ll dir1/ -d
drwxr-sr-x 2 root bin 4096 9月  14 14:38 dir1/
[root@asianux4 ~]# cd dir1/
[root@asianux4 dir1]# touch file1
[root@asianux4 dir1]# ll
總用量 0
-rw-r--r-- 1 root bin 0 9月  14 14:40 file1
[root@asianux4 dir1]# mkdir dir1
[root@asianux4 dir1]# ll
總用量 4
drwxr-sr-x 2 root bin 4096 9月  14 14:40 dir1
-rw-r--r-- 1 root bin    0 9月  14 14:40 file1
[root@asianux4 dir1]# cd ..
[root@asianux4 ~]# ll -d dir1/
drwxr-sr-x 3 root bin 4096 9月  14 14:40 dir1/
[root@asianux4 ~]# chmod g-s dir1/
[root@asianux4 ~]# ll dir1/ -d
drwxr-xr-x 3 root bin 4096 9月  14 14:40 dir1/
[root@asianux4 ~]# cd dir1/
[root@asianux4 dir1]# touch file2
[root@asianux4 dir1]# mkdir dir2
[root@asianux4 dir1]# ll
總用量 8
drwxr-sr-x 2 root bin  4096 9月  14 14:40 dir1
drwxr-xr-x 2 root root 4096 9月  14 14:41 dir2
-rw-r--r-- 1 root bin     0 9月  14 14:40 file1
-rw-r--r-- 1 root root    0 9月  14 14:41 file2
[root@asianux4 dir1]# 

t:粘貼位。在t位目錄下普通用戶a建立的文件,只容許本身和超級管理員root用戶刪除,其它用戶不容許刪除。
[root@asianux4 ~]# useradd user1    建立user1用戶
[root@asianux4 ~]# useradd user2
[root@asianux4 ~]# ll /tmp/ -d
drwxrwxrwt. 10 root root 4096 9月  14 14:22 /tmp/
[root@asianux4 ~]# su - user1    切換到user1用戶環境
[user1@asianux4 ~]$ whoami
user1
[user1@asianux4 ~]$ cd /tmp/
[user1@asianux4 tmp]$ touch user1
[user1@asianux4 tmp]$ chmod 777 user1
[user1@asianux4 tmp]$ ll user1 
-rwxrwxrwx 1 user1 user1 0 9月  14 14:44 user1
[user1@asianux4 tmp]$ exit    退出user1環境。
logout
[root@asianux4 ~]# su - user2
[user2@asianux4 ~]$ whoami
user2
[user2@asianux4 ~]$ cd /tmp/
[user2@asianux4 tmp]$ ll /tmp/user1 
-rwxrwxrwx 1 user1 user1 0 9月  14 14:44 /tmp/user1
[user2@asianux4 tmp]$ rm user1 -rf
rm: 沒法刪除"user1": 不容許的操做
[user2@asianux4 tmp]$ exit
logout
[root@asianux4 ~]# 



chown    控制文件的屬有者和工做組的命令
setfacl    設置用戶的ACL訪問控制列表
getfacl    查看用戶的ACL訪問控制列表

chown語法:
chown -R [username][.:][groupname] file|dir
[root@asianux4 ~]# ll 1.tar.gz 
-rwxr-xr-x 1 root root 153607 9月  10 11:38 1.tar.gz

[root@asianux4 ~]# useradd he.shixiao
[root@asianux4 ~]# chown he.shixiao:bin 1.tar.gz 設置屬有者爲he.shixiao,工做組爲bin
[root@asianux4 ~]# ll 1.tar.gz 
-rwxr-xr-x 1 he.shixiao bin 153607 9月  10 11:38 1.tar.gz

[root@asianux4 ~]# chown bin 1.tar.gz     設置屬有者爲bin
[root@asianux4 ~]# ll 1.tar.gz 
-rwxr-xr-x 1 bin bin 153607 9月  10 11:38 1.tar.gz
[root@asianux4 ~]# chown .root 1.tar.gz 設置工做組爲root
[root@asianux4 ~]# ll 1.tar.gz 
-rwxr-xr-x 1 bin root 153607 9月  10 11:38 1.tar.gz
[root@asianux4 ~]# 

setfacl    設置用戶的ACL訪問控制列表
getfacl    查看用戶的ACL訪問控制列表


[root@asianux4 ~]# ll 1.tar.gz 
-rwxr-xr-x 1 bin root 153607 9月  10 11:38 1.tar.gz
[root@asianux4 ~]# getfacl 1.tar.gz 
# file: 1.tar.gz
# owner: bin
# group: root
user::rwx
group::r-x
other::r-x

setfacl -m [ug]:[username]:[perm] file|dir

[root@asianux4 ~]# setfacl -m u:he.shixiao:rwx 1.tar.gz 
[root@asianux4 ~]# getfacl 1.tar.gz 
# file: 1.tar.gz
# owner: bin
# group: root
user::rwx
user:he.shixiao:rwx
group::r-x
mask::rwx
other::r-x

[root@asianux4 ~]# ll 1.tar.gz 
-rwxrwxr-x+ 1 bin root 153607 9月  10 11:38 1.tar.gz
9.14_2
4、顯示文件內容的命令
cat     顯示小文件的內容
less    分屏顯示
more    分屏顯示
head    顯示文件的頭10行
tail    顯示文件的後10行

[root@asianux4 ~]# cat -n /etc/hosts    (-n 顯示行號)
     1  127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
     2  ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

[root@asianux4 ~]# cat -n install.log|less 按空格或f日後顯示,b往文件頭顯示。

[root@asianux4 ~]# cat -n install.log|head
     1  °2×° libgcc-4.4.7-4.AXS4.x86_64
     2  warning: libgcc-4.4.7-4.AXS4.x86_64: Header V4 DSA/SHA1 Signature, key ID b941844d: NOKEY
     3  °2×° setup-2.8.14-20.AXS4.1.noarch
     4  °2×° tzdata-2014e-1.0.1.AXS4.noarch
     5  °2×° filesystem-2.4.30-3.AXS4.x86_64
     6  °2×° mesa-dri-filesystem-9.2-0.5.0.1.AXS4.2.x86_64
     7  °2×° foomatic-db-filesystem-4.0-7.20091126.AXS4.noarch
     8  °2×° latencytop-common-0.5-9.AXS4.x86_64
     9  °2×° tzdata-java-2014e-1.0.1.AXS4.noarch
    10  °2×° asianux-release-4.0-9.AXS4.x86_64
[root@asianux4 ~]# cat -n install.log|head -5
     1  °2×° libgcc-4.4.7-4.AXS4.x86_64
     2  warning: libgcc-4.4.7-4.AXS4.x86_64: Header V4 DSA/SHA1 Signature, key ID b941844d: NOKEY
     3  °2×° setup-2.8.14-20.AXS4.1.noarch
     4  °2×° tzdata-2014e-1.0.1.AXS4.noarch
     5  °2×° filesystem-2.4.30-3.AXS4.x86_64
[root@asianux4 ~]# cat -n install.log|tail -5
   639  °2×° rootfiles-8.1-6.1.AXS4.noarch
   640  °2×° man-pages-3.22-20.AXS4.noarch
   641  °2×° words-3.0-17.AXS4.noarch
   642  °2×° asianux-indexhtml-4.0-3.AXS4.noarch
   643  *** FINISHED INSTALLING PACKAGES ***[root@asianux4 ~]# 
[root@asianux4 ~]# 

tail -f filename  動態顯示文件的變化。
[root@asianux4 ~]# tail -f /var/log/messages  (按ctrl+c停止)

5、關機和重啓命令
shutdown 關機或重啓命令
reboot  重啓
init     切換運行級別。0表示關機,6表示重啓
halt     關機


關機命令:
shutdown -h now
halt
init 0
poweroff

重啓命令:
reboot
shutdown -r now
init 6
shutdown -r +5 "reboot"


6、壓縮和打包類的命令
gzip    壓縮命令
bzip2    壓縮命令
xz    壓縮命令

[root@asianux4 ~]# gzip  install.log
[root@asianux4 ~]# ll install.log.gz 
-rw-r--r-- 1 root root 6904 9??   9 22:58 install.log.gz
[root@asianux4 ~]# gzip -d install.log.gz 
[root@asianux4 ~]# bzip2 install.log
[root@asianux4 ~]# ll install.log.bz2 
-rw-r--r-- 1 root root 5992 9??   9 22:58 install.log.bz2
[root@asianux4 ~]# bzip2 -d install.log.bz2 
[root@asianux4 ~]# xz install.log
[root@asianux4 ~]# ll install.log.xz 
-rw-r--r-- 1 root root 6368 9??   9 22:58 install.log.xz
[root@asianux4 ~]# xz -d install.log.xz 
[root@asianux4 ~]# ll install.log
-rw-r--r-- 1 root root 26488 9??   9 22:58 install.log
[root@asianux4 ~]# 

tar    打包或解包文件,備份和恢復文件。
tar命令選項:
-c 打包 -v 顯示過程 -f 打包到文件 
-x 解包 -C 解包到指定目錄。
-j bzip2  
-z gzip
-J xz
[root@asianux4 ~]# mkdir /backup
[root@asianux4 ~]# tar -cjvf /backup/log-20150914.tar.bz2 /var/log    將/var/log目錄打包並壓縮到/backup目錄
tar: Removing leading `/' from member names
/var/log/
/var/log/anaconda.syslog
/var/log/spooler-20150914
/var/log/ntpstats/
/var/log/anaconda.ifcfg.log
/var/log/spooler
/var/log/wtmp
/var/log/anaconda.log
/var/log/dmesg.old
/var/log/ppp/
/var/log/wpa_supplicant.log
/var/log/messages
/var/log/cups/
/var/log/dmesg
/var/log/dracut.log
/var/log/lastlog
/var/log/tallylog
/var/log/anaconda.yum.log
/var/log/boot.log
/var/log/anaconda.storage.log
/var/log/maillog-20150914
/var/log/yum.log
/var/log/gdm/
/var/log/anaconda.program.log
/var/log/secure
/var/log/sa/
/var/log/sa/sa14
/var/log/sa/sar09
/var/log/sa/sa09
/var/log/sa/sa10
/var/log/audit/
/var/log/audit/audit.log
/var/log/samba/
/var/log/samba/old/
/var/log/httpd/
/var/log/sssd/
/var/log/btmp
/var/log/messages-20150914
/var/log/anaconda.xlog
/var/log/cron
/var/log/cron-20150914
/var/log/ConsoleKit/
/var/log/ConsoleKit/history
/var/log/prelink/
/var/log/prelink/prelink.log
/var/log/pm-powersave.log
/var/log/maillog
/var/log/up2date
/var/log/up2date-20150914
/var/log/mcelog
/var/log/secure-20150914
[root@asianux4 ~]# ls -l /backup/
total 164
-rw-r--r-- 1 root root 167888 Sep 14 15:39 log-20150914.tar.bz2
[root@asianux4 ~]# mkdir /test
[root@asianux4 ~]# tar -xjf /backup/log-20150914.tar.bz2 -C /test/    解包和解壓縮到/test目錄下。
[root@asianux4 ~]# ls /test/
var
[root@asianux4 ~]# ls /test/var/log/
ConsoleKit            boot.log       httpd              ppp               tallylog
anaconda.ifcfg.log    btmp           lastlog            prelink           up2date
anaconda.log          cron           maillog            sa                up2date-20150914
anaconda.program.log  cron-20150914  maillog-20150914   samba             wpa_supplicant.log
anaconda.storage.log  cups           mcelog             secure            wtmp
anaconda.syslog       dmesg          messages           secure-20150914   yum.log
anaconda.xlog         dmesg.old      messages-20150914  spooler
anaconda.yum.log      dracut.log     ntpstats           spooler-20150914
audit                 gdm            pm-powersave.log   sssd
[root@asianux4 ~]# 

7、查找的類命令。
find    在硬盤上查找
whereis    查找命令
which    查找命令
locate    快速查找,在索引文件中查找。


[root@asianux4 ~]# whereis ls    查找ls命令和幫助
ls: /bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz
[root@asianux4 ~]# whereis jake
jake:
[root@asianux4 ~]# which ls    查找ls命令和別名,只在$PATH環境變量的路徑中查找。
alias ls='ls --color=auto'
        /bin/ls
[root@asianux4 ~]# which jake
/usr/bin/which: no jake in (/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root@asianux4 ~]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@asianux4 ~]# 

[root@asianux4 ~]# ls
1.tar.gz         install.log         ??????     ??????  ??????  ??????
anaconda-ks.cfg  install.log.syslog  ?????????  ??????  ??????  ??????
[root@asianux4 ~]# locate 1.tar.gz    在索引表上查找
/root/1.tar.gz
[root@asianux4 ~]# touch 2.file
[root@asianux4 ~]# locate 2.file  
[root@asianux4 ~]# updatedb     重建索引表
[root@asianux4 ~]# locate 2.file

find語法
find <路徑> 選項
-rw-r--r--. 1 root root   7572 Sep  9 22:54 install.log.syslog
選項:
一、查找文件類型:-type f|d|b|c|l|s|p
 find . -type f

二、按權限查找:-perm 755|+2000|+4000|+6000|+1000
[root@asianux4 ~]# find /bin/ -perm +4000 -exec ls -l {} \; 查找具備suid權限的文件
-rwsr-xr-x. 1 root root 36488 Sep 30  2013 /bin/ping6
-rwsr-xr-x. 1 root root 53472 May  2  2014 /bin/umount
-rwsr-xr-x. 1 root root 34904 Jun 27  2014 /bin/su
-rwsr-x--- 1 root fuse 32336 Feb 10  2012 /bin/fusermount
-rwsr-xr-x. 1 root root 40760 Sep 30  2013 /bin/ping
-rwsr-xr-x. 1 root root 77336 May  2  2014 /bin/mount

[root@asianux4 ~]# find /usr/bin/ -perm +2000 -exec ls -l {} \; 查找具備sgid權限的文件
-rwx--s--x 1 root slocate 38464 Nov  9  2012 /usr/bin/locate
-r-xr-sr-x 1 root tty 15224 Jul 10  2013 /usr/bin/wall
-rwxr-sr-x. 1 root nobody 125000 Feb 26  2014 /usr/bin/ssh-agent
-rwxr-sr-x. 1 root tty 12016 May  2  2014 /usr/bin/write
[root@asianux4 ~]# 

3、按用戶和工做組查找
 -user <username>
 -group <groupname>
 -nouser
 -nogroup

[root@asianux4 ~]# useradd user1
[root@asianux4 ~]# useradd user2
[root@asianux4 ~]# chown user1.user2 1.tar.gz 
[root@asianux4 ~]# ll 1.tar.gz 
-rw-r--r-- 1 user1 user2 153607 Sep 10 11:38 1.tar.gz
[root@asianux4 ~]# cat /etc/passwd|grep user
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
oprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
user1:x:500:500::/home/user1:/bin/bash
user2:x:501:501::/home/user2:/bin/bash
[root@asianux4 ~]# find . -user user1 -exec ls -l {} \; 查找user1用戶的文件
-rw-r--r-- 1 user1 user2 153607 Sep 10 11:38 ./1.tar.gz
[root@asianux4 ~]# find . -group user2 -exec ls -l {} \; 查找user2工做組的文件
-rw-r--r-- 1 user1 user2 153607 Sep 10 11:38 ./1.tar.gz
[root@asianux4 ~]# userdel -r user1    刪除user1用戶
[root@asianux4 ~]# ll 1.tar.gz 
-rw-r--r-- 1 500 user2 153607 Sep 10 11:38 1.tar.gz
[root@asianux4 ~]# find . -nouser    查找無主用戶的文件
./1.tar.gz
[root@asianux4 ~]# userdel -r user2
[root@asianux4 ~]# ll 1.tar.gz 
-rw-r--r-- 1 500 501 153607 Sep 10 11:38 1.tar.gz
[root@asianux4 ~]# find . -nogroup    查找無工做組的文件
./1.tar.gz
[root@asianux4 ~]# 

4、按文件大小查找
-size [+-]n[km]   
eg:find . -size +5k 查找大於5k的文件
   find . -size 0 查找空文件

-empty 查找空文件和空目錄

5、按時間查找
-ctime [+-]n  建立時間  find . -ctime +2 查找2天前
-atime [+-]n 訪問時間 find . -ctime -2 查找2天之內的
-mtime [+-]n 修改時間

6、按文件名查找
-name "*.c"  查找.c結尾。

[root@asianux4 ~]# find . -name *.log -exec ls -l {} \;
-rw-r--r-- 1 root root 26488 Sep  9 22:58 ./install.log
[root@asianux4 ~]# 

7、綜合查找
-a 與
-o 或
! 非

8、調用shell命令
-exec <shell_command> {} \;

例:查找文件大小爲超過1K,且屬於user1的文件。
find . -size +1k -a -user user1

例:查找空文件和空目錄,並將其刪除。
find . -empty -exec rm -rf {} \;
9.14_3
課後練習:
1、安裝asianux server 4系統,具體要求以下。
一、/boot分區,大小爲100M;根分區和swap分區建立在LVM邏輯捲上,其中swap分區的名稱爲lv_swap,大小爲2048M;根分區的名稱爲lv_root,大小爲剩餘空間。
2、設置超級管理員root密碼爲123456.
3、選擇「基本服務器」套件包,並選擇安裝開發工具包,ftp服務器包,dns服務器軟件包。

2、建立/root/dir1目錄,設置dir1目錄的權限爲屬有者和工做組具備全部權限,其它用戶具備只讀權限,屬有者爲bin,工做組爲root.

3、建立/root/file.txt文件,設置權限爲全部用戶都具備讀寫權限。

4、將/etc/目錄的全部文件打包壓縮後備份到/backup目錄下,並設置文件爲etc-20150914.tar.xz

5、查找/root目錄下全部空文件和空目錄,並將其顯示結果保存到/root/empty.txt文件中

6、查找權限爲644且屬有root用戶的文件,並將其顯示結果保存到/root/root.txt文件中。

7、將/backup/etc-20150914.tar.xz文件解壓到/root/dir1目錄下。

8、複製/var/log目錄下的全部文件到/root/dir1目錄下。並改log目錄更名爲logic。

9、經過快速查找命令locate,查找file.txt文件的位置。

10、建立/root/dir2目錄,設置sgid權限,屬有者爲root,工做組爲bin。
9.14-課後練習
課後練習:
1、安裝asianux server 4系統,具體要求以下。
一、/boot分區,大小爲100M;根分區和swap分區建立在LVM邏輯捲上,其中swap分區的名稱爲lv_swap,大小爲2048M;根分區的名稱爲lv_root,大小爲剩餘空間。
2、設置超級管理員root密碼爲123456.
3、選擇「基本服務器」套件包,並選擇安裝開發工具包,ftp服務器包,bind服務器軟件包。

2、建立/root/dir1目錄,設置dir1目錄的權限爲屬有者和工做組具備全部權限,其它用戶具備只讀權限,屬有者爲bin,工做組爲root.
mkdir /root/dir1 或 mkdir -m 774 /root/dir1
chmod 774 /root/dir1 或 chmod ug=rwx,o=r /root/dir1
chown bin.root /root/dir1


3、建立/root/file.txt文件,設置權限爲全部用戶都具備讀寫權限。
touch /root/file.txt
chmod 666 /root/file.txt 或 chmod 777 /root/file.txt

4、將/etc/目錄的全部文件打包壓縮後備份到/backup目錄下,並設置文件爲etc-20150914.tar.xz
mkdir /backup
tar -cvJf /backup/etc-20150914.tar.xz /etc
ls /backup/

5、查找/root目錄下全部空文件和空目錄,並將其顯示結果保存到/root/empty.txt文件中
find /root -empty -exec ls -l {} \; > /root/empty.txt
或
find /root -empty > /root/empty.txt

6、查找權限爲644且屬有root用戶的文件,並將其顯示結果保存到/root/root.txt文件中。
find . -perm 644 -a -user root > /root/root.txt

7、將/backup/etc-20150914.tar.xz文件解壓到/root/dir1目錄下。
tar -xJf /backup/etc-20150914.tar.xz -C /root/dir1

8、複製/var/log目錄下的全部文件到/root/dir1目錄下。並改log目錄更名爲logic。
cp -rap /var/log /root/dir1
mv /root/dir1/log /root/dir1/logic

9、經過快速查找命令locate,查找file.txt文件的位置。
updatedb
locate file.txt

10、建立/root/dir2目錄,設置sgid權限,屬有者爲root,工做組爲bin。
mkdir /root/dir2
chmod g+s /root/dir2
chown root.bin /root/dir2
9.14-課後練習答案

 

vim編輯器
linux系統的編輯器:
vi/vim
emcas
gedit(圖形的記事本)
office

vim的安裝軟件包。
[root@asianux4 ~]# rpm -qa |grep vim
vim-common-7.2.411-1.8.AXS4.x86_64
vim-enhanced-7.2.411-1.8.AXS4.x86_64    vi的擴展包,能夠執行vim
vim-minimal-7.2.411-1.8.AXS4.x86_64    最小化安裝,只能執行vi
[root@asianux4 ~]# 

vim的配置文件
[root@asianux4 ~]# ll /etc/vimrc     vim命令的初始化文件,當用戶執行vim命令時,系統會自動執行/etc/vimrc配置文件。

-rw-r--r--. 1 root root 1962 4?  10 2012 /etc/vimrc

[root@asianux4 ~]# ll ~/.viminfo     vim的歷史記錄文件,用戶在vim中作了什麼事情。
-rw------- 1 root root 4622 9?  14 12:37 /root/.viminfo
[root@asianux4 ~]# 


vim的工做模式
1、命令模式:系統的默認模式,左下角無任何提示。主要用於處理文字。
2、編輯模式:在左下角有插入、替換之類的內容。主要用於錄入文字。
三、末行模式:左下角有: / ? 字符。主要用於處理文字和設置vim的選項

各模式之間的切換:
命令--編輯:i/I a/A o/O R/r
命令--末行:: or / or ?
末行|編輯--命令:按esc健。

vim的功能:
1、打開文件(編輯文件)
[root@asianux4 ~]# vim /root/install.log
在vim模式中,執行:e /root/install.log

2、讀文件 read
:r /etc/hosts
:8r !ls -l    將ls -l命令的結果,讀到第8行的後面。
:10r /etc/hosts    將/etc/hosts文件,讀到第10行的後面。

3、保存,另存爲文件 write
:w /root/install.log
:10w /root/1.txt
:10,20w /root/2.txt

4、保存退出 wirte quit
:wq
:wq!
:x
:x!

5、不保存退出。quit
:q
:q!


末行模式命令的語法:
[add1],[add2] <command> [add3]|[options]

複製 copy
:1 co 10  第1行復制到第10行
:1,10 co 20
:1,10 co $ 將第1~10行復制到最後一行。

yy    複製當前行
nyy     複製當前行及如下的n行。
y0    複製從當前光標到行首
y$    複製從光前光標到行尾

刪除 del
:1 d     刪除第1行
:1,10 d 刪除第1 ̄10行
:1,$ d    全文刪除

dd
ndd
d0
d$
dG    刪除光標行到文件末行的內容。
d1G    刪除光標行到文件頭的內容。

粘貼
p    小寫對當前光標的下一行操做。粘貼內容到當前光標的下一行
P(大)    大寫對當前光標的上一行操做。粘貼內容到當前光標的上一行


替換 s    (分隔符 / , #)
:1,10 s /old/new/g    在第1~10行之間,將全部的old字符替換成new字符。
:1,10 s /old/new/    在第1~10行之間,將全部行中第一個old字符替換成new字符。

:1,4 s /^#//g    將第1~4行中行首的#號替換成空。刪除#號。

:1,$ s /^/#/g    在全文的行首添加#號。

將全文中的/var/www/html替換成/srv/www/htdocs內容。
:1,$ s ,/var/www/html,/srv/www/htdocs,g
:1,$ s #/var/www/html#/srv/www/htdocs#g


查找 /  ?
/firmware 
?firmware

n/N 繼續查找。

vim的選項:
:set nu        顯示行號
:set nonu    去掉行號。
:set 回車    顯示全部選項
:set tab健    查找全部選項

vim的光標定位
宏觀:
:1
:100
:$
H  M  L    定位當前屏幕的上中下
ctrl+b pgup
ctrl+f pgdw

微觀
k j h l 上下左右健
0    定位到行首
$    定位到行尾

練習:
掛載光驅到/media/目錄,並將/media/Packages目錄的文件名寫入到/root/install.sh文件中。
修改/root/install.sh,將內容修改爲如下模版。
#!/bin/bash
mount /dev/cdrom /media
rpm -ivh /media/Packages/389-ds-base-1.2.11.15-33.AXS4.x86_64.rpm --force --nodeps
rpm -ivh /media/Packages/389-ds-base-libs-1.2.11.15-33.AXS4.i686.rpm --force --nodeps
rpm -ivh /media/Packages/389-ds-base-libs-1.2.11.15-33.AXS4.x86_64.rpm --force --nodeps
rpm -ivh /media/Packages/ConsoleKit-0.4.1-3.AXS4.x86_64.rpm --force --nodeps

答案:
mount /dev/cdrom /media
ls /media/Packages > /root/install.sh
vim /root/install.sh
:1,$ s #^#rpm -ivh /media/Packages/#g
:1,$ s #$# --force --nodeps#g
:1
O
#!/bin/bash
mount /dev/cdrom /media/
按esc健
:wq!
chmod +x /root/install.sh
sh /root/install.sh
ctrl+c停止執行



linux系統用戶和工做組管理:
linux的用戶的初始組是與用戶名相同的組名。
root用戶是超級管理員,其中uid=0[,gid=0]的用戶也是超級管理員。
用戶的管理:
useradd/usermod/userdel/passwd 建立、修改、刪除、修改密碼。
選項:useradd/usermod
Usage: useradd [options] LOGINAME
options:
-u <uid> 指定用戶的uid.
-o     去除用戶UID的惟一性。
-g <gid> 指定用戶的初始組。默認初始組是和用戶名相同的組名。
-G <gid,gid,gid> 將用戶添加到指定工做組中。
-c "描述信息" 指定帳號的描述和說明信息。
-d <home_dir> 指定用戶的自家目錄,home目錄。
-s <shell> 指定用戶的工做shell,默認shell爲/bin/bash.設置用戶只容許訪問受權資料,不容許遠程管理計算機時,能夠將shell設置爲/sbin/nologin;通常狀況ftp,samba,mail,http用戶都是/sbin/nologin

實例1:
設置用戶user1,要求uid爲1000,初始組爲root,添加到bin,postfix工做組中。自家目錄爲/home/user1,shell爲/sbin/nologin.
useradd -u 1000 -g root -G bin,postfix -d /home/user1 -s /sbin/nologin user1

[root@asianux4 ~]# useradd -u 1000 -g root -G bin,postfix -d /home/user1 -s /sbin/nologin user1
[root@asianux4 ~]# cat /etc/passwd|grep user1    查看user1的信息
user1:x:1000:0::/home/user1:/sbin/nologin
第一列:帳號,用戶名
第二列:密碼位。
第三列:uid號
第四列:初始組gid
第五列:帳號的描述信息。
第六列:用戶的工做目錄 
第七列:shell,平臺

[root@asianux4 ~]# cat /etc/group|grep bin|grep user1 查看user1的所在的工做組。
bin:x:1:bin,daemon,user1
第一列:組名
第二列:密碼位
第三列:工做組的gid號。
第四列:組的成員
[root@asianux4 ~]# cat /etc/group|grep postfix|grep user1
postfix:x:89:user1
[root@asianux4 ~]# cat /etc/shadow|grep user1 查看user1的密碼
user1:!!:16693:0:99999:7:3::
第一列:用戶名。
第二列:SHA512加密的密碼。!!表示冰結用戶密碼。
第三列:用戶的建立時間1970年1月1日+16693獲得的日期
第四列:用戶的激活時間,0表示當即激活。
第五列:用戶的失效時間
第六列:用戶的密碼在失效前7天提醒用戶。
第七列:最後通融時間,3天后第自動冰結。
第八列:保留位

[root@asianux4 ~]# passwd user1    設置用戶密碼
Changing password for user user1.
New password: 
BAD PASSWORD: it is too short
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@asianux4 ~]# cat /etc/shadow|grep user1
user1:$6$1nJ9Nk8r$MEwA8h7G.vO3Yx.I5NX.Uhaf82Wtof.1h4oJap9MFEBWbVYLY2JFEbpd/VPRvSDiRiOzVUZmprrSbPf.auft6.:16693:0:99999:7:::

[root@asianux4 ~]# echo "123456" |passwd --stdin user1    設置用戶密碼
Changing password for user user1.
passwd: all authentication tokens updated successfully.
[root@asianux4 ~]# cat /etc/shadow|grep user1
user1:$6$vLm6L5dC$Fg27LORfgldoHtc7oohRxtfFqtcj2.w58AH5.ShmWdZ/GMv2mt1iB7Lv9G4Ey7IO5.GcorYo1KgA737BUA5ud.:16693:0:99999:7:::
[root@asianux4 ~]# passwd -S user1    查看用戶密碼的加密算法
user1 PS 2015-09-15 0 99999 7 -1 (Password set, SHA512 crypt.)
[root@asianux4 ~]# 
[root@asianux4 ~]# passwd -l user1     冰結user1用戶
Locking password for user user1.
passwd: Success
[root@asianux4 ~]# cat /etc/shadow|grep user1    查看密碼位多出兩個!!號。
user1:!!$6$vLm6L5dC$Fg27LORfgldoHtc7oohRxtfFqtcj2.w58AH5.ShmWdZ/GMv2mt1iB7Lv9G4Ey7IO5.GcorYo1KgA737BUA5ud.:16693:0:99999:7:::
[root@asianux4 ~]# passwd -u user1    解冰用戶
Unlocking password for user user1.
passwd: Success
[root@asianux4 ~]# cat /etc/shadow|grep user1
user1:$6$vLm6L5dC$Fg27LORfgldoHtc7oohRxtfFqtcj2.w58AH5.ShmWdZ/GMv2mt1iB7Lv9G4Ey7IO5.GcorYo1KgA737BUA5ud.:16693:0:99999:7:::
[root@asianux4 ~]# su - user1    切換到user1用戶環境
This account is currently not available.
[root@asianux4 ~]# cat /etc/passwd|grep user1
user1:x:1000:0::/home/user1:/sbin/nologin
[root@asianux4 ~]# 

實例2:
建立用戶user2,要求只容許訪問受權的資源,不容許管理linux系統。
useradd -s /sbin/nologin user2

實例3:
設置user1,user2的密碼爲123456,並切換到user1用戶環境中。
echo "123456" |passwd --stdin user1
passwd user2
su - user1

實例4:
修改user1用戶的工做shell爲/bin/bash.
[root@asianux4 ~]# usermod -s /bin/bash user1
[root@asianux4 ~]# cat /etc/passwd|grep user1
user1:x:1000:0::/home/user1:/bin/bash
[root@asianux4 ~]# su - user1
[user1@asianux4 ~]$ whoami
user1
[user1@asianux4 ~]$ exit
logout
[root@asianux4 ~]# 

實例5:
刪除user1,user2,連同自家目錄和我的郵箱一併刪除。
userdel 
Usage: userdel [options] LOGIN
options:
-r 連同自家目錄和我的郵箱一併刪除
-f 強制刪除
[root@asianux4 ~]# userdel -r user1

[root@asianux4 ~]# useradd -s /sbin/nologin user2
[root@asianux4 ~]# ls /home/
user2
[root@asianux4 ~]# ls /var/spool/mail/
rpc  user2
[root@asianux4 ~]# userdel -r user2
[root@asianux4 ~]# ls /home/
[root@asianux4 ~]# ls /var/spool/mail/
rpc
[root@asianux4 ~]# 

實例6:
建立用戶admin,要求具備root同樣的權限。
[root@asianux4 ~]# cat /etc/passwd|grep ^root
root:x:0:0:root:/root:/bin/bash
[root@asianux4 ~]# useradd -u 0 -g 0 -c root -d /root -s /bin/bash admin
useradd: UID 0 is not unique
[root@asianux4 ~]# useradd -u 0 -o -g 0 -c root -d /root -s /bin/bash admin
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
[root@asianux4 ~]# pwd
/root
[root@asianux4 ~]# cat /etc/passwd|grep -E '^root|admin'
root:x:0:0:root:/root:/bin/bash
admin:x:0:0:root:/root:/bin/bash
[root@asianux4 ~]# passwd admin
Changing password for user admin.
New password: 
BAD PASSWORD: it is too short
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@asianux4 ~]# cat /etc/shadow|grep admin
admin:$6$wNlok3Ua$B5nb2egM/YAykcvUVueXJjn8f/EvnlmC4KXCrTyc3EkkpTuVdlUVV3fVAznWhOexQWis7iXpwyDZRhl1nakRb.:16693:0:99999:7:::
[root@asianux4 ~]# su - admin
[root@asianux4 ~]# whoami
root
[root@asianux4 ~]# exit
logout
[root@asianux4 ~]# userdel admin
userdel: user admin is currently logged in
[root@asianux4 ~]# userdel -f admin
userdel: user admin is currently logged in
[root@asianux4 ~]# cat /etc/passwd|grep admin
[root@asianux4 ~]# 


工做組的管理:
groupadd/groupmod/groupdel/gpasswd
groupadd/groupmod:
Usage: groupadd [options] GROUP
選項:
-g <gid> 指定工做組的gid
-o      去除gid的惟一性
-r      建立系統的工做組(gid<500的組)
-f     覆蓋建立。

實例1:
建立工做work,gid爲8000.
groupadd -g 8000 work

實例2:
將bin,postfix用戶添加以work工做組。
方法一:
usermod -G work bin
usermod -G work postfix
方法二:gpasswd
Usage: gpasswd [option] GROUP
選項:
  -a, --add USER                add USER to GROUP
  -d, --delete USER             remove USER from GROUP
  -r, --remove-password         remove the GROUP's password
  -M, --members USER,...        set the list of members of GROUP
  -A, --administrators ADMIN,...

gpasswd -a bin work
gpasswd -a postfix work
或
gpasswd -M bin,postfix work

[root@asianux4 ~]# groupadd -g 8000 work    建立work工做組
[root@asianux4 ~]# cat /etc/group|grep work
work:x:8000:
[root@asianux4 ~]# cat /etc/gshadow|grep work    查看work工做組的密碼
work:!::
[root@asianux4 ~]# gpasswd -M bin,postfix work    設置工做組的成員
[root@asianux4 ~]# cat /etc/group|grep work
work:x:8000:bin,postfix
[root@asianux4 ~]# cat /etc/gshadow|grep work
work:!::bin,postfix
[root@asianux4 ~]# gpasswd work        設置work工做組的密碼
Changing the password for group work
New Password: 
Re-enter new password: 
[root@asianux4 ~]# cat /etc/gshadow|grep work
work:$6$zXbxNhh094Pd$1nmEfnYDu45R21iIVPo8/9i7CQ.eprR2/3wfD1Ud/b2ht90BgOduN6ye84XpwJU/HKgnGwHCqC9TkLloE0Ov51::bin,postfix
[root@asianux4 ~]# gpasswd -A root work    設置work工做組的管理員爲root
[root@asianux4 ~]# cat /etc/gshadow|grep work
work:$6$zXbxNhh094Pd$1nmEfnYDu45R21iIVPo8/9i7CQ.eprR2/3wfD1Ud/b2ht90BgOduN6ye84XpwJU/HKgnGwHCqC9TkLloE0Ov51:root:bin,postfix
[root@asianux4 ~]# gpasswd -r work    清除工做組的密碼
[root@asianux4 ~]# cat /etc/gshadow|grep work
work::root:bin,postfix
[root@asianux4 ~]# 


實例3:
刪除work工做組。
[root@asianux4 ~]# groupdel
Usage: groupdel group
[root@asianux4 ~]# groupdel work 刪除工做組
[root@asianux4 ~]# cat /etc/group|grep work
[root@asianux4 ~]# useradd user1
[root@asianux4 ~]# cat /etc/group|grep user1
user1:x:500:
[root@asianux4 ~]# groupdel user1 刪做工做組時,初始組不能被刪除。
groupdel: cannot remove the primary group of user 'user1'
[root@asianux4 ~]# 


與用戶相關的配置文件
/etc/default/useradd    執行useradd的默認參數。
/etc/login.defs        用戶相關的參數設置文件
/etc/skel/*        我的配置文件目錄的母版
~/.bashrc        bash命令初始化文件    
~/.bash_profile        我的用戶的配置文件,只在登陸時執行。

[root@asianux4 ~]# cat /etc/default/useradd 
# useradd defaults file
GROUP=100    初始組100,users組。在紅帽和AX4中無效。
HOME=/home    工做目錄 
INACTIVE=-1    激活時間
EXPIRE=        失效時間
SHELL=/bin/bash    工做shell
SKEL=/etc/skel    用戶的母版,我的配置文件目錄的母版
CREATE_MAIL_SPOOL=yes    是否建立用戶郵箱

如下兩條命令結果相同。
useradd user1 
useradd -d /home/user1 -f -1 -s /bin/bash -k /etc/skel


[root@asianux4 ~]# ls -a /etc/skel/
.  ..  .bash_logout  .bash_profile  .bashrc  .gnome2  .mozilla

[root@asianux4 ~]# useradd user2
[root@asianux4 ~]# ls -a /etc/skel/ /home/user2/
/etc/skel/:
.  ..  .bash_logout  .bash_profile  .bashrc  .gnome2  .mozilla

/home/user2/:
.  ..  .bash_logout  .bash_profile  .bashrc  .gnome2  .mozilla
[root@asianux4 ~]# 


批量建立和刪除用戶
[root@asianux4 ~]# vim adduser.sh 
#!/bin/bash
#desc adduser.sh add create username and setting password 123456
#     adduser.sh del delete username

for i in $(seq 1 10)
do
if [ $1 = "add" ];then
 useradd -s /sbin/nologin aa$i
 echo 123456|passwd --stdin aa$i
fi
if [ $1 = "del" ];then
 userdel -r aa$i
 echo aa$i delete...
fi
done

[root@asianux4 ~]# chmod +x adduser.sh
[root@asianux4 ~]# sh adduser.sh add
[root@asianux4 ~]# cat /etc/passwd |grep aa
[root@asianux4 ~]# cat /etc/shadow |grep aa
[root@asianux4 ~]# sh adduser.sh del
[root@asianux4 ~]# cat /etc/passwd |grep aa
9.15_1
Linux系統引導
linux系統的啓動過程:
加電->進入bios自檢
  ->最後自檢硬盤的MBR主引導記錄
  ->系統進入grub的引導菜單,/boot/grub/grub.conf
  ->選擇操做系統啓動
  ->加載內核和內核映像文件,並執行完成
  ->系統啓動母進程init
  ->系統進入初始化腳本/etc/rc.sysinit,能夠看到"welcome to Asianux Server"
  ->掛載磁盤分區/etc/fstab,磁盤分區自動掛載
  ->選擇啓動級別(0~6)/etc/inittab,/etc/init/*.conf[默認選擇了級別3]
  ->加載/etc/rc.d/rc3.d/S*的服務
  ->啓動/etc/rc.local文件
  ->進入登陸界面,輸入用戶名和密碼登陸
  ->進入shell,等待用戶指令

/boot/grub/grub.conf    定義系統的啓動條件,和c:\boot.ini文件類似

[root@asianux4 ~]# ll /etc/grub.conf  /boot/grub/grub.conf /boot/grub/menu.lst 
-rw-------. 1 root root 828 9?   9 22:58 /boot/grub/grub.conf
lrwxrwxrwx. 1 root root  11 9?   9 22:58 /boot/grub/menu.lst -> ./grub.conf
lrwxrwxrwx. 1 root root  22 9?   9 22:58 /etc/grub.conf -> ../boot/grub/grub.conf
[root@asianux4 ~]# 


default=0    默認啓動的操做系統,0表示第一個。
timeout=5    超時時間5秒
splashimage=(hd0,0)/grub/splash.xpm.gz    定義背景圖片
    hd0表示第一塊硬盤,hd0,0表示第一塊硬盤的第一個分區
    (hd0,0)==>/dev/sda1==>/boot分區(不可替換)

hiddenmenu    隱藏菜單,按esc健才能夠看到菜單
password --md5 <密文>    設置Grub的密碼。
title Asianux Server (2.6.32-431.20.3.el6.x86_64) 菜單名稱
        root (hd0,0)    定義引導分區,在linux系統中只有/boot分區和根分區能夠成爲引導分區
        kernel /vmlinuz-2.6.32-431.20.3.el6.x86_64 ro root=/dev/mapper/vg_asianux4-lv_root rd_NO_LUKS rd_LVM_LV=vg_asianux4/lv_swap rd_LVM_LV=vg_asianux4/lv_root rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
    定義內核文件,內核選項,操做系統的根分區。

        initrd /initramfs-2.6.32-431.20.3.el6.x86_64.img
    定義內核的映像文件

Linux系統啓動的必要條件:
1、定義引導分區
2、定義內核文件、選項、操做系統的根
3、定義內核映像文件


設置grub的密碼:
[root@asianux4 /]# grub-md5-crypt 
Password: 
Retype password: 
$1$1bGbQ$JjQ.PxayyNEoPEPEWLp8l/
[root@asianux4 /]# vim /boot/grub/grub.conf
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
password --md5 $1$1bGbQ$JjQ.PxayyNEoPEPEWLp8l/ (添加grub的密碼)
title Asianux Server (2.6.32-431.20.3.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-431.20.3.el6.x86_64 ro root=/dev/mapper/vg_asianux4-lv_root rd_NO_LUKS rd_LVM_LV=vg_asianux4/lv_swap rd_LVM_LV=vg_asianux4/lv_root rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /initramfs-2.6.32-431.20.3.el6.x86_64.img


/etc/fstab    設置開機時,自動掛載磁盤分區。
格式:
[root@asianux4 ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Wed Sep  9 22:35:47 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_asianux4-lv_root /                 ext4    defaults        1 1
UUID=708b4c94-abb9-466b-adf9-7ea1b500697e /boot   ext4    defaults        1 2
/dev/mapper/vg_asianux4-lv_swap swap              swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@asianux4 ~]# 

第一列:設備名稱。/dev/sda1,UUID=XXXX,LABEL=/ (設備名稱,UUID,標籤)
第二列:掛載點,swap分區沒有掛載點
第三列:文件系統。
第四列:掛載選項。defaults(rw,suid,dev,exec,auto,nouser,async,rela-time.)
第五列:是否使用dump備份
第六列:校驗的順序,0表示不校驗,1表示從根分區校驗,2表示從boot引導分區校驗


實例:設置光驅開機時自動掛載:
[root@asianux4 ~]# ll /dev/cdr* /dev/sr0 /dev/dvd*
lrwxrwxrwx 1 root root      3 9?  15 15:00 /dev/cdrom -> sr0
lrwxrwxrwx 1 root root      3 9?  15 15:00 /dev/cdrw -> sr0
lrwxrwxrwx 1 root root      3 9?  15 15:00 /dev/dvd -> sr0
lrwxrwxrwx 1 root root      3 9?  15 15:00 /dev/dvdrw -> sr0
brw-rw---- 1 root cdrom 11, 0 9?  15 15:00 /dev/sr0

/dev/sr0    /media        iso9660        defaults    0 0


實例:開機時,自動掛載/dev/sdb1設備到/mnt/sdb1,文件系統爲fat32.
/dev/sdb1    /mnt/sdb1    vfat        defaults    0 0



[root@asianux4 ~]# vim /etc/inittab 
# Default runlevel. The runlevels used are:
#   0 - halt (Do NOT set initdefault to this) 關機
#   1 - Single user mode 單用戶模式,root不須要密碼
#   2 - Multiuser, without NFS 多用戶模式,但沒有網絡
#   3 - Full multiuser mode 多用戶模式,有網絡,文本界面
#   4 - unused        自定義模式
#   5 - X11        圖形界面,多用戶模式
#   6 - reboot         重啓
#
id:3:initdefault:    定義系統的默認運行級別。系統會自動運行/etc/rc.d/rc3.d/S*的服務。

[root@asianux4 rc3.d]# cat /etc/rc.local 設置開機自動運行的腳本。
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
ifconfig eth0 192.168.232.128/24    
[root@asianux4 rc3.d]# 

練習:
1、設置grub的加密密碼爲123456,超時時間爲10秒。
2、設置運行級別爲級別5。
3、設置網卡的IP地址,與windows的vmnet1網段相同。


Linux系統計劃任務
1、經過atd服務,實現一次性計劃任務
2、經過crond服務,實現重複性計劃任務
3、經過開機自動運行的腳本,實現計劃任務。

經過atd服務,實現一次性計劃任務
1、啓動atd服務。
2、建立at任務
3、查看管理at任務。
[root@asianux4 ~]# service atd start
[root@asianux4 ~]# service atd status
atd (pid  1487) ????...
[root@asianux4 ~]# ps -ef |grep atd
rpcuser   1199     1  0 15:17 ?        00:00:00 rpc.statd
root      1487     1  0 15:17 ?        00:00:00 /usr/sbin/atd
root      2250  1541  0 15:51 pts/0    00:00:00 grep atd

[root@asianux4 ~]# at now +5 min|hour|day|week|mon
[root@asianux4 ~]# date
Tue Sep 15 15:52:14 CST 2015
[root@asianux4 ~]# at 15:55    建立計劃任務
at> ls -l > /root/date.txt
at> date >> /root/date.txt
at> <EOT>        按ctrl+d保存退出,按ctrl+c停止。
job 1 at 2015-09-15 15:55
[root@asianux4 ~]# atq        查看計劃任務
1       2015-09-15 15:55 a root

[root@asianux4 ~]# date
Tue Sep 15 15:55:09 CST 2015
[root@asianux4 ~]# ls
1.tar.gz  adduser.sh  anaconda-ks.cfg  date.txt  install.log  install.log.syslog  install.sh
[root@asianux4 ~]# cat date.txt 
total 496
-rw-r--r--  1 user1 user2 153607 Sep 10 11:38 1.tar.gz
-rwxr-xr-x  1 root  root     298 Sep 15 11:18 adduser.sh
-rw-------. 1 root  root    1454 Sep  9 22:58 anaconda-ks.cfg
-rw-r--r--  1 root  root       0 Sep 15 15:55 date.txt
-rw-r--r--  1 root  root   26488 Sep  9 22:58 install.log
-rw-r--r--. 1 root  root    7572 Sep  9 22:54 install.log.syslog
-rwxr-xr-x  1 root  root  303408 Sep 15 09:32 install.sh
Tue Sep 15 15:55:00 CST 2015
[root@asianux4 ~]# 

[root@asianux4 ~]# at now +2 min
at> date > date.txt
at> <EOT>
job 2 at 2015-09-15 15:57
[root@asianux4 ~]# atq
2       2015-09-15 15:57 a root
[root@asianux4 ~]# atrm 2    刪除計劃任務
[root@asianux4 ~]# atq
[root@asianux4 ~]# 

經過crond服務,實現一次性和重複性計劃任務
1、啓動crond服務
二、建立crond計劃任務(方法1 ̄33、查看計劃任務。

[root@asianux4 ~]# service crond status    查看crond服務的狀態
crond (pid  1476) ????...
[root@asianux4 ~]# service crond start    啓動crond服務
[root@asianux4 ~]# service crond restart重啓crond服務
?? crond:[??]
???? crond:[??]
[root@asianux4 ~]# ps -ef |grep crond    查看crond服務的進程
root      2328     1  0 15:58 ?        00:00:00 crond
root      2331  1541  0 15:58 pts/0    00:00:00 grep crond
[root@asianux4 ~]# 

方法一:將須要執行的腳本(純綷的shell腳本),放到指定的目錄(小時,天,月,周)下便可
[root@asianux4 cron.d]# ll /etc/cron* -d  
drwxr-xr-x. 2 root root 4096 Sep  9 22:54 /etc/cron.daily
drwxr-xr-x. 2 root root 4096 Sep  9 22:54 /etc/cron.hourly
drwxr-xr-x. 2 root root 4096 Sep  9 22:52 /etc/cron.monthly
drwxr-xr-x. 2 root root 4096 Jan 31  2012 /etc/cron.weekly

[root@asianux4 cron.hourly]# pwd
/etc/cron.hourly
[root@asianux4 cron.hourly]# cat 0anacron 
#!/bin/bash
# Skip excecution unless the date has changed from the previous run 
if test -r /var/spool/anacron/cron.daily; then
    day=`cat /var/spool/anacron/cron.daily`
fi
if [ `date +%Y%m%d` = "$day" ]; then
    exit 0;
fi

# Skip excecution unless AC powered
if test -x /usr/bin/on_ac_power; then
    /usr/bin/on_ac_power &> /dev/null
    if test $? -eq 1; then
    exit 0
    fi
fi
/usr/sbin/anacron -s
[root@asianux4 cron.hourly]# 

方法二:將須要執行的腳本(包含有時間和用戶名的shell腳本),放到/etc/cron.d/目錄下。
[root@asianux4 cron.hourly]# cd /etc/cron.d
[root@asianux4 cron.d]# ls
0hourly  raid-check  sysstat
[root@asianux4 cron.d]# cat 0hourly 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
01 * * * * root run-parts /etc/cron.hourly
分鐘 小時 天 月 周 用戶名 腳本(若是是目錄,須要添加run-parts關健字)
分鐘:0-59
小時:0-23
天:1-31
月:1-12
周:0-7, 0和7表示星期天。也能夠採用英文表示。

[root@asianux4 cron.d]# cat raid-check 
# Run system wide raid-check once a week on Sunday at 1am by default
0 1 * * Sun root /usr/sbin/raid-check

實例1:
每週六23:55以root身份執行備份腳本/backup/backup_etc.sh
55 23 * * 6 root /backup/backup_etc.sh

每隔5分鐘,採樣一次IO的吞吐狀況。
*/5 * * * * root /usr/bin/iostat 1 10 >> /root/iostat.txt  每秒採樣一次,共採樣10次

天天的1,4,8,13,20,23小時和1,8,13,14,23,50,44採樣一次。
1,8,13,14,23,50,44 1,4,8,13,20,23 * * * root /usr/bin/iostat 1 10 >> /root/iostat.txt

方法三:採用crontab -e命令自定義腳本(包含時間和shell腳本)
每隔一分鐘執行一次date命令,並將結果寫入到/root/date.txt文件中。
[root@asianux4 cron.d]# crontab -e [-u root] 建立計劃任務
*/1 * * * * /bin/date >> /root/date.txt
保存退出

[root@asianux4 cron.d]# crontab -l    查看計劃任務
*/1 * * * * /bin/date >> /root/date.txt
[root@asianux4 cron.d]# service crond restart
?? crond:[??]
???? crond:[??]
[root@asianux4 cron.d]# tailf /var/log/cron  查看計劃任務的日誌

Sep 15 16:01:01 asianux4 run-parts(/etc/cron.hourly)[2366]: finished mcelog.cron
Sep 15 16:10:01 asianux4 CROND[2404]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Sep 15 16:13:52 asianux4 crontab[2412]: (root) BEGIN EDIT (root)
Sep 15 16:15:27 asianux4 crontab[2412]: (root) REPLACE (root)
Sep 15 16:15:27 asianux4 crontab[2412]: (root) END EDIT (root)
Sep 15 16:15:29 asianux4 crontab[2417]: (root) LIST (root)
Sep 15 16:15:33 asianux4 crond[2433]: (CRON) STARTUP (1.4.4)
Sep 15 16:15:33 asianux4 crond[2433]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 59% if used.)
Sep 15 16:15:33 asianux4 crond[2433]: (CRON) INFO (running with inotify support)
Sep 15 16:15:33 asianux4 crond[2433]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
Sep 15 16:16:01 asianux4 CROND[2437]: (root) CMD (/bin/date >> /root/date.txt)


[root@asianux4 cron.d]# crontab -l
*/1 * * * * /bin/date >> /root/date.txt
[root@asianux4 cron.d]# crontab -r    刪除計劃任務
[root@asianux4 cron.d]# crontab -l
no crontab for root
[root@asianux4 cron.d]# 


3、經過開機自動運行的腳本,實現計劃任務。
/etc/rc.local    開機自動運行的腳本。
~/.bash_profile    用戶登陸時執行
~/.bashrc    用戶執行bash命令時執行。


Linux軟件包的安裝
1、rpm:紅帽、紅旗、suse、centos系統。具備特定的操做系統和硬件[CPU位數,x86,ppc]要求。
ypbind-1.20.4-30.AXS4.x86_64.rpm
yum-utils-1.1.30-17.AXS4.noarch.rpm

二、bin:添加了許可的rpm包。 ypbind-1.20.4-30.AXS4.x86_64.bin (許可+rpm)

3、deb:debain,ubuntu系統,具備特定的操做系統和硬件[CPU位數,x86,ppc]要求。
ypbind-1.20.4-30.AXS4.x86_64.deb

四、源碼:webmin-1.680.tar.gz,沒有操做系統和硬件環境的要求。


rpm軟件包的管理:
方法一:rpm命令
方法二:yum命令(推薦)

經過yum命令管理軟件包。
一、建立yum源 /etc/yum.repos.d/*.repo
2、清除本地yum源的信息。 yum clean all
3、下載遠程yum源的信息到本地 yum repolist;或yum list 
4、搜索軟件包關健字 yum search gcc; yum search tomcat
五、安裝軟件包,卸載軟件,更新軟件包。 yum install gcc -y


[root@asianux4 Packages]# cd /etc/yum.repos.d/
[root@asianux4 yum.repos.d]# ls
[root@asianux4 yum.repos.d]# vim mycdrom.repo 編輯光盤安裝源文件
[mycdrom]        安裝源的標籤
name=mycdrom        安裝源的名稱
baseurl=file:///media    指定安裝源,本地光盤。
enabled=1        打開安裝源,0表示關閉安裝源
gpgcheck=0        關閉gpg校驗

安裝源:
baseurl=http://xx.x.x.x/server6/Server
baseurl=ftp://xx.x.x.x/server5/Server
baseurl=file:///media

[root@asianux4 yum.repos.d]# mount /dev/cdrom  /media/
mount: block device /dev/sr0 is write-protected, mounting read-only

[root@asianux4 yum.repos.d]# yum clean all    清除本地安裝源信息
Loaded plugins: axtu-plugin, refresh-packagekit, security
Please register, or you can not connect to Asianux Update Server!
Cleaning repos: mycdrom
Cleaning up Everything
[root@asianux4 yum.repos.d]# yum repolist    下載yum安裝源的信息。
Loaded plugins: axtu-plugin, refresh-packagekit, security
Please register, or you can not connect to Asianux Update Server!
mycdrom                                                                   | 3.8 kB     00:00 ... 
mycdrom/primary_db                                                        | 3.2 MB     00:00 ... 
repo id                                      repo name                                     status
mycdrom                                      mycdrom                                       3749
repolist: 3749
[root@asianux4 yum.repos.d]# yum search tomcat    搜索須要安裝的軟件包。
[root@asianux4 yum.repos.d]# yum install tomcat6.noarch -y 安裝tomcat6
[root@asianux4 yum.repos.d]# yum update tomcat6.noarch -y 更新,升級tomcat6
[root@asianux4 yum.repos.d]# yum remove tomcat6.noarch -y 卸載tomcat6

查詢軟件包是否安裝:
[root@asianux4 yum.repos.d]# 
[root@asianux4 yum.repos.d]# rpm -qa |grep vsftpd
[root@asianux4 yum.repos.d]# yum install vsftpd -y
[root@asianux4 yum.repos.d]# rpm -qa |grep vsftpd
vsftpd-2.2.2-11.AXS4.1.x86_64
[root@asianux4 yum.repos.d]# 
9.15-2
課後練習:
1、vim編輯器練習。
掛載光驅到/media/目錄,並將/media/Packages目錄的文件名寫入到/root/install.sh文件中。
修改/root/install.sh,將內容修改爲如下模版。
#!/bin/bash
mount /dev/cdrom /media
rpm -ivh /media/Packages/389-ds-base-1.2.11.15-33.AXS4.x86_64.rpm --force --nodeps
rpm -ivh /media/Packages/389-ds-base-libs-1.2.11.15-33.AXS4.i686.rpm --force --nodeps
rpm -ivh /media/Packages/389-ds-base-libs-1.2.11.15-33.AXS4.x86_64.rpm --force --nodeps
rpm -ivh /media/Packages/ConsoleKit-0.4.1-3.AXS4.x86_64.rpm --force --nodeps


2、建立用戶alex,用戶ID號爲2345,密碼爲123321

3、建立用戶user1,用戶ID號1000,初始組爲bin,添加到root,postfix工做組中。自家目錄爲/tmp/user1,shell爲/bin/bash.

4、建立用戶user2,要求只容許訪問受權的資源,不容許管理linux系統。

5、建立用戶admin,要求具備root同樣的權限。

6、建立work1工做組,並將alex,user1,user2,admin用戶添加以work1工做組。其中admin爲work1工做組的管理員。

7、設置grub的加密密碼爲123456,超時時間爲5秒。設置運行級別爲級別3。

8、每週六02:01以root身份執行備份/var/log日誌到/backup目錄下,備份文件名的格式爲log-20150915.tar.bz2.

9、經過yum安裝gcc,tomcat6軟件包。並將gcc,tomcat軟件包的版本信息寫入到/root/rpm.txt文件中。

10、設置光盤開機時自動掛載。
9.15-課後練習
課後練習:
1、vim編輯器練習。
掛載光驅到/media/目錄,並將/media/Packages目錄的文件名寫入到/root/install.sh文件中。
修改/root/install.sh,將內容修改爲如下模版。
#!/bin/bash
mount /dev/cdrom /media
rpm -ivh /media/Packages/389-ds-base-1.2.11.15-33.AXS4.x86_64.rpm --force --nodeps
rpm -ivh /media/Packages/389-ds-base-libs-1.2.11.15-33.AXS4.i686.rpm --force --nodeps
rpm -ivh /media/Packages/389-ds-base-libs-1.2.11.15-33.AXS4.x86_64.rpm --force --nodeps
rpm -ivh /media/Packages/ConsoleKit-0.4.1-3.AXS4.x86_64.rpm --force --nodeps

答案:
mount /dev/cdrom /media
ls /media/Packages > /root/install.sh
vim /root/install.sh
:1,$ s #^#rpm -ivh /media/Packages/#g
:1,$ s #$# --force --nodeps#g
:1
O (大歐)
#!/bin/bash
mount /dev/cdrom /media/
按esc健
:wq!
chmod +x /root/install.sh
sh /root/install.sh
ctrl+c停止執行


2、建立用戶alex,用戶ID號爲2345,密碼爲123321
[root@asianux4 ~]# useradd -u 2345 alex
[root@asianux4 ~]# passwd alex
或
[root@asianux4 ~]# echo "123321" |passwd --stdin alex

3、建立用戶user1,用戶ID號1000,初始組爲bin,添加到root,postfix工做組中。自家目錄爲/tmp/user1,shell爲/bin/bash.
[root@asianux4 ~]# useradd -u 1000 -g bin -G root,postfix -d /tmp/user1 -s /bin/bash user1
[root@asianux4 ~]# cat /etc/passwd |grep user1


4、建立用戶user2,要求只容許訪問受權的資源,不容許管理linux系統。
[root@asianux4 ~]# useradd -s /sbin/nologin user2
[root@asianux4 ~]# cat /etc/passwd |grep user2

5、建立用戶admin,要求具備root同樣的權限。
[root@asianux4 ~]# useradd -u 0 -o -g 0 -c "root" -d /root -s /bin/bash admin
或
[root@asianux4 ~]# useradd -u 0 -o -g 0 -d /root admin


6、建立work1工做組,並將alex,user1,user2,admin用戶添加以work1工做組。其中admin爲work1工做組的管理員。
[root@asianux4 ~]# groupadd work1
[root@asianux4 ~]# gpasswd -M alex,user1,user2,admin work1
[root@asianux4 ~]# gpasswd -A admin work1
[root@asianux4 ~]# cat /etc/group|grep work1

7、設置grub的加密密碼爲123456,超時時間爲5秒。設置運行級別爲級別3。
[root@asianux4 ~]# grub-md5-crypt (輸入一個密碼的密文)
[root@asianux4 ~]# vim /boot/grub/grub.conf 在title標題前一行添加如下內容。
password --md5 <密文>
timeout=5 (修改timeout=5)
[root@asianux4 ~]# vim /etc/inittab
id:3:initdefault:    (將5更改成3)


8、每週六02:01以root身份執行備份/var/log日誌到/backup目錄下,備份文件名的格式爲log-20150915.tar.bz2.
[root@asianux4 ~]# mkdir /backup
[root@asianux4 ~]# vim /etc/backup_log.sh
#!/bin/bash
tar -cjf /backup/log-20150915.tar.bz2 /var/log
保存退出。
[root@asianux4 ~]# chmod +x /etc/backup_log.sh

[root@asianux4 ~]# crontab -e -u root
01 02 * * 6 /etc/backup_log.sh
保存退出。
[root@asianux4 ~]# service crond restart

9、經過yum安裝gcc,tomcat6軟件包。並將gcc,tomcat軟件包的版本信息寫入到/root/rpm.txt文件中。
[root@asianux4 /]# vim /etc/yum.repos.d/mycdrom.repo 
[mycdrom]
name=mycdrom
baseurl=file:///media
enabled=1
gpgcheck=0
[root@asianux4 /]# yum clean all
[root@asianux4 /]# yum repolist
[root@asianux4 /]# yum install gcc -y
[root@asianux4 /]# yum install tomcat6 -y
[root@asianux4 /]# rpm -qa |grep gcc > /root/rpm.txt
[root@asianux4 /]# rpm -qa |grep tomcat >> /root/rpm.txt 追加

10、設置光盤開機時自動掛載。
[root@asianux4 /]# vim /etc/fstab  在文件末尾添加如下內容。
/dev/sr0    /media        iso9660        defaults    0 0
保存退出
[root@asianux4 /]# mount -a
[root@asianux4 /]# df -h 查看是否掛載成功。
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_asianux4-lv_root
                      6.7G  3.0G  3.4G  47% /
tmpfs                 935M     0  935M   0% /dev/shm
/dev/sda1             485M   39M  421M   9% /boot
/dev/sr0              3.8G  3.8G     0 100% /media   光驅掛載成功
[root@asianux4 /]# 
9.15-課後練習答案

 

linux系統軟件包的管理
方法一:yum命令管理
 一、配置YUM的安裝源 /etc/yum.repes.d/*.repo
 2、清除本地的安裝源。yum clean all
 3、下載最新的安裝源。yum repolist或yum list
 4、安裝軟件包。

方法二:rpm命令管理

rpm軟件包的安裝
yum:
[root@asianux4 ~]# yum install xxx -y
[root@asianux4 ~]# yum groupinstall mysql -y  安裝mysql軟件包組的全部軟件包。
[root@asianux4 ~]# yum groupinfo mysql          查看mysql軟件包組的信息
[root@asianux4 ~]# yum grouplist              查看系統存在的軟件包組    
[root@asianux4 ~]# yum groupremove mysql -y   卸載mysql軟件包組。

rpm: -i 安裝  -v 顯示操做的過程 -h # --force覆蓋安裝 --nodeps 強制安裝
[root@asianux4 ~]# rpm -ivh xxx.rpm --force --nodeps  

[root@asianux4 ~]# mount /dev/cdrom /media
[root@asianux4 ~]# cd /media/Packages/
[root@asianux4 Packages]# ls gcc-4.4.7-4.AXS4.x86_64.rpm 
gcc-4.4.7-4.AXS4.x86_64.rpm
[root@asianux4 Packages]# rpm -ivh gcc-4.4.7-4.AXS4.x86_64.rpm 安裝gcc軟件包
warning: gcc-4.4.7-4.AXS4.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID b941844d: NOKEY
Preparing...                ########################################### [100%]
        package gcc-4.4.7-4.AXS4.x86_64 is already installed
[root@asianux4 Packages]# rpm -e gcc      卸載gcc軟件包。
[root@asianux4 Packages]# rpm -ivh gcc-4.4.7-4.AXS4.x86_64.rpm 
warning: gcc-4.4.7-4.AXS4.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID b941844d: NOKEY
Preparing...                ########################################### [100%]
   1:gcc                    ########################################### [100%]
[root@asianux4 Packages]# rpm -e gcc 
[root@asianux4 Packages]# rpm -i gcc-4.4.7-4.AXS4.x86_64.rpm 安裝gcc軟件包。
warning: gcc-4.4.7-4.AXS4.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID b941844d: NOKEY
[root@asianux4 Packages]# 
[root@asianux4 Packages]# rpm -ivh gcc-c++-4.4.7-4.AXS4.x86_64.rpm 
warning: gcc-c++-4.4.7-4.AXS4.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID b941844d: NOKEY
error: Failed dependencies:
        libstdc++-devel = 4.4.7-4.AXS4 is needed by gcc-c++-4.4.7-4.AXS4.x86_64
[root@asianux4 Packages]# rpm -ivh gcc-c++-4.4.7-4.AXS4.x86_64.rpm libstdc++-devel-4.4.7-4.AXS4.x86_64.rpm 安裝軟件包時,將依賴一併安裝。
warning: gcc-c++-4.4.7-4.AXS4.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID b941844d: NOKEY
Preparing...                ########################################### [100%]
   1:libstdc++-devel        ########################################### [ 50%]
   2:gcc-c++                ########################################### [100%]
[root@asianux4 Packages]# 

rpm軟件包的更新,升級
yum:
[root@asianux4 ~]# yum update xxx -y 升級軟件包。
[root@asianux4 ~]# yum upgrade 系統更新,升級。

rpm: -U 升級 -v  -h  --force --nodeps
[root@asianux4 Packages]# rpm -Uvh zip-3.0-1.AXS4.x86_64.rpm 
warning: zip-3.0-1.AXS4.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID b941844d: NOKEY
Preparing...                ########################################### [100%]
        package zip-3.0-1.AXS4.x86_64 is already installed
[root@asianux4 Packages]# rpm -Uvh zip-3.0-1.AXS4.x86_64.rpm --force
warning: zip-3.0-1.AXS4.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID b941844d: NOKEY
Preparing...                ########################################### [100%]
   1:zip                    ########################################### [100%]
[root@asianux4 Packages]# 

rpm軟件包的卸載 -e
yum:
[root@asianux4 ~]# yum remove xxx    卸載軟件包
[root@asianux4 ~]# yum erase xxx    卸載軟件包

rpm: -e 卸載
[root@asianux4 Packages]# rpm -e libstdc++-devel    卸載軟件包時,提示有依賴。
error: Failed dependencies:
        libstdc++-devel = 4.4.7-4.AXS4 is needed by (installed) gcc-c++-4.4.7-4.AXS4.x86_64
[root@asianux4 Packages]# rpm -e libstdc++-devel --nodeps    強制卸載。 
或
[root@asianux4 Packages]# rpm -e libstdc++-devel gcc-c++     將依賴一併卸載。
 
[root@asianux4 Packages]# rpm -qa |grep libstdc++-devel    查詢libstdc++-devel軟件是否安裝。

[root@asianux4 Packages]# rpm -ivh libstdc++-devel-4.4.7-4.AXS4.x86_64.rpm 
warning: libstdc++-devel-4.4.7-4.AXS4.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID b941844d: NOKEY
Preparing...                ########################################### [100%]
   1:libstdc++-devel        ########################################### [100%]
[root@asianux4 Packages]# rpm -qa |grep libstdc++-devel
libstdc++-devel-4.4.7-4.AXS4.x86_64


rpm軟件包的查詢 -q
rpm -qa         查詢系統中已安裝的全部軟件包。
rpm -qa|grep php 查詢php軟件包是否安裝
rpm -ql zip     查詢zip軟件包安裝到哪些目錄,裏面有哪些文件 
rpm -qf /bin/ls   查詢ls命令(文件)屬於哪一個軟件包。
[root@asianux4 Packages]# rpm -qf /bin/ls
coreutils-8.4-31.2.0.3.AXS4.x86_64

rpm -qi zip      查詢軟件包的詳細信息,如安裝信息,協議等


[root@asianux4 Packages]# rpm -ql zip
/usr/bin/zip
/usr/bin/zipcloak
/usr/bin/zipnote
/usr/bin/zipsplit
/usr/share/doc/zip-3.0
/usr/share/doc/zip-3.0/CHANGES
/usr/share/doc/zip-3.0/LICENSE
/usr/share/doc/zip-3.0/README
/usr/share/doc/zip-3.0/README.CR
/usr/share/doc/zip-3.0/TODO
/usr/share/doc/zip-3.0/WHATSNEW
/usr/share/doc/zip-3.0/WHERE
/usr/share/doc/zip-3.0/algorith.txt
/usr/share/man/man1/zip.1.gz
/usr/share/man/man1/zipcloak.1.gz
/usr/share/man/man1/zipnote.1.gz
/usr/share/man/man1/zipsplit.1.gz
[root@asianux4 Packages]# 

[root@asianux4 Packages]# rpm -qi zip    查詢軟件包的詳細信息。
Name        : zip                          Relocations: (not relocatable)
Version     : 3.0                               Vendor: Asianux
Release     : 1.AXS4                        Build Date: Mon Jan 10 23:32:52 2011
Install Date: Wed Sep 16 08:50:23 2015         Build Host: Hiranya-mock-x86_64.asianux.com
Group       : Applications/Archiving        Source RPM: zip-3.0-1.AXS4.src.rpm
Size        : 823612                           License: BSD
Signature   : DSA/SHA1, Mon Jan 10 23:32:52 2011, Key ID d4ab6978b941844d
Packager    : packager@asianux.com
URL         : http://www.info-zip.org/Zip.html
Summary     : A file compression and packaging utility compatible with PKZIP
Description :
The zip program is a compression and file packaging utility.  Zip is
analogous to a combination of the UNIX tar and compress commands and
is compatible with PKZIP (a compression and file packaging utility for
MS-DOS systems).

Install the zip package if you need to compress files using the zip
program.

rpm軟件包的校驗 -V
rpm -Va        對系統全部的文件和軟件包作校驗。
rpm -Vf /bin/ls    對/bin/ls命令作校驗
rpm -Vl zip    對zip軟件包的全部文件作校驗。

[root@asianux4 ~]# rpm -Vf /bin/ls
prelink: /usr/bin/expr: at least one of file's dependencies has changed since prelinking
S.?......    /usr/bin/expr
prelink: /usr/bin/factor: at least one of file's dependencies has changed since prelinking
S.?......    /usr/bin/factor

S:md5校驗發生了變化
?:長度發生了變化
U:屬有者發生了變化
G:工做組發生了變化
T:時間發生了變化

[root@asianux4 ~]# ll /bin/pwd
-rwxr-xr-x 1 root root 31656 Jun 27  2014 /bin/pwd
[root@asianux4 ~]# chown bin.bin /bin/pwd
[root@asianux4 ~]# rpm -Vf /bin/pwd 
.....UG..    /bin/pwd
prelink: /usr/bin/expr: at least one of file's dependencies has changed since prelinking
S.?......    /usr/bin/expr
prelink: /usr/bin/factor: at least one of file's dependencies has changed since prelinking
S.?......    /usr/bin/factor
[root@asianux4 ~]# touch /bin/pwd
[root@asianux4 ~]# 
[root@asianux4 ~]# chown bin.bin /bin/pwd
[root@asianux4 ~]# ll /bin/pwd
-rwxr-xr-x 1 bin bin 31656 Sep 16 09:13 /bin/pwd
[root@asianux4 ~]# rpm -Vf /bin/pwd
.....UGT.    /bin/pwd
prelink: /usr/bin/expr: at least one of file's dependencies has changed since prelinking
S.?......    /usr/bin/expr
prelink: /usr/bin/factor: at least one of file's dependencies has changed since prelinking
S.?......    /usr/bin/factor
[root@asianux4 ~]# chown root.root /bin/pwd
[root@asianux4 ~]# rpm -Vf /bin/pwd
.......T.    /bin/pwd
prelink: /usr/bin/expr: at least one of file's dependencies has changed since prelinking
S.?......    /usr/bin/expr
prelink: /usr/bin/factor: at least one of file's dependencies has changed since prelinking
S.?......    /usr/bin/factor
[root@asianux4 ~]# 


在linux系統安裝源碼包。
xx.tar.gz
xx.tar.bz2
xx.tar.xz

安裝軟件包的步驟:
一、下載源碼包。http://sourceforge.net
二、解壓和解包源碼到指定的目錄上。tar -zxf xxx.tar.gz -C /opt
3、進入解包目錄,並查看README、INSTALL文件,並按照提示安裝。
 通常源碼軟件包的安裝步驟以下。
 配置軟件包:./configure --prefix=/usr/local/xxx --enable-xx --disable-xx --with-xx=xxx  (最終的結果是產生一個MakeFile文件)
 編譯軟件包:make (執行makefile文件)
 編譯安裝:  make install (將編譯的文件複製到指定目錄下)

eg:安裝proftpd軟件包。
[root@asianux4 ~]# tar -zxf proftpd-1.3.5.tar.gz -C /opt/
[root@asianux4 ~]# cd /opt/proftpd-1.3.5/
[root@asianux4 proftpd-1.3.5]# vim README
[root@asianux4 proftpd-1.3.5]# vim INSTALL 
[root@asianux4 proftpd-1.3.5]# ./configure --prefix=/usr/local/proftpd    配置軟件包。
[root@asianux4 proftpd-1.3.5]# vim Makefile
[root@asianux4 proftpd-1.3.5]# make;make install    編譯和編譯安裝
[root@asianux4 proftpd-1.3.5]# 

[root@asianux4 proftpd-1.3.5]# cd /usr/local/proftpd/    進入安裝目錄
[root@asianux4 proftpd]# ls
bin  etc  include  lib  libexec  sbin  share  var
[root@asianux4 proftpd]# cd sbin/    進入超級管理員能夠執行命令目錄下
[root@asianux4 sbin]# ls
ftpscrub  ftpshut  in.proftpd  proftpd
[root@asianux4 sbin]# ./proftpd     啓動proftpd服務器
2015-09-16 09:45:49,995 asianux4 proftpd[21944]: fatal: Group: Unknown group 'nogroup' on line 30 of '/usr/local/proftpd/etc/proftpd.conf'
[root@asianux4 sbin]# groupadd nogroup    建立nogroup組
[root@asianux4 sbin]# ./proftpd     啓動proftpd
2015-09-16 09:46:28,209 asianux4 proftpd[21949]: warning: unable to determine IP address of 'asianux4'
2015-09-16 09:46:28,209 asianux4 proftpd[21949]: error: no valid servers configured
2015-09-16 09:46:28,209 asianux4 proftpd[21949]: fatal: error processing configuration file '/usr/local/proftpd/etc/proftpd.conf'
[root@asianux4 sbin]# vim /etc/hosts    在文件末尾添加最後一行內容。
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.232.128         asianux4

[root@asianux4 sbin]# ./proftpd     啓動proftpd服務
[root@asianux4 sbin]# netstat -atnup|grep :21     查看ftp端口,表示啓動成功。
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      21971/proftpd       
[root@asianux4 sbin]


安裝webmin軟件包
[root@asianux4 ~]# tar -zxf webmin-*.tar.gz -C /opt
[root@asianux4 ~]# cd /opt/webmin* 
[root@asianux4 webmin-1.680]# ./setup.sh 
***********************************************************************
*            Welcome to the Webmin setup script, version 1.680        *
***********************************************************************
Webmin is a web-based interface that allows Unix-like operating
systems and common Unix services to be easily administered.

Installing Webmin in /opt/webmin-1.680 ...

***********************************************************************
Webmin uses separate directories for configuration files and log files.
Unless you want to run multiple versions of Webmin at the same time
you can just accept the defaults.

Config file directory [/etc/webmin]: 
Log file directory [/var/webmin]: 

***********************************************************************
Webmin is written entirely in Perl. Please enter the full path to the
Perl 5 interpreter on your system.

Full path to perl (default /usr/bin/perl): 

Testing Perl ...
Perl seems to be installed ok

***********************************************************************
Operating system name:    Asianux Server
Operating system version: 4

***********************************************************************
Webmin uses its own password protected web server to provide access
to the administration programs. The setup script needs to know :
 - What port to run the web server on. There must not be another
   web server already using this port.
 - The login name required to access the web server.
 - The password required to access the web server.
 - If the webserver should use SSL (if your system supports it).
 - Whether to start webmin at boot time.

Web server port (default 10000): 
Login name (default admin): 
Login password: 
Password again: 
The Perl SSLeay library is not installed. SSL not available.
Start Webmin at boot time (y/n): n
***********************************************************************
Creating web server config files..
..done

Creating access control file..
..done

Inserting path to perl into scripts..
..done

Creating start and stop scripts..
..done

Copying config files..
..done

Creating uninstall script /etc/webmin/uninstall.sh ..
..done

Changing ownership and permissions ..
..done

Running postinstall scripts ..
Use of uninitialized value in split at /opt/webmin-1.680/acl/acl-lib.pl line 47.
Use of uninitialized value in -r at /opt/webmin-1.680/webalizer/webalizer-lib.pl line 16.
..done

Enabling background status collection ..
..done

Attempting to start Webmin mini web server..
Starting Webmin server in /opt/webmin-1.680
Pre-loaded WebminCore
..done

***********************************************************************
Webmin has been installed and started successfully. Use your web
browser to go to

  http://asianux4:10000/

and login with the name and password you entered previously.

[root@asianux4 webmin-1.680]# 

linux系統的網絡命令:
1、網卡的配置。
方法一:ifconfig eth0 192.168.232.128/24 up|down 設置網卡的臨時IP地址。Linux沒有重啓和network服務沒有重啓以前,此IP一直生效。
方法二:setup     經過嚮導設置IP地址。設置的內容直接寫入/etc/sysconfig/network-scripts/ifcfg-eth0文件。
方法三:dhclient自動獲取IP地址。
方法四:直接修改/etc/sysconfig/network-scripts/ifcfg-eth0文件。

[root@asianux4 webmin-1.680]# vim /etc/sysconfig/network-scripts/ifcfg-eth0   DEVICE=eth0    設備名稱eth0,eth1,eth2...
TYPE=Ethernet    網絡類型,以太網,atm,ppp
UUID=e43183b0-7e49-4033-bb5e-e5099139b04b    設備的UUID號。可選
ONBOOT=yes    開機時激活此網卡,默認爲不激活網卡。
NM_CONTROLLED=no是否容許networkmanager服務控制網卡,在axs4中建議關閉
BOOTPROTO=none    使用靜態IP地址,none,static,dhcp
HWADDR=00:0c:29:62:8c:77    MAC地址
DEFROUTE=yes    打啓默認路由
PEERROUTES=yes    打開路由功能
IPV4_FAILURE_FATAL=yes    IPV4故障是否轉移
IPV6INIT=no    是否開啓ipv6
NAME=eth0    網卡的別名信息
IPADDR=192.168.232.128    IP地址
NETMASK=255.255.255.0    子網掩碼
GATEWAY=192.168.137.1    網關
USERCTL=no    是否容許普通用戶控制。
[root@asianux4 webmin-1.680]# service network restart 重啓網卡服務,ifcfg-eth0文件的修改才能生效。不然不會生效。


方法一:ifconfig eth0 192.168.232.128/24 up|down  up激活,down禁用。

[root@asianux4 webmin-1.680]# ifconfig eth1 down
[root@asianux4 webmin-1.680]# ifconfig    
eth0      Link encap:Ethernet  HWaddr 00:0C:29:62:8C:77  
          inet addr:192.168.232.128  Bcast:192.168.232.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe62:8c77/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:32208 errors:0 dropped:0 overruns:0 frame:0
          TX packets:13788 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:32690904 (31.1 MiB)  TX bytes:2917230 (2.7 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:275 errors:0 dropped:0 overruns:0 frame:0
          TX packets:275 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:356211 (347.8 KiB)  TX bytes:356211 (347.8 KiB)

[root@asianux4 webmin-1.680]# ifconfig eth1 up
[root@asianux4 webmin-1.680]# ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 00:0C:29:62:8C:81  
          inet addr:10.6.65.180  Bcast:10.6.65.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe62:8c81/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:64152 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6000 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:6476428 (6.1 MiB)  TX bytes:755143 (737.4 KiB)
          Interrupt:16 Base address:0x2000 

[root@asianux4 webmin-1.680]# 

[root@asianux4 webmin-1.680]# ifconfig eth0:1 192.168.232.129/24 設置網卡子接口的IP地址。
[root@asianux4 webmin-1.680]# ping -c1 192.168.232.129     ping一個包
PING 192.168.232.129 (192.168.232.129) 56(84) bytes of data.
64 bytes from 192.168.232.129: icmp_seq=1 ttl=64 time=0.019 ms

--- 192.168.232.129 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.019/0.019/0.019/0.000 ms
[root@asianux4 webmin-1.680]# 

方法三:dhclient自動獲取IP地址。
[root@asianux4 webmin-1.680]# killall dhclient 殺死dhclient進程
[root@asianux4 webmin-1.680]# dhclient  自動獲取IP地址。
[root@asianux4 webmin-1.680]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:62:8C:77  
          inet addr:192.168.232.128  Bcast:192.168.232.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe62:8c77/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:32475 errors:0 dropped:0 overruns:0 frame:0
          TX packets:13995 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:32718100 (31.2 MiB)  TX bytes:2943680 (2.8 MiB)


路由的配置 route
查看路由:route -n或netstat -rn
[root@asianux4 webmin-1.680]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.137.1   0.0.0.0         255.255.255.255 UH        0 0          0 eth0
10.6.65.0       0.0.0.0         255.255.255.0   U         0 0          0 eth1
192.168.232.0   0.0.0.0         255.255.255.0   U         0 0          0 eth0
0.0.0.0         192.168.137.1   0.0.0.0         UG        0 0          0 eth0
[root@asianux4 webmin-1.680]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.137.1   0.0.0.0         255.255.255.255 UH    0      0        0 eth0
10.6.65.0       0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.232.0   0.0.0.0         255.255.255.0   U     1      0        0 eth0
0.0.0.0         192.168.137.1   0.0.0.0         UG    0      0        0 eth0
[root@asianux4 webmin-1.680]# 

添加或刪除主機路由:
route add -host 202.123.123.123 gw 192.168.232.100 dev eth0
route del -host 202.123.123.123

[root@asianux4 webmin-1.680]# route add -host 202.123.123.123 gw 192.168.232.100 dev eth0
[root@asianux4 webmin-1.680]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.137.1   0.0.0.0         255.255.255.255 UH    0      0        0 eth0
202.123.123.123 192.168.232.100 255.255.255.255 UGH   0      0        0 eth0
10.6.65.0       0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.232.0   0.0.0.0         255.255.255.0   U     1      0        0 eth0
0.0.0.0         192.168.137.1   0.0.0.0         UG    0      0        0 eth0

[root@asianux4 webmin-1.680]# route del -host 202.123.123.123
[root@asianux4 webmin-1.680]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.137.1   0.0.0.0         255.255.255.255 UH    0      0        0 eth0
10.6.65.0       0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.232.0   0.0.0.0         255.255.255.0   U     1      0        0 eth0
0.0.0.0         192.168.137.1   0.0.0.0         UG    0      0        0 eth0
[root@asianux4 webmin-1.680]# 

網絡路由的配置
route add -net 202.123.123.0/24 gw 10.6.65.100 dev eth1 添加網絡路由,表示到202.123.123.0網段的全部請求,都經過10.6.65.100主機出去。
route del -net 202.123.123.0/24  刪除路由

設置默認路由:
route add default gw 192.168.232.1    添加默認路由
rotue del deafult            刪除默認路由

查看主機開放的端口:
netstat -antup    查看全部的tcp,udp的鏈接會話
netstat -anlp    查看全部監聽會話和端口
lsof -i        查看全部監聽端口

[root@asianux4 webmin-1.680]# lsof -i
COMMAND     PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rpcbind    1099     rpc    6u  IPv4   9889      0t0  UDP *:sunrpc 
rpcbind    1099     rpc    7u  IPv4   9891      0t0  UDP *:850 
rpcbind    1099     rpc    8u  IPv4   9892      0t0  TCP *:sunrpc (LISTEN)
rpcbind    1099     rpc    9u  IPv6   9894      0t0  UDP *:sunrpc 
rpcbind    1099     rpc   10u  IPv6   9896      0t0  UDP *:850 
rpcbind    1099     rpc   11u  IPv6   9897      0t0  TCP *:sunrpc (LISTEN)
rpc.statd  1228 rpcuser    5r  IPv4  10172      0t0  UDP *:980 
rpc.statd  1228 rpcuser    8u  IPv4  10178      0t0  UDP *:47899 
rpc.statd  1228 rpcuser    9u  IPv4  10182      0t0  TCP *:46978 (LISTEN)
rpc.statd  1228 rpcuser   10u  IPv6  10186      0t0  UDP *:60938 
rpc.statd  1228 rpcuser   11u  IPv6  10190      0t0  TCP *:35925 (LISTEN)
cupsd      1257    root    6u  IPv6  10325      0t0  TCP localhost:ipp (LISTEN)
cupsd      1257    root    7u  IPv4  10326      0t0  TCP localhost:ipp (LISTEN)
cupsd      1257    root    9u  IPv4  10329      0t0  UDP *:ipp 
sshd       1403    root    3u  IPv4  10939      0t0  TCP *:ssh (LISTEN)
sshd       1403    root    4u  IPv6  10941      0t0  TCP *:ssh (LISTEN)
master     1479    root   12u  IPv4  11173      0t0  TCP localhost:smtp (LISTEN)
master     1479    root   13u  IPv6  11175      0t0  TCP localhost:smtp (LISTEN)
sshd       2543    root    3r  IPv4  13495      0t0  TCP asianux4:ssh->192.168.232.1:conferencetalk (ESTABLISHED)
proftpd   21971  nobody    0u  IPv4  59915      0t0  TCP *:ftp (LISTEN)
miniserv. 22762    root    6u  IPv4  86123      0t0  TCP *:ndmp (LISTEN)
miniserv. 22762    root    7u  IPv4  86124      0t0  UDP *:ndmp 
dhclient  25617    root    9u  IPv4  93190      0t0  UDP *:bootpc 
[root@asianux4 webmin-1.680]# 

[root@asianux4 webmin-1.680]# netstat -atnup|more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program
 name   
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1099/rpcbin
d        
tcp        0      0 0.0.0.0:10000               0.0.0.0:*                   LISTEN      22762/perl 
         
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      21971/proft
pd       
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1403/sshd  
         
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      1257/cupsd 
         
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      1479/master
         
tcp        0      0 0.0.0.0:46978               0.0.0.0:*                   LISTEN      1228/rpc.st
atd      
tcp        0     48 192.168.232.128:22          192.168.232.1:1713          ESTABLISHED 2543/sshd  
         
tcp        0      0 :::111                      :::*                        LISTEN      1099/rpcbin
d        
tcp        0      0 :::35925                    :::*                        LISTEN      1228/rpc.st
atd      
tcp        0      0 :::22                       :::*                        LISTEN      1403/sshd  


檢查網絡的連通性 ping -c 4 192.168.232.128
檢查網絡的路徑   traceroute 202.96.128.86 檢查到202.96.128.86路徑的路由。
[root@asianux4 webmin-1.680]# traceroute 192.168.232.128
traceroute to 192.168.232.128 (192.168.232.128), 30 hops max, 60 byte packets
 1  asianux4 (192.168.232.128)  0.013 ms  0.002 ms  0.001 ms
[root@asianux4 webmin-1.680]# traceroute 192.168.232.1  
traceroute to 192.168.232.1 (192.168.232.1), 30 hops max, 60 byte packets
 1  192.168.232.1 (192.168.232.1)  0.104 ms  0.061 ms  0.058 ms
[root@asianux4 webmin-1.680]# 

下載軟件包。wget
[root@asianux4 webmin-1.680]# cd /var/ftp/
[root@asianux4 ftp]# tar -cJf log.tar.gz /var/log/
tar: Removing leading `/' from member names
[root@asianux4 ftp]# ls
log.tar.gz  pub  upload
[root@asianux4 ftp]# cd
[root@asianux4 ~]# pwd
/root
[root@asianux4 ~]# wget ftp://192.168.232.128/log.tar.gz . 將192.168.232.128服務器上的log.tar.gz文件下載到當前目錄。
--2015-09-16 11:06:13--  ftp://192.168.232.128/log.tar.gz
           => `log.tar.gz'
Connecting to 192.168.232.128:21... connected.
Logging in as anonymous ... Logged in!
==> SYST ... done.    ==> PWD ... done.
==> TYPE I ... done.  ==> CWD not needed.
==> SIZE log.tar.gz ... 210112
==> PASV ... done.    ==> RETR log.tar.gz ... done.
Length: 210112 (205K) (unauthoritative)

100%[=========================================================>] 210,112     --.-K/s   in 0s      

2015-09-16 11:06:13 (758 MB/s) - `log.tar.gz' saved [210112]

--2015-09-16 11:06:13--  http://./
Resolving . (.)... failed: Temporary failure in name resolution.
wget: unable to resolve host address `.'
FINISHED --2015-09-16 11:06:13--
Downloaded: 1 files, 205K in 0s (758 MB/s)
[root@asianux4 ~]# ls log.tar.gz
log.tar.gz
[root@asianux4 ~]# 

DNS服務器解析的命令。
nslookup www.baidu.com    將www.badiu.com域名解析爲IP地址
host www.baidu.com    將www.badiu.com域名解析爲IP地址
dig www.baidu.com    將www.badiu.com域名解析爲IP地址

host 192.168.232.128    將192.168.233.128解析成域名

文本界面的網頁瀏覽器curl
[root@asianux4 ~]# service httpd start    啓動httpd服務
???? httpd:httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.232.128 for ServerName
[??]
[root@asianux4 ~]# curl http://127.0.0.1 瀏覽www服務器的主頁。


網絡基礎相關的配置文件
/etc/sysconfig/network-scripts/ifcfg-eth0 eth0網卡的配置文件
/etc/sysconfig/network-scripts/ifcfg-lo      lo環路網卡的配置文件
/etc/hosts    IP地址和主機名|域名對應關係文件
/etc/service    端口和服務的對應關係文件
[root@asianux4 ~]# cat /etc/services |grep www |grep 80
http            80/tcp          www www-http    # WorldWideWeb HTTP
http            80/udp          www www-http    # HyperText Transfer Protocol
[root@asianux4 ~]# cat /etc/services |grep ftp |grep 21
# 21 is registered to ftp, but also used by fsp
ftp             21/tcp
ftp             21/udp          fsp fspd

/etc/resolv.conf    DNS客戶端配置文件
[root@asianux4 ~]# cat /etc/resolv.conf 
; generated by /sbin/dhclient-script
search localdomain        搜索域名
nameserver 192.168.232.1    定義DNS服務器的IP地址,最多能夠寫三行nameserver.
9.16-1
遠程管理:
文本模式:ssh/openssh
圖形模式:webmin,vnc

Openssh:
1、遠程管理
2、遠程數據傳輸,上傳和下載數據。
3、壓縮和加密功能
4、圖形傳輸,將對方顯示的圖形界面,傳輸到本地顯示,遠程不顯示。

[root@asianux4 ~]# rpm -qa |grep openssh
openssh-server-5.3p1-94.AXS4.x86_64    服務器軟件包
openssh-askpass-5.3p1-94.AXS4.x86_64
openssh-5.3p1-94.AXS4.x86_64
openssh-clients-5.3p1-94.AXS4.x86_64    客戶端
[root@asianux4 ~]# netstat -atnup|grep :22
tcp        0      0 0.0.0.0:22      0.0.0.0:*       LISTEN      1403/sshd  
         
[root@asianux4 ~]# cd /etc/ssh/
[root@asianux4 ssh]# ls
moduli      ssh_host_dsa_key      ssh_host_key      ssh_host_rsa_key      sshd_config
ssh_config  ssh_host_dsa_key.pub  ssh_host_key.pub  ssh_host_rsa_key.pub
[root@asianux4 ssh]# 

sshd_config 主配置文件,服務器的配置文件。
ssh_config 客戶端的配置文件
ssh_host_key ssh_host_key.pub v1版本的對稱密鑰
ssh_host_dsa_key ssh_host_dsa_key.pub
ssh_host_rsa_key ssh_host_rsa_key.pub v2版本的非對稱密鑰

v1和v2版本不兼容。

/etc/ssh/sshd_config主配置文件。
 13 #Port 22    ssh服務的默認端口
 15 #ListenAddress 0.0.0.0    監聽的IP地址。
 21 Protocol 2    v2版本協議
 36 SyslogFacility AUTHPRIV    日誌類型,將ssh的日誌的記錄到/var/log/secure文件
 37 LogLevel INFO    日誌錯誤級別
 42 PermitRootLogin yes    容許root管理員遠程登陸,no表示拒絕。
 45 #MaxSessions 10    最大會話數。
 49 AuthorizedKeysFile .ssh/authorized_keys    ssh服務器的公鑰文件
 66 PasswordAuthentication yes    容許使用本地密碼認證。ssh支持兩種認證方式,分別1)本地密碼認證,2)rsa|dsa非對稱密鑰認證,優先高。

 81 GSSAPIAuthentication yes    GSSAPI認證
109 X11Forwarding yes    X11轉發,將遠端的圖形傳輸入到本地顯示。
122 #UseDNS yes        是否使用DNS解析。若是沒有DNS服務器,建議關閉UseDNS no
132 Subsystem   sftp  /usr/libexec/openssh/sftp-server    開啓sftp服務器

實例:
提高sshd服務的安全性:
1、關閉root遠程登陸,若是root沒法關閉,建議使用保壘機。
2、修復sshd的漏洞。(忽略)
3、容許普通用戶在特定的IP或網段登陸。
4、只容許非對稱密鑰認證。(忽略)
5、監聽特定的某個端口。

[root@asianux4 ssh]# cat /etc/ssh/sshd_config |grep -vnE '^$|^#'
17:ListenAddress 192.168.232.128    限制的監聽IP地址
22:Protocol 2
37:SyslogFacility AUTHPRIV
38:LogLevel INFO
43:PermitRootLogin no
44:AllowUsers hsx123abd@192.168.232.0/24禁止root用戶登陸,只容許hsx123abd在192.168.232.0/24網段登陸。
68:PasswordAuthentication no        關閉本地密碼認證
72:ChallengeResponseAuthentication no
83:GSSAPIAuthentication yes
85:GSSAPICleanupCredentials yes
99:UsePAM yes
102:AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
103:AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
104:AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
105:AcceptEnv XMODIFIERS
111:X11Forwarding yes
134: # Subsystem   sftp    /usr/libexec/openssh/sftp-server    關閉sftp服務
[root@asianux4 ssh]# 
[root@asianux4 ssh]# useradd hsx123abd 
[root@asianux4 ssh]# passwd hsx123abd
[root@asianux4 ssh]# ssh hsx123abd@192.168.232.128
hsx123abd@192.168.232.128's password: 
[hsx123abd@asianux4 ~]$ exit
logout
Connection to 192.168.232.128 closed.

實例2:
建立hsx123abd的非對稱密碼認證。
[root@asianux4 ssh]# su - hsx123abd        切換用戶到hsx123abd
[hsx123abd@asianux4 ~]$ ssh-keygen -t rsa    建立rsa密鑰,-t dsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hsx123abd/.ssh/id_rsa): 
Created directory '/home/hsx123abd/.ssh'.    
Enter passphrase (empty for no passphrase):     輸入密鑰的密碼
Enter same passphrase again:             輸入密鑰的密碼
Your identification has been saved in /home/hsx123abd/.ssh/id_rsa.
Your public key has been saved in /home/hsx123abd/.ssh/id_rsa.pub.
The key fingerprint is:
3d:da:6e:9a:00:29:0a:db:12:01:18:ff:6a:f6:97:3c hsx123abd@asianux4
The key's randomart image is:
+--[ RSA 2048]----+
|+.               |
|o.               |
|. .              |
| . . .   .       |
|o . +   S o      |
|.= o .   o .     |
|+ =  .... .      |
| + .  E. o.      |
|    .. .oo.      |
+-----------------+
[hsx123abd@asianux4 ~]$ cd /home/hsx123abd/.ssh/
[hsx123abd@asianux4 .ssh]$ ls    發現已經有了公鑰和私鑰
id_rsa  id_rsa.pub
[hsx123abd@asianux4 .ssh]$ scp id_rsa.pub hsx123abd@192.168.232.128:/home/hsx123abd/.ssh/authorized_keys    將本地的公鑰上傳到192.168.232.128服務器的指定用戶名上。
The authenticity of host '192.168.232.128 (192.168.232.128)' can't be established.
RSA key fingerprint is 60:81:13:db:e2:65:e7:a9:20:9d:67:a5:d2:28:d5:3c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.232.128' (RSA) to the list of known hosts.
hsx123abd@192.168.232.128's password: 
id_rsa.pub                                                       100%  400     0.4KB/s   00:00    
[hsx123abd@asianux4 .ssh]$ ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
[hsx123abd@asianux4 .ssh]$ ssh hsx123abd@192.168.232.128    使用密鑰對認證登陸
Enter passphrase for key '/home/hsx123abd/.ssh/id_rsa': 
Enter passphrase for key '/home/hsx123abd/.ssh/id_rsa': 
Enter passphrase for key '/home/hsx123abd/.ssh/id_rsa': 
hsx123abd@192.168.232.128's password: 
Permission denied, please try again.
hsx123abd@192.168.232.128's password: 
Permission denied, please try again.
hsx123abd@192.168.232.128's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[hsx123abd@asianux4 .ssh]$ 
[hsx123abd@asianux4 .ssh]$ ssh hsx123abd@192.168.232.128
Enter passphrase for key '/home/hsx123abd/.ssh/id_rsa': 
Last login: Wed Sep 16 15:06:50 2015 from 192.168.232.1  登陸成功

[hsx123abd@asianux4 ~]$ w 顯示全部鏈接到本機的管理會話
 15:15:25 up 20:05,  3 users,  load average: 0.04, 0.02, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                Tue18   20:15m  0.10s  0.10s -bash
hsx123ab pts/0    192.168.232.128  15:15    0.00s  0.00s  0.00s w
root     pts/1    192.168.232.1    Tue18    0.00s  1.11s  0.00s ssh hsx123abd@192.168.232.128
[hsx123abd@asianux4 ~]$ 

實例3:
經過windows遠程管理linux系統。
1、SecureCRT:至關於linux系統的ssh命令
2、putty:至關於linux系統的ssh命令
3、SSH Secure Shell Client:至關於linux系統的ssh命令

經過windows遠程數據傳輸到linux系統。
一、winscp|SSH Secure File Transfer Client:至關於linux的scp和sftp命令。

[root@asianux4 hsx123abd]# ll /home/
total 12
drwx------ 5 hsx123abd hsx123abd 4096 Sep 16 15:10 hsx123abd
drwx------ 4 user1     user1     4096 Sep 15 11:02 user1
drwx------ 4 user2     user2     4096 Sep 15 11:11 user2
[root@asianux4 hsx123abd]# cd /home/hsx123abd
[root@asianux4 hsx123abd]# sftp hsx123abd@192.168.232.128    遠程訪問sftp服務
Connecting to 192.168.232.128...
hsx123abd@192.168.232.128's password: 
sftp> cd /tmp
sftp> get 1.tar.gz     下載1.tar.gz文件到本地
Fetching /tmp/1.tar.gz to 1.tar.gz
/tmp/1.tar.gz                                                    100%   87KB  87.0KB/s   00:00    
sftp> quit
[root@asianux4 hsx123abd]# ls    顯示下載成功。
1.tar.gz
[root@asianux4 hsx123abd]# 



圖形的遠程VNC
1、卸載VNC軟件包。
[root@asianux4 .vnc]# rpm -qa|grep vnc
tigervnc-server-1.1.0-8.0.2.AXS4.x86_64
[root@asianux4 .vnc]# rpm -e tigervnc-server-1.1.0-8.0.2.AXS4.x86_64
warning: /etc/sysconfig/vncservers saved as /etc/sysconfig/vncservers.rpmsave
[root@asianux4 .vnc]# rm /root/.vnc/* -rf
[root@asianux4 .vnc]# rm /etc/sysconfig/vncservers.rpmsave -rf
[root@asianux4 .vnc]#

2、安裝VNC軟件包。
[root@asianux4 ~]# yum install tigervnc-server -y

3、建立VNC服務器
[root@asianux4 ~]# vncserver :1    建立VNC端口

You will require a password to access your desktops.

Password:    輸入VNC中root用戶的密碼
Verify:        輸入vnc中root用戶的密碼

New 'asianux4:1 (root)' desktop is asianux4:1

Creating default startup script /root/.vnc/xstartup
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/asianux4:1.log

[root@asianux4 ~]# cd /root/.vnc/
[root@asianux4 .vnc]# ls
asianux4:1.log  asianux4:1.pid  passwd  xstartup
asianux4:1.log vnc鏈接時的日誌
asianux4:1.pid vnc運行時的PID號,進程ID號。
passwd        VNC當前用戶的密碼
xstartup    VNC初始化文件(***)

[root@asianux4 .vnc]# vim xstartup    編輯文件,將文件末尾的twm更改gnome-session。保存退出。
[root@asianux4 .vnc]# tail -2 xstartup 
xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
gnome-session &
[root@asianux4 .vnc]#


[root@asianux4 .vnc]# vncserver -kill :1    殺死:1端口
Killing Xvnc process ID 31911
[root@asianux4 .vnc]# vim /etc/sysconfig/vncservers    在此文件末尾添加如下行
VNCSERVERS="2:root"

[root@asianux4 .vnc]# tail -1 /etc/sysconfig/vncservers    顯示最後一行的內容
VNCSERVERS="2:root"
[root@asianux4 .vnc]# service vncserver restart    重啓VNC服務
關閉 VNC 服務器:                                          [肯定]
正在啓動 VNC 服務器:2:root
New 'asianux4:2 (root)' desktop is asianux4:2

Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/asianux4:2.log            [肯定]

[root@asianux4 .vnc]# netstat -atnup|grep -i vnc    顯示的端口,表示已啓動成功
tcp        0      0 0.0.0.0:5902                0.0.0.0:*                   LISTEN      32257/Xvnc
tcp        0      0 0.0.0.0:6002                0.0.0.0:*                   LISTEN      32257/Xvnc
tcp        0      0 :::6002                     :::*                        LISTEN      32257/Xvnc
[root@asianux4 .vnc]#

在windows上,打開vncviewer,輸入192.168.232.128:2,鏈接linux服務囂。
在linux上,安裝vnc軟件包。再執行vncviewer遠程鏈接。
[root@asianux4 .vnc]# yum install vnc -y
[root@asianux4 .vnc]# vncviewer 192.168.232.128:2

在windows|linux上,使用瀏覽器訪問VNC服務器,輸入網址http://192.168.232.128:5902/

磁盤管理:
Linux目錄系統:linux系統是一個單根系統,/
/bin /usr/bin  /usr/local/bin  /xxx/bin 普通用戶能夠執行的命令
/sbin /usr/sbin /usr/local/sbin /xx/sbin 超級管理員能夠執行的命令
/etc/ /usr/etc  /usr/local/etc  /xx/etc  服務的配置文件
/lib  /usr/lib  /usr/local/lib  /xx/lib  動態連接庫文件
/lib64     64位的動態連接庫文件

/boot    系統引導的目錄,大小約爲100~500M,只在系統啓動有效。
/dev    設備文件目錄
/home      普通用戶的自家目錄
/root    root用戶的自家目錄
/media/  /mnt/    媒體設備的掛載點。(磁盤,光盤,U盤,SD卡)
/misc  /net    autofs服務的掛載點。

/lost+found     fsck校驗後找回文件目錄
/opt    第三方軟件存放目錄,這是一個約定。
/proc    內核的參數,系統的運行進程,硬件的配置目錄
/selinux    selinux的配置文件目錄
/srv    ftp或www服務器資源存放目錄,這是一個約定。
/sys    系統固件的信息。
/tmp /usr/tmp    臨時文件目錄
/usr    Linux系統中最大的目錄,存放命令、配置文件、動態連接庫,幫助,文檔等
/var    日誌和我的郵箱的目錄

文件系統管理:
ext2/ext3/ext4/xfs    linux文件系統
iso9660,udf    光盤文件系統
nfs    網絡文件系統
cifs    共享的文件系統
vfat    fat32文件系統
ntfs-3g    NTFS文件系統
swap    虛擬內存的文件系統

磁盤分區:
實例:
對/dev/sdb磁盤作分區,要求主分區大小爲1G,linux系統類型;擴展分區大小爲剩餘空間,在擴展分區上建立兩個邏輯分區,第一個邏輯分區大小爲2G,LVM系統類型;第二個邏輯分區大小剩餘空間,swap系統類型。
[root@asianux4 /]# fdisk /dev/sdb    對/dev/sdb硬盤作分區
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x0e144a16.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): m    顯示幫助
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition        刪除分區
   l   list known partition types
   m   print this menu            顯示幫助菜單
   n   add a new partition        建立分區
   o   create a new empty DOS partition table
   p   print the partition table    顯示分區
   q   quit without saving changes    不保存退出
   s   create a new empty Sun disklabel
   t   change a partition's system id    改變分區的ID號
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit    保存退出
   x   extra functionality (experts only)

Command (m for help): n    建立分區
Command action
   e   extended
   p   primary partition (1-4)
p    主分區
Partition number (1-4): 1    輸入主分區號
First cylinder (1-522, default 1): 開始柱面
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-522, default 522): +1G 結束柱面

Command (m for help): p

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0e144a16

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4): 4
First cylinder (133-522, default 133):
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-522, default 522):
Using default value 522

Command (m for help): p

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0e144a16

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
/dev/sdb4             133         522     3132675    5  Extended

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (133-522, default 133):
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-522, default 522): +2G

Command (m for help): p

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0e144a16

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
/dev/sdb4             133         522     3132675    5  Extended
/dev/sdb5             133         394     2104483+  83  Linux

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (395-522, default 395):
Using default value 395
Last cylinder, +cylinders or +size{K,M,G} (395-522, default 522):
Using default value 522

Command (m for help): p

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0e144a16

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
/dev/sdb4             133         522     3132675    5  Extended
/dev/sdb5             133         394     2104483+  83  Linux
/dev/sdb6             395         522     1028128+  83  Linux

Command (m for help): t    改變分區系統ID號
Partition number (1-6): 5
Hex code (type L to list codes): L 顯示分區系統ID號的類型

83 linux
82  swap交換
8e  lvm
fd  RAID
b/c FAT32
7/17 ntfs

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris      
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx       
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data  
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility 
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt       
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access   
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O      
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor    
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs      
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT          
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor    
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor    
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS  
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep      
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT          
1e  Hidden W95 FAT1
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0e144a16

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
/dev/sdb4             133         522     3132675    5  Extended
/dev/sdb5             133         394     2104483+  8e  Linux LVM
/dev/sdb6             395         522     1028128+  83  Linux

Command (m for help): t
Partition number (1-6): 6
Hex code (type L to list codes): 82
Changed system type of partition 6 to 82 (Linux swap / Solaris)

Command (m for help): p

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0e144a16

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
/dev/sdb4             133         522     3132675    5  Extended
/dev/sdb5             133         394     2104483+  8e  Linux LVM
/dev/sdb6             395         522     1028128+  82  Linux swap / Solaris

Command (m for help): w    保存
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@asianux4 /]# partprobe    將更改的分區信息寫入到內核表上。若是寫入不成功,則須要使用reboot重啓。

Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (設備或資源忙).  As a result, it may not reflect all of your changes until after reboot.
Warning: 沒法以讀寫方式打開 /dev/sr0 (只讀文件系統)。/dev/sr0 已按照只讀方式打開。
[root@asianux4 /]# cat /proc/partitions    查看分區信息
major minor  #blocks  name

   8        0    8388608 sda
   8        1     512000 sda1
   8        2    7875584 sda2
   8       16    4194304 sdb
   8       17    1060258 sdb1
   8       20          1 sdb4
   8       21    2104483 sdb5
   8       22    1028128 sdb6
   8       48    4194304 sdd
   8       32    4194304 sdc
   8       80    4194304 sdf
   8       64    4194304 sde
 253        0    7036928 dm-0
 253        1     835584 dm-1
[root@asianux4 /]# mkfs
mkfs          mkfs.ext2     mkfs.ext4     mkfs.msdos
mkfs.cramfs   mkfs.ext3     mkfs.ext4dev  mkfs.vfat
[root@asianux4 /]# mkfs.ext4 /dev/sdb1    建立文件系統,格式化
mke2fs 1.41.12 (17-May-2010)
文件系統標籤=
操做系統:Linux
塊大小=4096 (log=2)
分塊大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
66384 inodes, 265064 blocks
13253 blocks (5.00%) reserved for the super user
第一個數據塊=0
Maximum filesystem blocks=272629760
9 block groups
32768 blocks per group, 32768 fragments per group
7376 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376

正在寫入inode表: 完成
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@asianux4 /]# mkdir /mnt/sdb1        建立掛載點
[root@asianux4 /]# mount /dev/sdb1 /mnt/sdb1/    掛載使用
[root@asianux4 /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_asianux4-lv_root
                      6.7G  3.2G  3.2G  51% /
tmpfs                 935M     0  935M   0% /dev/shm
/dev/sda1             485M   39M  421M   9% /boot
/dev/sr0              3.8G  3.8G     0 100% /media
/dev/sdb1            1020M   34M  935M   4% /mnt/sdb1
[root@asianux4 /]# cp /root/install.* /mnt/sdb1/
[root@asianux4 /]# ls /mnt/sdb1/
install.log  install.log.syslog  install.sh  lost+found
[root@asianux4 /]# vim /etc/fstab
[root@asianux4 /]# tail -2 /etc/fstab
/dev/sr0                /media                  iso9660 defaults        0 0
/dev/sdb1               /mnt/sdb1               ext4    defaults        0 0
[root@asianux4 /]#
9.16-2
課後練習:
1、對/dev/sdb磁盤作分區,要求主分區大小爲1G,linux系統類型;擴展分區大小爲剩餘空間,在擴展分區上建立兩個邏輯分區,第一個邏輯分區大小爲2G,LVM系統類型;第二個邏輯分區大小剩餘空間,swap系統類型。將/dev/sdb1格式化爲ext4,並設置開機時自動掛載到/mnt/sdb1目錄上。

2、打開user10的VNC服務器端口:3.並在windows上使用vncviewer工具測試經過。

3、openssh配置實驗,要求實現如下功能。
1、打開root用戶遠程登陸功能
2、採用DSA密鑰對方式登陸,打開本地密碼登陸。
3、設置ssh的監聽端口爲2222。

4、設置eth0網卡的第二個IP地址爲192.168.x.100/24,並設置開機時自動激活。

5、安裝proftpd服務器,並將/var/log目錄的備份到/var/ftp/log.tar.gz文件中,並經過ftp下載到windows上。

6、設置到達202.1.2.3主機的全部請求包,經過eth0轉發。
9.16-課後練習
課後練習:
1、對/dev/sdb磁盤作分區,要求主分區大小爲1G,linux系統類型;擴展分區大小爲剩餘空間,在擴展分區上建立兩個邏輯分區,第一個邏輯分區大小爲2G,LVM系統類型;第二個邏輯分區大小剩餘空間,swap系統類型。將/dev/sdb1格式化爲ext4,並設置開機時自動掛載到/mnt/sdb1目錄上。
[root@asianux4 .vnc]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0e144a16

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
/dev/sdb4             133         522     3132675    5  Extended
/dev/sdb5             133         394     2104483+  8e  Linux LVM
/dev/sdb6             395         522     1028128+  82  Linux swap / Solaris

Command (m for help): d
Partition number (1-6): 6

Command (m for help): d
Partition number (1-5): d
Partition number (1-5): 4

Command (m for help): d
Selected partition 1

Command (m for help): p

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0e144a16

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): p

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0e144a16

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 設備或資源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@asianux4 .vnc]# umount /dev/sdb1
[root@asianux4 .vnc]# partprobe
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (設備或資源忙).  As a result, it may not reflect all of your changes until after reboot.
Warning: 沒法以讀寫方式打開 /dev/sr0 (只讀文件系統)。/dev/sr0 已按照只讀方式打開。
[root@asianux4 .vnc]# cat /proc/partitions
major minor  #blocks  name

   8        0    8388608 sda
   8        1     512000 sda1
   8        2    7875584 sda2
   8       16    4194304 sdb
   8       48    4194304 sdd
   8       32    4194304 sdc
   8       80    4194304 sdf
   8       64    4194304 sde
 253        0    7036928 dm-0
 253        1     835584 dm-1
[root@asianux4 .vnc]# clear
[root@asianux4 .vnc]# ls
asianux4:1.log  asianux4:2.log  asianux4:2.pid  passwd  xstartup
[root@asianux4 .vnc]# netstat -atnup|grep vnc
tcp        0      0 0.0.0.0:5903                0.0.0.0:*                   LISTEN      2812/Xvnc
tcp        0      0 0.0.0.0:6003                0.0.0.0:*                   LISTEN      2812/Xvnc
tcp        0      0 :::6003                     :::*                        LISTEN      2812/Xvnc
[root@asianux4 .vnc]# kill 2812
[root@asianux4 .vnc]# netstat -atnup|grep vnc
[root@asianux4 .vnc]# userdel user10
userdel:用戶 user10 目前已登陸
[root@asianux4 .vnc]# clear
[root@asianux4 .vnc]# userdel user10
[root@asianux4 .vnc]# userdel user10
userdel: user 'user10' does not exist
[root@asianux4 .vnc]# clear
[root@asianux4 .vnc]# ls
asianux4:1.log  asianux4:2.log  asianux4:2.pid  passwd  xstartup
[root@asianux4 .vnc]# fdisk -l /dev/sdb

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0e144a16

   Device Boot      Start         End      Blocks   Id  System
[root@asianux4 .vnc]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0e144a16

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-522, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-522, default 522): +1G

Command (m for help): N
Command action
   e   extended
   p   primary partition (1-4)
E
Partition number (1-4): 2
First cylinder (133-522, default 133):
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-522, default 522):
Using default value 522

Command (m for help): P

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0e144a16

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
/dev/sdb2             133         522     3132675    5  Extended

Command (m for help): N
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
L
First cylinder (133-522, default 133):
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-522, default 522): +2G

Command (m for help): N
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
L
First cylinder (395-522, default 395):
Using default value 395
Last cylinder, +cylinders or +size{K,M,G} (395-522, default 522):
Using default value 522

Command (m for help): P

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0e144a16

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
/dev/sdb2             133         522     3132675    5  Extended
/dev/sdb5             133         394     2104483+  83  Linux
/dev/sdb6             395         522     1028128+  83  Linux

Command (m for help): T
Partition number (1-6): 5
Hex code (type L to list codes): 8E
Changed system type of partition 5 to 8e (Linux LVM)

Command (m for help): T
Partition number (1-6): 6
Hex code (type L to list codes): 82
Changed system type of partition 6 to 82 (Linux swap / Solaris)

Command (m for help): P

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0e144a16

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
/dev/sdb2             133         522     3132675    5  Extended
/dev/sdb5             133         394     2104483+  8e  Linux LVM
/dev/sdb6             395         522     1028128+  82  Linux swap / Solaris

Command (m for help): W
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@asianux4 .vnc]# partprobe
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (設備或資源忙).  As a result, it may not reflect all of your changes until after reboot.
Warning: 沒法以讀寫方式打開 /dev/sr0 (只讀文件系統)。/dev/sr0 已按照只讀方式打開。
[root@asianux4 .vnc]# cat /proc/partitions
major minor  #blocks  name

   8        0    8388608 sda
   8        1     512000 sda1
   8        2    7875584 sda2
   8       16    4194304 sdb
   8       17    1060258 sdb1
   8       18          1 sdb2
   8       21    2104483 sdb5
   8       22    1028128 sdb6
   8       48    4194304 sdd
   8       32    4194304 sdc
   8       80    4194304 sdf
   8       64    4194304 sde
 253        0    7036928 dm-0
 253        1     835584 dm-1
[root@asianux4 .vnc]# mkdir /mnt/sdb1
[root@asianux4 .vnc]# mount /dev/sdb1 /mnt/sdb1
[root@asianux4 .vnc]# vim /etc/fstab
[root@asianux4 .vnc]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed Sep  9 22:35:47 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_asianux4-lv_root /                       ext4    defaults        1 1
UUID=708b4c94-abb9-466b-adf9-7ea1b500697e /boot                   ext4    defaults        1 2
/dev/mapper/vg_asianux4-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sr0                /media                  iso9660 defaults        0 0
/dev/sdb1               /mnt/sdb1               ext4    defaults        0 0
[root@asianux4 .vnc]#

2、打開user10的VNC服務器端口:3.並在windows上使用vncviewer工具測試經過。
[root@asianux4 home]# useradd user10
[root@asianux4 home]# cat /etc/passwd|grep user10
user10:x:503:504::/home/user10:/bin/bash

[root@asianux4 home]# rpm -qa |grep -i vnc
tigervnc-1.1.0-8.0.2.AXS4.x86_64
tigervnc-server-1.1.0-8.0.2.AXS4.x86_64

[root@asianux4 home]# su - user10
[user10@asianux4 ~]$ pwd
/home/user10
[user10@asianux4 ~]$ vncserver :3

You will require a password to access your desktops.

Password:
Verify:
xauth:  creating new authority file /home/user10/.Xauthority

New 'asianux4:3 (user10)' desktop is asianux4:3

Creating default startup script /home/user10/.vnc/xstartup
Starting applications specified in /home/user10/.vnc/xstartup
Log file is /home/user10/.vnc/asianux4:3.log

[user10@asianux4 ~]$ cd /home/user10/.vnc/
[user10@asianux4 .vnc]$ ls
asianux4:3.log  asianux4:3.pid  passwd  xstartup
[user10@asianux4 .vnc]$ vim xstartup
[user10@asianux4 .vnc]$ tail -1 xstartup
gnome-session &
[user10@asianux4 .vnc]$ exit
logout
[root@asianux4 home]# vim /etc/sysconfig/vncservers
[root@asianux4 home]# cat -n /etc/sysconfig/vncservers |tail -1
    21  VNCSERVERS="3:user10"
[root@asianux4 home]# service vncserver restart
關閉 VNC 服務器:3:user10                                  [肯定]
正在啓動 VNC 服務器:3:user10
New 'asianux4:3 (user10)' desktop is asianux4:3

Starting applications specified in /home/user10/.vnc/xstartup
Log file is /home/user10/.vnc/asianux4:3.log

                                                           [肯定]
[root@asianux4 home]#

3、openssh配置實驗,要求實現如下功能。
1、打開root用戶遠程登陸功能
2、採用DSA密鑰對方式登陸,打開本地密碼登陸。
3、設置ssh的監聽端口爲2222。

[root@asianux4 home]# vim /etc/ssh/sshd_config
 13 Port 2222
 42 PermitRootLogin yes
 66 PasswordAuthentication yes

[root@asianux4 home]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
06:10:1b:2e:55:a1:1a:7f:af:7d:1e:96:e7:c2:4a:11 root@asianux4
The key's randomart image is:
+--[ DSA 1024]----+
|    =oo.         |
|   o =           |
|  o + . E        |
|   =   . .       |
|  . . . S        |
|     . o . .     |
|        o.+ .    |
|       + .++     |
|      . o+...    |
+-----------------+
[root@asianux4 home]# cd /root/.ssh/
[root@asianux4 .ssh]# ls
id_dsa  id_dsa.pub  known_hosts
[root@asianux4 .ssh]# cp id_dsa.pub authorized_keys
[root@asianux4 .ssh]# ssh 192.168.232.128 -p 2222
ssh: connect to host 192.168.232.128 port 2222: Connection refused
[root@asianux4 .ssh]# service sshd restart
中止 sshd:                                                [肯定]
正在啓動 sshd:                                            [肯定]
[root@asianux4 .ssh]# ssh 192.168.232.128 -p 2222
Enter passphrase for key '/root/.ssh/id_dsa':
Last login: Wed Sep 16 14:40:17 2015 from 192.168.232.1
[root@asianux4 ~]# w
 20:42:47 up  3:56,  3 users,  load average: 0.02, 0.01, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
hsx123ab pts/0    192.168.232.1    16:46    0.00s  0.37s  0.02s sshd: hsx123
user10   pts/1    :3.0             20:38    4:36   0.00s  0.00s /bin/bash
root     pts/2    192.168.232.128  20:42    0.00s  0.00s  0.00s w
[root@asianux4 ~]#


4、設置eth0網卡的第二個IP地址爲192.168.x.100/24,並設置開機時自動激活。
[root@asianux4 ~]# vim /etc/rc.local
  8 ifconfig eth0:1 192.168.232.100/24


5、安裝proftpd服務器,並將/var/log目錄的備份到/var/ftp/log.tar.gz文件中,並經過ftp下載到windows上。
[root@asianux4 ~]# tar -zxf proftpd*.tar.gz -C /opt
[root@asianux4 ~]# cd /opt/proftpd*
[root@asianux4 ~]# mount /dev/cdrom /media; yum install gcc -y
[root@asianux4 ~]# ./configure --prefix=/usr/local/proftpd
[root@asianux4 ~]# make;make install
[root@asianux4 ~]# groupadd nogroup
[root@asianux4 ~]# echo "192.168.232.128    asianux4" >> /etc/hosts
[root@asianux4 ~]# /usr/local/proftpd/sbin/proftpd
[root@asianux4 ~]# netstat -atnup|grep :21
[root@asianux4 ~]# mkdir /var/ftp
[root@asianux4 ~]# tar -czf /var/ftp/log.tar.gz /var/log


6、設置到達202.1.2.3主機的全部請求包,經過eth0轉發。
[root@asianux4 ~]# route add -host 202.1.2.3 dev eth0
[root@asianux4 ~]# route -n
9.16-課後練習答案

 

linux磁盤分區
1、關閉linux虛擬機,添加5塊4G的硬盤。
二、fdisk /dev/sdb
  /dev/sdb1   1G    83  linux
  /dev/sdb2   FREE 5  extended
  /dev/sdb5   2G    8e  lvm  (第一個邏輯分區,都是從5開始,15結束)
 /dev/sdb6   FREE  82  swap
3、partprobe 將最新的分區表寫入到內核。若是partprobe命令寫入不成功則須要reboot
四、mkfs.ext4 /dev/sdb1 建立文件系統
五、mkdir /mnt/sdb1;mount /dev/sdb1 /mnt/sdb1 建立掛載點,並將其掛載。
六、echo "/dev/sdb1    /mnt/sdb1    ext4    defaults    0 0" >>/etc/fstab

RAID磁盤陣列
分類:
1、軟件的RAID:經過軟件實現的RAID。mdadm軟件實現RAID,CPU
二、Server-RAID:服務器上的RAID卡。內存通常爲512M~4G。LSI廠家
3、RAID陣列:專業的磁盤陣列的RAID卡,有獨立的CPU和內存,具備一個精簡的操做系統。內存通常在4~96G不等。


RAID的工做原理:
初始化--RAID正常工做--硬盤出現故障時,RAID降級(2,1,0)--熱備盤自動頂上,從新校驗--校驗成功後,進入正常工做

RAID的級別:
RAID0
RAID1
RAID5
RAID6
RAID10
RAID01
RAID50
RAID60


RAID的建立:
實例:建立RAID5+1,將/dev/sdbc,sdd,sde作成RAID5,sdf作爲熱備盤。校驗位爲128K
[root@asianux4 ~]# mdadm -Cv /dev/md0 -l5 -n3 -x1 -c128 /dev/sd{c,d,e,f}
-C 建立RAID設備/dev/md0
-v 顯示建立的過程
-l raid級別,0、一、五、六、10
-n raid中硬盤的數量
-x 熱備盤的數量
-c 校驗位,默認是64K。


mdadm: layout defaults to left-symmetric
mdadm: layout defaults to left-symmetric
mdadm: size set to 4192128K
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[root@asianux4 ~]# cat /proc/mdstat    查看RAID的狀態,UU_:表示初始化
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sde[4] sdf[3](S) sdd[1] sdc[0]
      8384256 blocks super 1.2 level 5, 128k chunk, algorithm 2 [3/2] [UU_]
      [========>............]  recovery = 42.9% (1800064/4192128) finish=0.1min speed=200007K/sec

unused devices: <none>
[root@asianux4 ~]# cat /proc/mdstat    UUU:表示正常工做
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sde[4] sdf[3](S) sdd[1] sdc[0]
      8384256 blocks super 1.2 level 5, 128k chunk, algorithm 2 [3/3] [UUU]

unused devices: <none>
[root@asianux4 ~]# mkfs.ext4 /dev/md0    建立文件系統
mke2fs 1.41.12 (17-May-2010)
文件系統標籤=
操做系統:Linux
塊大小=4096 (log=2)
分塊大小=4096 (log=2)
Stride=32 blocks, Stripe width=64 blocks
524288 inodes, 2096064 blocks
104803 blocks (5.00%) reserved for the super user
第一個數據塊=0
Maximum filesystem blocks=2147483648
64 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

正在寫入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@asianux4 ~]# mkdir /mnt/raid5
[root@asianux4 ~]# mount /dev/md0  /mnt/raid5/
[root@asianux4 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_asianux4-lv_root
                      6.7G  4.0G  2.4G  64% /
tmpfs                 935M   76K  935M   1% /dev/shm
/dev/sda1             485M   39M  421M   9% /boot
/dev/sr0              3.8G  3.8G     0 100% /media
/dev/sdb1            1020M   34M  934M   4% /mnt/sdb1
/dev/md0              7.9G  146M  7.4G   2% /mnt/raid5
[root@asianux4 ~]#
[root@asianux4 ~]# mdadm -Ds    顯示RAID的配置信息
ARRAY /dev/md0 metadata=1.2 spares=1 name=asianux4:0 UUID=af7af545:26a6d60c:957a2c5b:03a1cc94
[root@asianux4 ~]# mdadm -Ds > /etc/mdadm.conf    將RAID的配置信息寫往以/etc/mdadm.conf文件中。
[root@asianux4 ~]#
[root@asianux4 ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sde[4] sdf[3](S) sdd[1] sdc[0]
      8384256 blocks super 1.2 level 5, 128k chunk, algorithm 2 [3/3] [UUU]

unused devices: <none>
[root@asianux4 ~]# mdadm /dev/md0 -f /dev/sde    模擬/dev/sde硬盤出錯
mdadm: set /dev/sde faulty in /dev/md0
[root@asianux4 ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sde[4](F) sdf[3] sdd[1] sdc[0]
      8384256 blocks super 1.2 level 5, 128k chunk, algorithm 2 [3/2] [UU_]
      [==>..................]  recovery = 10.9% (457216/4192128) finish=0.4min speed=152405K/sec

unused devices: <none>
[root@asianux4 ~]# cp /root/install.* /mnt/raid5/

[root@asianux4 ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sde[4](F) sdf[3] sdd[1] sdc[0]
      8384256 blocks super 1.2 level 5, 128k chunk, algorithm 2 [3/3] [UUU]

unused devices: <none>
[root@asianux4 ~]# mdadm /dev/md0 -r /dev/sde    移除故障盤
mdadm: hot removed /dev/sde from /dev/md0
[root@asianux4 ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdf[3] sdd[1] sdc[0]
      8384256 blocks super 1.2 level 5, 128k chunk, algorithm 2 [3/3] [UUU]

unused devices: <none>
[root@asianux4 ~]# mdadm /dev/md0 -a /dev/sde    添加熱備盤
mdadm: added /dev/sde
[root@asianux4 ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sde[4](S) sdf[3] sdd[1] sdc[0]
      8384256 blocks super 1.2 level 5, 128k chunk, algorithm 2 [3/3] [UUU]

unused devices: <none>
[root@asianux4 ~]# umount /dev/md0
[root@asianux4 ~]# mdadm -S /dev/md0        中止RAID設備
mdadm: stopped /dev/md0
[root@asianux4 ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
unused devices: <none>
[root@asianux4 ~]# mdadm -As /dev/md0        啓動RAID設備
mdadm: /dev/md0 has been started with 3 drives and 1 spare.
[root@asianux4 ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdc[0] sde[4](S) sdf[3] sdd[1]
      8384256 blocks super 1.2 level 5, 128k chunk, algorithm 2 [3/3] [UUU]

unused devices: <none>
[root@asianux4 ~]#


LVM邏輯卷管理
LVM的功能:
1、在線擴容和在線縮小
2、數據保護,快照
3、卷的複製和遷移。

卷:卷的大小能夠在線擴大和縮小。
分區:分區的大小不能夠變化。

LVM的組成:
物理卷:由一個或多個PE組成的PV。能夠是一個分區,邏輯卷,RAID,磁盤陣列的盤。
卷組:由一個或多個物理卷組成。
邏輯卷:在卷組上劃分多個邏輯卷。大小不能超過卷組的大小。

LVM的建立過程:
一、建立物理卷 pvcreate /dev/md0
二、建立卷組 vgcreate vg00 /dev/md0
三、在卷組上,建立邏輯卷 lvcreate -L 1G -n lv01 vg00
四、建立文件系統 mkfs.ext4 /dev/mapper/vg00-lv01
5、掛載使用


[root@asianux4 /]# umount /dev/md0    卸載上一個實驗中的/dev/md0掛載
[root@asianux4 /]# df -h |grep md0    並檢查,md0設備沒有被掛載。
[root@asianux4 /]# pvcreate /dev/md0    建立物理卷
  Physical volume "/dev/md0" successfully created
[root@asianux4 /]# pvs            顯示物理卷
  PV         VG          Fmt  Attr PSize PFree
  /dev/md0               lvm2 a--  8.00g 8.00g
  /dev/sda2  vg_asianux4 lvm2 a--  7.51g    0
[root@asianux4 /]# vgcreate vg00 /dev/md0    建立vg00卷組
  Volume group "vg00" successfully created
[root@asianux4 /]# pvs        顯示物理卷/dev/md0已經屬於vg00卷組。
  PV         VG          Fmt  Attr PSize PFree
  /dev/md0   vg00        lvm2 a--  7.99g 7.99g
  /dev/sda2  vg_asianux4 lvm2 a--  7.51g    0
[root@asianux4 /]# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  vg00          1   0   0 wz--n- 7.99g 7.99g
  vg_asianux4   1   2   0 wz--n- 7.51g    0
第一列:卷組名稱
第二列:卷組中物理卷的數量
第三列:邏輯卷的數量
第四列:快照的數量
第五列:屬性
第六列:卷組的大小
第七列:剩餘卷組的大小
[root@asianux4 /]# lvcreate -L 1G -n lv01 vg00    在vg00卷組上建立lv01邏輯卷,大小爲1G。
  Logical volume "lv01" created
[root@asianux4 /]# lvs
  LV      VG          Attr       LSize   Pool Origin Data%  Move Log Cpy%Sync Convert
  lv01    vg00        -wi-a-----   1.00g                                
  lv_root vg_asianux4 -wi-ao----   6.71g                                
  lv_swap vg_asianux4 -wi-ao---- 816.00m                                
[root@asianux4 /]# mkfs.ext4 /dev/mapper/vg00-lv01
mke2fs 1.41.12 (17-May-2010)
文件系統標籤=
操做系統:Linux
塊大小=4096 (log=2)
分塊大小=4096 (log=2)
Stride=32 blocks, Stripe width=64 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
第一個數據塊=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376

正在寫入inode表: 完成
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@asianux4 /]# mkdir /mnt/lv01
[root@asianux4 /]# mount /dev/mapper/vg00-lv01 /mnt/lv01/
[root@asianux4 /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_asianux4-lv_root
                      6.7G  4.0G  2.4G  64% /
tmpfs                 935M   76K  935M   1% /dev/shm
/dev/sda1             485M   39M  421M   9% /boot
/dev/sr0              3.8G  3.8G     0 100% /media
/dev/sdb1            1020M   34M  934M   4% /mnt/sdb1
/dev/mapper/vg00-lv01
                     1008M   34M  924M   4% /mnt/lv01
[root@asianux4 /]#

對LV作擴容:

[root@asianux4 /]# lvs    查看lv的大小
  LV      VG          Attr       LSize   Pool Origin Data%  Move Log Cpy%Sync Convert
  lv01    vg00        -wi-ao----   1.00g                                
  lv_root vg_asianux4 -wi-ao----   6.71g                                
  lv_swap vg_asianux4 -wi-ao---- 816.00m                                
[root@asianux4 /]# vgs    查看vg卷組的大小,剩餘空間是6.99G
  VG          #PV #LV #SN Attr   VSize VFree
  vg00          1   1   0 wz--n- 7.99g 6.99g
  vg_asianux4   1   2   0 wz--n- 7.51g    0
[root@asianux4 /]# lvextend -L 7.99g /dev/mapper/vg00-lv01    對lv01邏輯卷擴容。擴到7.99G。 或 lvextend -L +6.99G /dev/mapper/vg00-lv01
  Rounding size to boundary between physical extents: 7.99 GiB
  Extending logical volume lv01 to 7.99 GiB
  Logical volume lv01 successfully resized

[root@asianux4 /]# vgs        顯示vg卷組沒有剩餘空間了
  VG          #PV #LV #SN Attr   VSize VFree
  vg00          1   1   0 wz--n- 7.99g    0
  vg_asianux4   1   2   0 wz--n- 7.51g    0
[root@asianux4 /]# lvs        顯示邏輯卷lv01擴容成功.擴展到了7.99G
  LV      VG          Attr       LSize   Pool Origin Data%  Move Log Cpy%Sync Convert
  lv01    vg00        -wi-ao----   7.99g                                
  lv_root vg_asianux4 -wi-ao----   6.71g                                
  lv_swap vg_asianux4 -wi-ao---- 816.00m                                
[root@asianux4 /]# df -h    顯示掛載狀況,發現容量沒有擴展
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_asianux4-lv_root
                      6.7G  4.0G  2.4G  64% /
tmpfs                 935M   76K  935M   1% /dev/shm
/dev/sda1             485M   39M  421M   9% /boot
/dev/sr0              3.8G  3.8G     0 100% /media
/dev/sdb1            1020M   34M  934M   4% /mnt/sdb1
/dev/mapper/vg00-lv01
                     1008M   34M  924M   4% /mnt/lv01
[root@asianux4 /]# resize2fs /dev/mapper/vg00-lv01    將後臺的容量擴展到前臺
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg00-lv01 is mounted on /mnt/lv01; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/mapper/vg00-lv01 to 2095104 (4k) blocks.
The filesystem on /dev/mapper/vg00-lv01 is now 2095104 blocks long.

[root@asianux4 /]# df -h    發現容量擴展成功。
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_asianux4-lv_root
                      6.7G  4.0G  2.4G  64% /
tmpfs                 935M   76K  935M   1% /dev/shm
/dev/sda1             485M   39M  421M   9% /boot
/dev/sr0              3.8G  3.8G     0 100% /media
/dev/sdb1            1020M   34M  934M   4% /mnt/sdb1
/dev/mapper/vg00-lv01
                      7.9G   35M  7.5G   1% /mnt/lv01
[root@asianux4 /]#


對vg卷組作擴容。
將/dev/sdb5擴容到vg00卷組中。
[root@asianux4 /]# fdisk -l /dev/sdb

Disk /dev/sdb: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0e144a16

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
/dev/sdb2             133         522     3132675    5  Extended
/dev/sdb5             133         394     2104483+  8e  Linux LVM
/dev/sdb6             395         522     1028128+  82  Linux swap / Solaris
[root@asianux4 /]# pvcreate /dev/sdb5    建立/dev/sdb5物理卷
  Physical volume "/dev/sdb5" successfully created
[root@asianux4 /]# pvs
  PV         VG          Fmt  Attr PSize PFree
  /dev/md0   vg00        lvm2 a--  7.99g    0
  /dev/sda2  vg_asianux4 lvm2 a--  7.51g    0
  /dev/sdb5              lvm2 a--  2.01g 2.01g
[root@asianux4 /]# vgextend vg00 /dev/sdb5    將sdb5添加到vg00卷組中。對vg00作擴容。
  Volume group "vg00" successfully extended
[root@asianux4 /]# pvs
  PV         VG          Fmt  Attr PSize PFree
  /dev/md0   vg00        lvm2 a--  7.99g    0
  /dev/sda2  vg_asianux4 lvm2 a--  7.51g    0
  /dev/sdb5  vg00        lvm2 a--  2.00g 2.00g
[root@asianux4 /]# vgs        顯示擴容成功。
  VG          #PV #LV #SN Attr   VSize  VFree
  vg00          2   1   0 wz--n- 10.00g 2.00g
  vg_asianux4   1   2   0 wz--n-  7.51g    0
[root@asianux4 /]#

linux系統的故障分析:
1、引導類的故障:
1、密碼丟失:
grub    root    解決辦法
nok    ok    登陸系統,使用grub-md5-crypt命令從新設置一個密碼。
ok    nok    進入單用戶級別,使用passwd root更改密碼,再執行exit從新登陸系統。
進入單用戶級別的方法:
進入Grub---按e--找到kernel行(即第二行),再按e--在行尾輸入空格single|空格1|空格s,回車---按b引導--passwd 更改root用戶密碼。
nok    nok    使用光盤,進入救援模式,一路回車---chroot /mnt/sysimage---vim /boot/grub/grub.conf文件,將password行刪除。---重啓。


2、內核文件或映像文件寫錯。
關鍵點:找到linux系統啓動的四要素。
1、指定引導分區
2、指定內核位置和名稱
3、指定操做系統的根分區
4、指定內核映像文件

解決辦法:
進入grub--按c進入命令行--root (hd0,按tab健,找出引導分區。補全,回車---kernel /vmlinuz按tab健自動補全 ro root=/dev/mapper/vg_asianux4-lv_root 回車---initrd /initramfs-按tab健自動補全,回車---boot引導系統,回車---系統正常進入。

進入系統,修改/boot/grub/grub.conf文件 
[root@asianux4 ~]# vim /boot/grub/grub.conf
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Asianux Server (2.6.32-431.20.3.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-431.20.3.el6.x86_64 ro root=/dev/mapper/vg_asianux4-lv_root rd_NO_LUKS rd_LVM_LV=vg_asianux4/lv_swap rd_LVM_LV=vg_asianux4/lv_root rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /initramfs-2.6.32-431.20.3.el6.x86_64.img
[root@asianux4 ~]#



二、內核文件或映像文件丟失、錯誤。
9.17-1
3、內核文件或映像文件丟失、錯誤。
模擬故障:
[root@asianux4 ~]# cd /boot/
[root@asianux4 boot]# ls
config-2.6.32-431.20.3.el6.x86_64
efi
grub
initramfs-2.6.32-431.20.3.el6.x86_64.img
initrd-2.6.32-431.20.3.el6.x86_64kdump.img
lost+found
symvers-2.6.32-431.20.3.el6.x86_64.gz
System.map-2.6.32-431.20.3.el6.x86_64
vmlinuz-2.6.32-431.20.3.el6.x86_64
[root@asianux4 boot]# rm vmlinuz-2.6.32-431.20.3.el6.x86_64 initramfs-2.6.32-431.20.3.el6.x86_64.img -rf

解決辦法:
一、從其它linux系統上,複製一個內核和內核映像文件到/boot目錄便可。
2、進入救援模式,掛載光盤,再按一個kernel內核。
chroot /mnt/sysimage
mount /dev/cdrom /media
cd /media/Packages/
rpm -ivh kernel-2.6.32-*.rpm --force    從新安裝內核,便可找回內核文件和映像文件
exit
reboot

dd if=/dev/sda of=/dev/sdb
dd if=/dev/sda1 of=/dev/sdb5
dd if=/dev/sda1 of=/backup/boot_bak.dd
dd if=/backup/boot_bak.dd of=/dev/sda1


4、MBR被破壞
備份/dev/sda1引導分區。
[root@asianux4 boot]# dd if=/dev/sda1 of=/boot_bak.dd
記錄了1024000+0 的讀入
記錄了1024000+0 的寫出
524288000字節(524 MB)已複製,13.1391 秒,39.9 MB/秒
[root@asianux4 boot]# ll /boot_bak.dd -h
-rw-r--r-- 1 root root 500M 9月  17 14:39 /boot_bak.dd

備份MBR:
[root@asianux4 boot]# dd if=/dev/sda of=/mbr_bak.dd bs=512 count=1

模擬故障:
[root@asianux4 boot]# dd if=/dev/zero of=/dev/sda bs=50 count=1
記錄了1+0 的讀入
記錄了1+0 的寫出
50字節(50 B)已複製,0.000153545 秒,326 kB/秒
[root@asianux4 boot]# reboot

解決辦法:
使用光盤,進入救援模式,從新安裝grub,恢復mbr主引導記錄。
chroot /mnt/sysimge
grub-install /dev/sda    從新安裝Grub,恢復mbr記錄。
exit
reboot

五、備份/dev/sda1分區,並恢復。
[root@asianux4 backup]# dd if=/dev/sda1 of=/backup/boot_bak.dd    備份sda1分區
[root@asianux4 backup]# ls
boot_bak.dd           mbr_bak.dd
log-20150914.tar.bz2  sysinit_config_file_bak.tar.gz
[root@asianux4 backup]# rm /boot/* -rf    刪除/boot分區,/dev/sda1分區的數據。
[root@asianux4 backup]# ls /boot/
[root@asianux4 backup]# umount /dev/sda1    卸載/dev/sda1
[root@asianux4 backup]# dd if=/backup/boot_bak.dd of=/dev/sda1    恢復sda1分區
記錄了1024000+0 的讀入
記錄了1024000+0 的寫出
524288000字節(524 MB)已複製,13.1073 秒,40.0 MB/秒
[root@asianux4 backup]# mount /dev/sda1 /boot/    掛載/dev/sda1,發現恢復成功
[root@asianux4 backup]# ls /boot/
config-2.6.32-431.20.3.el6.x86_64
efi
grub
initramfs-2.6.32-431.20.3.el6.x86_64.img
initrd-2.6.32-431.20.3.el6.x86_64kdump.img
lost+found
symvers-2.6.32-431.20.3.el6.x86_64.gz
System.map-2.6.32-431.20.3.el6.x86_64
vmlinuz-2.6.32-431.20.3.el6.x86_64
[root@asianux4 backup]#

六、/etc/rc.d/rc.sysinit,/etc/inittab,/etc/init/*,文件被誤刪。
解決辦法:
方法一:
進入光盤的救援模式,從新安裝initscripts-9.03.40-2.AXS4.3.0.1.x86_64軟件包,便可。
chroot /mnt/sysimage
mount /dev/cdrom /media/
cd /media/Packages
rpm -ivh initscripts-9.03.40-2.AXS4.3.0.1.x86_64.rpm --force

方法二:
經過備份文件恢復。(誤刪除了rc.sysinit文件)
tar -czf /backup/sysinit_config_file_bak.tar.gz /etc/inittab /etc/rc.d/rc /etc/rc.d/rc.sysinit /etc/init/* /etc/fstab /etc/init.d/* /etc/rc.d/rc.local /etc/profile /etc/bashrc  (備份系統初始化文件)

進入光盤的救援模式,恢復被刪除的文件
chroot /mnt/sysimge
tar -zxf /backup/sysinit_config_file_bak.tar.gz -C /tmp
cd /tmp/etc/rc.d/rc.sysinit /etc/rc.d/rc.sysinit


七、/etc/fstab文件錯誤
模擬故障:
[root@asianux4 rc.d]# vim /etc/fstab 將/分區和/boot破壞。
/dev/mapper/vg_asianux4-(被刪除部分)     /    ext4    defaults        1 1
UUID=(被刪除部分)     /boot  ext4    defaults        1 2
解決辦法:
看到control+d提示符時,輸入root用戶的密碼。進入shell界面。
mount -o remount,rw /    從新掛載根分區,以讀寫的方式掛載。
修改/etc/fstab文件,恢復到正常狀態。
reboot


文件系統類故障
1、沒法刪除文件
文件寫鎖。
文件是以只讀的方式掛載
添加了文件i屬性,給文件加鎖了。
文件被掛載
用戶無權限刪除
[root@asianux4 ~]# lsattr adduser.sh    查看adduser.sh文件的屬性,ACL
-------------e- adduser.sh
[root@asianux4 ~]# chattr +i adduser.sh    添加+i屬性,防刪除屬性
[root@asianux4 ~]# lsattr adduser.sh    
----i--------e- adduser.sh
[root@asianux4 ~]# rm adduser.sh -rf
rm: 沒法刪除"adduser.sh": 不容許的操做
[root@asianux4 ~]# chattr -i adduser.sh    去掉-i屬性
[root@asianux4 ~]# lsattr adduser.sh
-------------e- adduser.sh


2、文件被誤刪除,須要恢復。
ext2/ext3/ext4。採用debugfs工具進行恢復。

3、X window沒法打開。
緣由:
在級別3運行。
x window,desktop軟件包沒有安裝包。
解決辦法:
從新安裝桌面。

日誌管理:
日誌的功能:
審計、監測、跟蹤、排錯。

日誌的類型:
1、基於時間的日誌  
2、基於rsyslog日誌服務的日誌
3、基於服務的日誌,oracle,weblogic,apache,jboss,
4、基於進程的日誌,作跟蹤時,作一個進程的日誌


基於時間的日誌
命令:last,lastb,lastlog,w,who,finger,ac
文件:/var/log/wtmp  /var/run/utmp  /var/log/lastlog  /var/log/btmp

[root@asianux4 ~]# ll /var/log/wtmp
-rw-rw-r--. 1 root utmp 160512 9月  18 00:08 /var/log/wtmp
[root@asianux4 ~]# ll /var/run/utmp
-rw-rw-r-- 1 root utmp 3456 9月  18 00:08 /var/run/utmp
[root@asianux4 ~]# ll /var/log/lastlog
-rw-r--r--. 1 root root 292292 9月  18 00:08 /var/log/lastlog
[root@asianux4 ~]# ll /var/log/btmp
-rw-------. 1 root utmp 5760 9月  16 21:11 /var/log/btmp
[root@asianux4 ~]#

[root@asianux4 ~]# w    查看哪些用戶鏈接到了本機
 00:29:00 up 37 min,  1 user,  load average: 0.05, 0.02, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.232.1    00:08    0.00s  0.18s  0.00s w

[root@asianux4 ~]# who    查看哪些用戶鏈接到了本機
root     pts/0        2015-09-18 00:08 (192.168.232.1)
[root@asianux4 ~]#

[root@asianux4 ~]# ac    用戶登陸到本機停留的時間
        total      118.72
[root@asianux4 ~]# ac -p
        user10                              13.92
        hsx123abd                            8.44
        root                                95.35
        ftp                                  1.00
        total      118.72
[root@asianux4 ~]#

[root@asianux4 ~]# last    查看用戶登入、登出、重啓的狀態。
root     pts/0        192.168.232.1    Fri Sep 18 00:08   still logged in
reboot   system boot  2.6.32-431.20.3. Thu Sep 17 23:52 - 00:33  (00:41)
reboot   system boot  2.6.32-431.20.3. Thu Sep 17 23:51 - 23:51  (00:00)
root     pts/0        192.168.232.1    Thu Sep 17 23:23 - down   (00:20)
reboot   system boot  2.6.32-431.20.3. Thu Sep 17 23:02 - 23:43  (00:41)
root     pts/0        192.168.232.1    Thu Sep 17 14:49 - down   (00:09)
reboot   system boot  2.6.32-431.20.3. Thu Sep 17 14:48 - 14:58  (00:10)
root     pts/0        192.168.232.1    Thu Sep 17 14:43 - down   (00:00)
reboot   system boot  2.6.32-431.20.3. Thu Sep 17 14:41 - 14:44  (00:02)
root     pts/0        192.168.232.1    Thu Sep 17 14:36 - down   (00:04)


[root@asianux4 ~]# lastb    登陸失敗的信息
         tty1                          Wed Sep 16 21:11 - 21:11  (00:00)
hsx123ab ssh:notty    192.168.232.128  Wed Sep 16 15:15 - 15:15  (00:00)
hsx123ab ssh:notty    192.168.232.128  Wed Sep 16 15:15 - 15:15  (00:00)
hsx123ab ssh:notty    192.168.232.128  Wed Sep 16 15:15 - 15:15  (00:00)
hsx123ab ssh:notty    asianux4         Wed Sep 16 15:04 - 15:04  (00:00)
hsx123ab ssh:notty    asianux4         Wed Sep 16 15:03 - 15:03  (00:00)
hsx123ab ssh:notty    asianux4         Wed Sep 16 15:02 - 15:02  (00:00)
hsx123ab ssh:notty    asianux4         Wed Sep 16 15:02 - 15:02  (00:00)

[root@asianux4 ~]# lastlog    最後登陸的時間
用戶名           端口     來自             最後登錄時間
root             pts/0    192.168.232.1    五 9月 18 00:08:39 +0800 2015
bin                                        **從未登陸過**
daemon                                     **從未登陸過**
adm                                        **從未登陸過**
lp                                         **從未登陸過**
sync                                       **從未登陸過**
shutdown                                   **從未登陸過**

基於進程的日誌
[root@asianux4 ~]# accton /var/account/pacct
[root@asianux4 ~]# ls
1.tar.gz         date.txt            install.sh  proftpd-1.3.5.tar.gz
adduser.sh       install.log         iostat.txt  test
anaconda-ks.cfg  install.log.syslog  log.tar.gz  webmin-1.680.tar.gz
[root@asianux4 ~]# lastcomm
ls                      root     pts/0      0.00 secs Fri Sep 18 00:42
accton            S     root     pts/0      0.00 secs Fri Sep 18 00:42
[root@asianux4 ~]# date
2015年 09月 18日 星期五 00:42:30 CST
[root@asianux4 ~]# lastcomm
date                    root     pts/0      0.00 secs Fri Sep 18 00:42
lastcomm                root     pts/0      0.00 secs Fri Sep 18 00:42
ls                      root     pts/0      0.00 secs Fri Sep 18 00:42
accton            S     root     pts/0      0.00 secs Fri Sep 18 00:42
[root@asianux4 ~]# accton
[root@asianux4 ~]# lastcomm
lastcomm                root     pts/0      0.00 secs Fri Sep 18 00:42
date                    root     pts/0      0.00 secs Fri Sep 18 00:42
lastcomm                root     pts/0      0.00 secs Fri Sep 18 00:42
ls                      root     pts/0      0.00 secs Fri Sep 18 00:42
accton            S     root     pts/0      0.00 secs Fri Sep 18 00:42


基於rsyslog日誌服務的日誌
在不一樣的LINUX系統,實現的軟件略有不一樣。
syslog,rsyslog,syslog-ng,用於實現系統日誌的管理。

[root@asianux4 ~]# rpm -qa |grep syslog
rsyslog-5.8.10-8.AXS4.x86_64

rsyslog日誌服務的配置文件
/etc/rsyslog.conf    rsyslog日誌的主配置文件
/etc/rsyslog.d/*.conf    rsyslog日誌的輔助配置文件
/var/log/        日誌文件的目錄


  8 $ModLoad imuxsock     加載socket的日誌
  9 $ModLoad imklog     加載log的日誌
 13 $ModLoad imudp    加載遠程日誌服務器的UDP協議
 14 $UDPServerRun 514    遠程日誌服務器的端口,默認爲514
45 authpriv.*   /var/log/secure    定義策略,

第一列:日誌的類型和錯誤級別。
日誌的類型
auth,authpriv,cron,daemon,kern,lpr,mail,mark,news,security (same as auth),syslog,user,uucp,local0~local7,*

auth|authpriv|security 用戶認證類的日誌
cron    計劃任務的日誌
daemon    守護進程的日誌
kern    內核類型的日誌
lpr    打印機
mail    郵件服務器
mark    時間戳
news    新聞組
syslog    日誌服務
user    用戶
uucp    unix to unix copy
local0~local7 用戶自定義


日誌的錯誤級別:(由低到高)
debug,info,notice,warning|warn,err|error,crit,alert,emerg|panic,*

第二列:日誌的存放位置或文件
1、文件
2、設備
3、遠程的日誌服務器
4、用戶

實例:
將全部日誌類型的全部日誌寫入/dev/tty1終端上。
[root@asianux4 ~]# vim /etc/rsyslog.conf 在文件末尾添加如下內容。
*.*        /dev/tty1
[root@asianux4 ~]# service rsyslog restart重啓日誌服務
關閉系統日誌記錄器:                                       [肯定]
啓動系統日誌記錄器:                                       [肯定]

在ssh終端上,執行如下命令,在tty1終端看,查看日誌。
[root@asianux4 ~]# su -
[root@asianux4 ~]# exit
logout
[root@asianux4 ~]# useradd user111
[root@asianux4 ~]# userdel user111
[root@asianux4 ~]#


卸載LVM和RAID。
[root@asianux4 ~]# umount /dev/mapper/vg00-lv01    卸載lv01邏輯卷
[root@asianux4 ~]# df -h|grep vg00
[root@asianux4 ~]# lvs
  LV      VG          Attr       LSize   Pool Origin Data%  Move Log Cpy%Sync Convert
  lv01    vg00        -wi-a-----   7.99g                                
  lv_root vg_asianux4 -wi-ao----   6.71g                                
  lv_swap vg_asianux4 -wi-ao---- 816.00m                                
[root@asianux4 ~]# lvremove /dev/vg00/lv01    刪除lv邏輯卷
Do you really want to remove active logical volume lv01? [y/n]: y
  Logical volume "lv01" successfully removed    
[root@asianux4 ~]# vgremove vg00        刪除vg00卷組
  Volume group "vg00" successfully removed
[root@asianux4 ~]# pvremove /dev/md0        刪除/dev/md0物理卷
  Labels on physical volume "/dev/md0" successfully wiped
[root@asianux4 ~]# pvs
  PV         VG          Fmt  Attr PSize PFree
  /dev/sda2  vg_asianux4 lvm2 a--  7.51g    0
  /dev/sdb5              lvm2 a--  2.01g 2.01g
[root@asianux4 ~]# pvremove /dev/sdb5        刪除/dev/sdb5物理卷
  Labels on physical volume "/dev/sdb5" successfully wiped
[root@asianux4 ~]# mdadm -S /dev/md0        中止RAID
mdadm: stopped /dev/md0
[root@asianux4 ~]# rm /etc/mdadm.conf -rf    刪除RAID配置
[root@asianux4 ~]#
9.17-2
課後練習
1、在VMware workstation中添加sdc,sdd,sde,sdf虛擬硬盤,將/dev/sdc,sdd,sde,sdf作成RAID5,sdf作爲熱備盤。校驗位爲128K,建立raid設備名稱爲/dev/md0。再將/dev/md0設備添加到vg00卷組上,並在vg00卷組上建立lv01和lv02兩個邏輯卷。設置lv01的大小爲1G,文件系統爲ext4,設置開機掛載到/mnt/lv01目錄;lv02大小爲2G,文件系統爲ext3,設置開機掛載到/mnt/lv02目錄上。

2、在線對lv01邏輯卷擴容,擴容到4G。同時將/dev/sdb5擴容到vg00組中。

3、忘記root密碼和grub密碼時,須要如何破解。

4、備份/boot分區和mbr主引導記錄信息,執行rm /boot/* -rf命令後重啓,並恢復故障。

5、將ssh服務器的debug及以上的錯誤日誌信息,寫入到/var/log/sshd.log文件。
9.17-課後練習
課後練習
1、在VMware workstation中添加sdc,sdd,sde,sdf虛擬硬盤,將/dev/sdc,sdd,sde,sdf作成RAID5,sdf作爲熱備盤。校驗位爲128K,建立raid設備名稱爲/dev/md0。再將/dev/md0設備添加到vg00卷組上,並在vg00卷組上建立lv01和lv02兩個邏輯卷。設置lv01的大小爲1G,文件系統爲ext4,設置開機掛載到/mnt/lv01目錄;lv02大小爲2G,文件系統爲ext3,設置開機掛載到/mnt/lv02目錄上。
[root@asianux4 upload]# mdadm -Ds > /etc/mdadm.conf
[root@asianux4 upload]# pvcreate /dev/md0
  Physical volume "/dev/md0" successfully created
[root@asianux4 upload]# vgcreate vg00 /dev/md0
  Volume group "vg00" successfully created
[root@asianux4 upload]# lvcreate -L 1g -n lv01 vg00
  Logical volume "lv01" created
[root@asianux4 upload]# lvcreate -L 2g -n lv02 vg00
  Logical volume "lv02" created
[root@asianux4 upload]# lvs
  LV      VG          Attr       LSize   Pool Origin Data%  Move Log Cpy%Sync Convert
  lv01    vg00        -wi-a-----   1.00g                                
  lv02    vg00        -wi-a-----   2.00g                                
  lv_root vg_asianux4 -wi-ao----   6.71g                                
  lv_swap vg_asianux4 -wi-ao---- 816.00m                                
[root@asianux4 upload]# mkdir /mnt/lv{01,02}
[root@asianux4 mapper]# mkfs.ext4 /dev/vg00/lv01
mke2fs 1.41.12 (17-May-2010)
文件系統標籤=
操做系統:Linux
塊大小=4096 (log=2)
分塊大小=4096 (log=2)
Stride=32 blocks, Stripe width=64 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
第一個數據塊=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376

正在寫入inode表: 完成
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@asianux4 mapper]# mkfs.ext3 /dev/vg00/lv02
mke2fs 1.41.12 (17-May-2010)
文件系統標籤=
操做系統:Linux
塊大小=4096 (log=2)
分塊大小=4096 (log=2)
Stride=32 blocks, Stripe width=64 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
第一個數據塊=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

正在寫入inode表: 完成
Creating journal (16384 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@asianux4 mapper]# mount /dev/vg00/lv01 /mnt/lv01/
[root@asianux4 mapper]# mount /dev/vg00/lv02 /mnt/lv02

[root@asianux4 mapper]# vim /etc/fstab
[root@asianux4 mapper]# tail -2 /etc/fstab
/dev/vg00/lv01          /mnt/lv01               ext4    defaults       0 0
/dev/vg00/lv02          /mnt/lv02               ext3    defaults       0 0
[root@asianux4 mapper]#

2、在線對lv01邏輯卷擴容,擴容到4G。同時將/dev/sdb5擴容到vg00組中。
[root@asianux4 mapper]# lvs
  LV      VG          Attr       LSize   Pool Origin Data%  Move Log Cpy%Sync Convert
  lv01    vg00        -wi-ao----   1.00g                                
  lv02    vg00        -wi-ao----   2.00g                                
  lv_root vg_asianux4 -wi-ao----   6.71g                                
  lv_swap vg_asianux4 -wi-ao---- 816.00m                                
[root@asianux4 mapper]# lvextend -L 4g /dev/vg00/lv01
  Extending logical volume lv01 to 4.00 GiB
  Logical volume lv01 successfully resized

[root@asianux4 mapper]# resize2fs /dev/vg00/lv01
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg00/lv01 is mounted on /mnt/lv01; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg00/lv01 to 1048576 (4k) blocks.
The filesystem on /dev/vg00/lv01 is now 1048576 blocks long.

[root@asianux4 mapper]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_asianux4-lv_root
                      6.7G  4.5G  1.9G  72% /
tmpfs                 935M     0  935M   0% /dev/shm
/dev/sda1             485M   39M  421M   9% /boot
/dev/sr0              3.8G  3.8G     0 100% /media
/dev/sdb1            1020M   34M  934M   4% /mnt/sdb1
/dev/mapper/vg00-lv01
                      4.0G   34M  3.8G   1% /mnt/lv01
/dev/mapper/vg00-lv02
                      2.0G   68M  1.9G   4% /mnt/lv02
[root@asianux4 mapper]# vgs
  VG          #PV #LV #SN Attr   VSize VFree
  vg00          1   2   0 wz--n- 7.99g 1.99g
  vg_asianux4   1   2   0 wz--n- 7.51g    0
[root@asianux4 mapper]# pvcreate /dev/sdb5
  Physical volume "/dev/sdb5" successfully created

[root@asianux4 mapper]# vgextend vg00 /dev/sdb5
  Volume group "vg00" successfully extended
[root@asianux4 mapper]# vgs
  VG          #PV #LV #SN Attr   VSize  VFree
  vg00          2   2   0 wz--n- 10.00g 4.00g
  vg_asianux4   1   2   0 wz--n-  7.51g    0
[root@asianux4 mapper]#

3、忘記root密碼和grub密碼時,須要如何破解。
答:進入救援模式
chroot /mnt/sysimage
vim /boot/grub/grub.conf 將password行刪除
passwd root 更改root密碼
exit
reboot

4、備份/boot分區和mbr主引導記錄信息,執行rm /boot/* -rf命令後重啓,並恢復故障。
dd if=/dev/sda1 of=/boot_bak.dd (備份/boot分區)
dd if=/dev/sda of=/mbr_bak.dd bs=512 count=1(備份mbr主引導記錄)
rm /boot/* -rf

進入救援模式
chroot /mnt/sysimage
dd if=/boot_bak.dd of=/dev/sda1
exit
reboot


5、將ssh服務器的debug及以上的錯誤日誌信息,寫入到/var/log/sshd.log文件。

[root@asianux4 mapper]# vim /etc/ssh/sshd_config
 36 SyslogFacility AUTHPRIV
 37 LogLevel DEBUG

[root@asianux4 mapper]# vim /etc/rsyslog.conf
 51 authpriv.debug                          /var/log/sshd.log

[root@asianux4 mapper]# service sshd restart
[root@asianux4 mapper]# service rsyslog restart

[root@asianux4 mapper]# tail -f /var/log/sshd.log
再打開一個終端,登陸linux系統,會發現上面的終端有不少日誌顯示。
9.17-課後練習答案

 

日誌管理:
1、基於服務的
2、基於時間的
 /var/log/wtmp,/var/run/utmp,/var/log/lastlog(lastlog),/var/log/btmp(lastb)
3、基於進程的
  accton /var/account/pacct;    打開進程監控
  lastcomm    顯示
  accton    關閉

四、基於rsyslog日誌,軟件:syslog,rsyslog,syslog-ng
syslog:/etc/syslog.conf
rsyslog:/etc/rsyslog.conf,/etc/rsyslog.d/*.conf
syslog-ng:/etc/syslog-ng.conf

[root@asianux4 ~]# rpm -qa |grep syslog
rsyslog-5.8.10-8.AXS4.x86_64

kern.*                                                 /dev/console
內核的全部錯誤日誌,都會發送給/dev/console控制檯。

*.info;mail.none;authpriv.none;cron.none               /var/log/messages
除了mail,authpriv,cron這三類日誌不發送到/var/log/messages文件外,其它的的有消息錯誤級爲info的都會發送到/var/log/messages.

authpriv.*                                             /var/log/secure
用戶認證的消息,全部錯誤級別都發送到/var/log/secure

mail.*                                                 -/var/log/maillog
郵件服務器的消息,全部錯誤級別都發送到/var/log/maillog日誌

在終端一執行如下命令
[root@asianux4 ~]# mail user1 user2
Subject: test mail
alsdjfalsdfjalsdfjklasdf
.
EOT

在終端二執行如下命令
[root@asianux4 ~]# tail -f /var/log/maillog
如下爲顯示的結果。
Sep 18 16:53:14 asianux4 postfix/pickup[5394]: 0701BA97A: uid=0 from=<root>
Sep 18 16:53:14 asianux4 postfix/cleanup[5579]: 0701BA97A: message-id=<20150918085314.0701BA97A@asianux4.localdomain>
Sep 18 16:53:14 asianux4 postfix/qmgr[1773]: 0701BA97A: from=<root@asianux4.localdomain>, size=499, nrcpt=2 (queue active)
Sep 18 16:53:14 asianux4 postfix/local[5581]: 0701BA97A: to=<user1@asianux4.localdomain>, orig_to=<user1>, relay=local, delay=0.17, delays=0.09/0.07/0/0, dsn=2.0.0, status=sent (delivered to mailbox)
Sep 18 16:53:14 asianux4 postfix/local[5581]: 0701BA97A: to=<user2@asianux4.localdomain>, orig_to=<user2>, relay=local, delay=0.18, delays=0.09/0.07/0/0.01, dsn=2.0.0, status=sent (delivered to mailbox)
Sep 18 16:53:14 asianux4 postfix/qmgr[1773]: 0701BA97A: removed
表示root用戶發送了一封郵件給user1和user2,而且發送成功。

*.*                                     /dev/tty1
將全部消息的全部錯誤級別的日誌發送給/dev/tty1

authpriv.debug                          /var/log/sshd.log
將安全認證相關的debug錯誤日誌,發送給/var/log/sshd.log文件


cron.*                                                  /var/log/cron
將crond計劃任務的全部錯誤日誌發送給/var/log/cron文件

uucp,news.crit                                          /var/log/spooler
將uucp和news消息類型,錯誤級別爲crit的發送到/var/log/spooler

local7.*                                                /var/log/boot.log
記錄linux引導的過程,並將日誌寫到/var/log/boot.log文件中。


實例:
將全部linux系統的日誌,發送到遠程的日誌服務器上,遠程的日誌服務器IP地址爲192.168.232.100.

在日誌客戶端上的配置:
[root@asianux4 ~]# vim /etc/rsyslog.conf
*.*                  @192.168.232.100:514(採用514/udp協議),若是@@192.168.232.100:514表示採用514/tcp協議,則日誌服務器也須要開啓514/tcp端口。

[root@asianux4 ~]# service rsyslog restart

在日誌服務器上的配置:
[root@logserver ~]# vim /etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514


[root@logserver ~]# service rsyslog restart

logserver ip: 10.6.65.180/24

補充網卡eth0,eth1沒有顯示,真實網卡顯示成eth2,eth3的故障。
解決辦法:
[root@asianux4 ~]# rm /etc/sysconfig/network-scripts/ifcfg-eth* -rf
[root@asianux4 ~]# rm /etc/udev/rules.d/70-persistent-net.rules -rf
[root@asianux4 ~]# reboot
[root@asianux4 ~]# setup    從新設置IP地址。


日誌的轉儲:
linux系統日誌的轉儲軟件是logrotate.只是一個命令,沒有以服務的方式呈現。
相關的配置文件:
[root@asianux4 log]# ll /etc/logrotate.conf    主配置文件
[root@asianux4 log]# ll /etc/logrotate.d/*    輔助配置文件
[root@asianux4 log]# ll /etc/cron.daily/logrotate天天轉儲的腳本。

[root@asianux4 log]# vim /etc/logrotate.conf
  3 weekly    每週轉儲,daily,monthly,hourly
  6 rotate 4    轉儲4次
  9 create 644 root root    轉儲後,建立日誌文件,定義權限、用戶、工做組。
 12 dateext    轉儲時在文件末尾添加日期。 messages-20150914
 15 compress    轉儲後壓縮    messages-20150914.gz
 18 include /etc/logrotate.d    包含其它的配置文件

 21 /var/log/wtmp {    爲wtmp日誌定義轉儲策略。
 22     monthly        每個月轉儲
 23     create 0664 root utmp    轉儲後,建立wtmp文件,權限爲664,用戶爲root,工做組爲utmp
 24     minsize 1M    wtmp至少達到1M時,才轉儲。若是wtmp沒有到1M,就算超過了一個月也不會轉儲,
 25     rotate 1    轉儲一次
 26 }
 27
 28 /var/log/btmp {    爲btmp日誌文件定義轉儲策略
 29     missingok    文件是否爲空都會轉儲
 30     monthly        每個月轉儲
 31     create 0600 root utmp
 32     rotate 1
 33 }



[root@asianux4 log]# cd /etc/logrotate.d/
[root@asianux4 logrotate.d]# ls
cups    httpd  ppp     sssd    tomcat6  vsftpd          yum
dracut  numad  psacct  syslog  up2date  wpa_supplicant
[root@asianux4 logrotate.d]# cat syslog    定義轉儲策略爲默認策略,轉儲後,重啓rsyslog日誌服務。

/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
    sharedscripts
    postrotate
        /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}
[root@asianux4 logrotate.d]#


實例:
將全部認證消息authpriv,錯誤級別爲debug級別的,發送到/var/log/sshd.log文件,設置天天轉儲,轉儲365次,轉儲時壓縮,大小到達1M時自動轉儲。

[root@asianux4 logrotate.d]# vim /etc/rsyslog.conf 在文件末尾添加如下內容。
authpriv.debug        /var/log/sshd.log
[root@asianux4 logrotate.d]# service rsyslog restart

[root@asianux4 logrotate.d]# vim /etc/logrotate.d/sshd.log
/var/log/sshd.log {
    daily
    rotate 365
    compress
    minsize 1M
}
[root@asianux4 logrotate.d]# /etc/cron.daily/logrotate


iptables防火牆的配置
linux系統的防火牆,默認狀況下打開。
防火牆上默認有四個表,每個表上擁有不一樣的鏈,在每個鏈上都不一樣的規則。

在默認的filter表中,有三條鏈,分別是輸入鏈INPUT,輸出鏈OUTPUT,轉發鏈FORWARD.在每個鏈上,都有不一樣的規則。

實現防火牆的軟件:
iptables    作ipv4/ipv6防火牆策略
ip6tables    專業的ipv6防火牆
arptables    mac地址的防火牆。

[root@asianux4 ftp]# rpm -qa |grep iptables
iptables-ipv6-1.4.7-11.AXS4.x86_64
iptables-1.4.7-11.AXS4.x86_64
[root@asianux4 ftp]#

iptables的相關配置文件
/etc/sysconfig/iptables-config    防火牆的主配置文件
/etc/sysconfig/iptables        iptables防火牆的規則保存文件
/etc/sysconfig/iptables.old    iptables防火牆的初版的規則保存文件

[root@asianux4 sysconfig]# iptables-save    保存iptables的規則
[root@asianux4 sysconfig]# iptables-restore < /etc/sysconfig/iptables 恢復規則
[root@asianux4 sysconfig]# iptables-save > /etc/sysconfig/iptables 保存iptables規則


[root@asianux4 sysconfig]# vim /etc/sysconfig/iptables-config
 19 IPTABLES_SAVE_ON_STOP="no"    在關閉iptabbles服務時,是否自動保存
 25 IPTABLES_SAVE_ON_RESTART="no"    在重啓iptabbles服務時,是否自動保存

防火牆的配置
方法一:使用iptables命令配置防火牆
方法二:使用setup命令配置防火牆。
方法三:直接將shell腳本,配置防火牆。


方法一:使用iptables命令配置防火牆
[root@asianux4 sysconfig]# iptables-restore < /etc/sysconfig/iptables
[root@asianux4 sysconfig]# iptables -I INPUT 2 -s 10.6.65.0/24 -p tcp --dport 21 -j ACCEPT
在INPUT鏈上的第2條上插入規則,容許10.6.65.0/24網段的全部主機訪問本要的21端口。

[root@asianux4 sysconfig]# iptables -nL    顯示iptables規則
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
ACCEPT     tcp  --  10.6.65.0/24         0.0.0.0/0           tcp dpt:21
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited    拒絕全部。

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination



方法二:使用setup命令配置防火牆。
[root@asianux4 sysconfig]# setup
[root@asianux4 sysconfig]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:80
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:21
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
[root@asianux4 sysconfig]#

[root@asianux4 sysconfig]# iptables-save    保存規則。

實例:
安裝vsftpd、httpd軟件包。啓動vsftpd和httpd服務器。設置iptables防火牆。容許用戶訪問ftp,拒絕用戶訪問www服務。

[root@asianux4 sysconfig]# mount /dev/cdrom /media
[root@asianux4 sysconfig]# rpm -qa |grep vsftpd
[root@asianux4 sysconfig]# rpm -qa |grep httpd
httpd-2.2.15-31.0.1.AXS4.x86_64
httpd-tools-2.2.15-31.0.1.AXS4.x86_64

[root@asianux4 sysconfig]# yum install vsftpd -y
[root@asianux4 sysconfig]# service vsftpd start
[root@asianux4 sysconfig]# service httpd start
[root@asianux4 sysconfig]# setup (在ftp上打*號)
在windows上測試。發現ftp能夠訪問。http沒法訪問。
9.18-1
linux的性能優化:
1、CPU,MEM
2、DISK--RAID
3、網絡相關的外設,網卡

linux系統性能分析:
top:linux系統的負載,CPU,MEM,SWAP,佔用CPU和內存比較的進程,殺死佔用性能高的進程。
[root@asianux4 ~]# top
top - 22:45:24 up 22:53,  5 users,  load average: 0.00, 0.00, 0.00
當前的時間,開機時間爲22小時53分鐘,5個用戶在線,linux系統的負載(CPU核數*1),最近1分鐘,最近5分鐘,最近15分鐘。

Tasks: 151 total,   1 running, 150 sleeping,   0 stopped,   0 zombie
系統已打開的進程總數爲151個,1個正在運行,150休眠,0箇中止,0個阻塞。

Cpu0  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu1  :  0.0%us,  0.3%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu2  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Cpu3  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
按1(123的1)顯示CPU的全部核。
id,wa: 
id=100%,wa=0% 表示系統負載很是低。
id=0%,wa=100% 表示系統我載很是高。 
id=100%,wa=100% 表示CPU壓力低,磁盤、網絡可能壓力高,可能死鎖。
id=0%,wa=0%   表示CPU壓力大,有進程在佔用CPU作運算。

Mem:   1914488k total,   468192k used,  1446296k free,    73692k buffers
Swap:   835576k total,        0k used,   835576k free,   240788k cached
物理內存爲2G,規劃內存爲468M左右,剩餘內存爲1.44G,共享緩存爲73M,高速緩存240M。
虛擬內存爲835M,沒有使用,剩餘835M。
使用內存:高速緩存+共享緩存=320M

[root@asianux4 ~]# cat /proc/meminfo
MemTotal:        1914488 kB
MemFree:         1446568 kB
Buffers:           73940 kB    共享緩存
Cached:           240792 kB    高速緩存
SwapCached:            0 kB
Active:           142040 kB    活動緩存
Inactive:         196632 kB    非活動緩存
Active(anon):      24120 kB
Inactive(anon):      128 kB
Active(file):     117920 kB
Inactive(file):   196504 kB
Unevictable:           0 kB

L(小寫) 顯示或關閉linux系統負載行
t     顯示或關閉進程和CPU行。
m      顯示或關閉內存行。
1     顯示或關閉多核CPU顯示。
z     顯示或關閉顏色
b     顯示或關閉高負載的進程。
k     殺死進程
r     調整進程的優先級,默認優先級爲0, 20~-19  -19優先級最高。
h     查看幫助。

sar 顯示CPU的性能,磁盤,頁面,IO的信息。

[root@asianux4 ~]# sar 1 10
[root@asianux4 ~]# sar 1 10
Linux 2.6.32-431.20.3.el6.x86_64 (asianux4)     2015年09月18日  _x86_64_        (4 CPU)

23時14分13秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
23時14分14秒     all      0.00      0.00      0.25      0.00      0.00     99.75
23時14分15秒     all      0.00      0.00      0.00      0.00      0.00    100.00
23時14分16秒     all      0.00      0.00      0.25      0.00      0.00     99.75
23時14分17秒     all      0.00      0.00      0.00      0.00      0.00    100.00
23時14分18秒     all      0.00      0.00      0.25      0.00      0.00     99.75
23時14分19秒     all      0.00      0.00      0.00      0.00      0.00    100.00
23時14分20秒     all      0.00      0.00      0.00      0.00      0.00    100.00
23時14分21秒     all      0.00      0.00      0.25      0.00      0.00     99.75
23時14分22秒     all      0.00      0.00      0.00      0.00      0.00    100.00
23時14分23秒     all      0.00      0.00      0.00      0.00      0.00    100.00
平均時間:     all      0.00      0.00      0.10      0.00      0.00     99.90

23時20分35秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
23時20分36秒     all      0.00      0.00     11.95      0.00      0.00     88.05
23時20分38秒     all      0.00      0.00     32.70      3.77      0.00     63.52
23時20分39秒     all      0.00      0.00     23.08     38.06      0.00     38.87
23時20分40秒     all      0.00      0.00      7.02     26.32      0.00     66.67
23時20分41秒     all      0.00      0.00     10.81     40.54      0.00     48.65
23時20分42秒     all      0.00      0.00     20.49     42.62      0.00     36.89

[root@asianux4 ~]# sar -d 1 每秒掃描一次。
23時23分07秒       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
23時23分08秒   dev11-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
23時23分08秒   dev8-16      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
23時23分08秒    dev8-0    256.45  65651.61      0.00    256.00      2.60     10.16      6.18    158.39
23時23分08秒   dev8-32    143.55     12.90 102812.90    716.31      3.53     24.60     10.57    151.77
23時23分08秒   dev8-64    109.68      0.00 101161.29    922.35      3.07     25.90     14.74    161.61
23時23分08秒   dev8-80      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
23時23分08秒   dev8-48    108.06      0.00 101161.29    936.12      2.86     24.33     13.60    146.94
23時23分08秒  dev253-0    254.84  65238.71      0.00    256.00      2.59     10.18      6.21    158.23
23時23分08秒  dev253-1      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
23時23分08秒    dev9-0  24879.03     12.90 199019.35      8.00      0.00      0.00      0.00      0.00
23時23分08秒  dev253-2  24879.03     12.90 199019.35      8.00    829.52     28.60      0.07    175.00
23時23分08秒  dev253-3      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

vmstat 顯示虛擬內存的情況。

[root@asianux4 ~]# vmstat 1
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0  77268  21360 1652176    0    0     4     6    4    5  0  0 100  0  0
 1  0      0  77260  21360 1652192    0    0     0     0   20   19  0  0 100  0  0
 0  0      0  77244  21368 1652200    0    0     0    72   43   48  0  0 100  0  0
 0  0      0  77244  21368 1652204    0    0     0     0   15   17  0  0 100  0  0
 0  0      0  77244  21368 1652208    0    0     0     0   14   21  0  0 100  0  0
 0  0      0  77244  21368 1652208    0    0     0     0   16   19  0  0 100  0  0
 0  0      0  77244  21368 1652208    0    0     0     0   14   23  0  0 100  0  0
 0  0      0  77244  21368 1652208    0    0     0     0   16   20  0  0 100  0  0

iostat 顯示磁盤IO情況。
[root@asianux4 ~]# iostat 1
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00   28.04   10.14    0.00   61.82

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
scd0              0.00         0.00         0.00          0          0
sdb               0.00         0.00         0.00          0          0
sda               3.96         0.00        55.45          0         56
sdc             121.78       182.18     51169.31        184      51681
sde             122.77       150.50     49347.52        152      49841
sdf               0.00         0.00         0.00          0          0
sdd             116.83       332.67     51232.67        336      51745
dm-0              6.93         0.00        55.45          0         56
dm-1              0.00         0.00         0.00          0          0
md0           12800.99        15.84    102392.08         16     103416
dm-2              0.00         0.00         0.00          0          0
dm-3          12801.98        15.84    102400.00         16     103424

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00   13.83   16.60    0.00   69.57

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
scd0              0.00         0.00         0.00          0          0
sdb               0.00         0.00         0.00          0          0
sda               0.00         0.00         0.00          0          0
sdc              78.00        24.00     40680.00         24      40680
sde              80.00        40.00     41680.00         40      41680
sdf               0.00         0.00         0.00          0          0
sdd              79.00       328.00     40680.00        328      40680
dm-0              0.00         0.00         0.00          0          0
dm-1              0.00         0.00         0.00          0          0
md0           10136.00         0.00     81088.00          0      81088
dm-2              0.00         0.00         0.00          0          0
dm-3          10136.00         0.00     81088.00          0      81088


ps    顯示進程
[root@asianux4 ~]# ps -ef    顯示全部進程
[root@asianux4 ~]# ps -aux    顯示系統中全部進程的詳細信息。

pstree     顯示進程樹
[root@asianux4 ~]# pstree
init┬─NetworkManager
     ├─abrtd
     ├─acpid
     ├─atd
     ├─auditd───{auditd}
     ├─automount───4*[{automount}]
     ├─certmonger
     ├─console-kit-dae───63*[{console-kit-da}]
     ├─crond
     ├─cupsd
     ├─dbus-daemon
     ├─hald─┬─hald-runner─┬─hald-addon-acpi
     │        │               └─hald-addon-inpu
     │        └─{hald}
     ├─httpd───8*[httpd]
     ├─irqbalance
     ├─4*[login───bash]
     ├─master─┬─pickup
     │            └─qmgr
     ├─mcelog
     ├─2*[mingetty]
     ├─modem-manager
     ├─rpc.statd
     ├─rpcbind
     ├─rsyslogd───4*[{rsyslogd}]
     ├─sshd───bash───pstree
     ├─sshd
     ├─udevd───2*[udevd]
     ├─vsftpd
     └─wpa_supplicant
[root@asianux4 ~]#


顯示系統運行的時間。
[root@asianux4 ~]# uptime
 23:43:23 up 23:51,  5 users,  load average: 0.03, 0.09, 0.10
[root@asianux4 ~]# cat /proc/uptime
85901.05 341926.67
[root@asianux4 ~]#

顯示內存的狀況:
[root@asianux4 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          1869       1800         68          0         21       1636
-/+ buffers/cache:        142       1727
Swap:          815          0        815

真正的使用內存:142+21=163M    1800-1636-1=163M

顯示多核CPU的情況:mpstat

[root@asianux4 ~]# mpstat -P ALL 1
23時49分44秒  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle

23時49分45秒  all    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
23時49分45秒    0    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
23時49分45秒    1    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
23時49分45秒    2    0.00    0.00    0.97    0.00    0.00    0.00    0.00    0.00   99.03
23時49分45秒    3    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00

第一列:時間
第二列:CPU及CPU的核數
第三列:用戶佔用CPU的百分比
第四列:優先級調整時佔用CPU的百分比
第五列:系統
第六列:iowait IO等待
第七列:硬中斷
第八列:軟中斷
第九列:虛擬CPU中虛擬指令佔用CPU的百分比(虛擬環境中使用)
第十列:虛機佔用CPU的百分比
第十一列:CPU的空閒百分比

顯示進程的動態連接庫文件及佔用內存的大小。

[root@asianux4 ~]# pmap -x 7652
7652:   /usr/sbin/sshd
Address           Kbytes     RSS   Dirty Mode   Mapping
00007f8ad1bf1000      48       0       0 r-x--  libnss_files-2.12.so
00007f8ad1bfd000    2048       0       0 -----  libnss_files-2.12.so
00007f8ad1dfd000       4       4       4 r----  libnss_files-2.12.so
00007f8ad1dfe000       4       4       4 rw---  libnss_files-2.12.so
00007f8ad1dff000      28       0       0 r-x--  librt-2.12.so
00007f8ad1e06000    2044       0       0 -----  librt-2.12.so
00007f8ad2005000       4       4       4 r----  librt-2.12.so
00007f8ad2006000       4       4       4 rw---  librt-2.12.so
00007f8ad2007000     228       0       0 r-x--  libnspr4.so
00007f8ad2040000    2048       0       0 -----  libnspr4.so
00007f8ad2240000       4       4       4 r----  libnspr4.so
00007f8ad2241000       8       8       8 rw---  libnspr4.so

查看系統中進程的動態內存。
[root@asianux4 ~]# cat while.sh
#!/bin/bash
while true
do
pmap -d 7652|tail -1
sleep 2
done
[root@asianux4 ~]# chmod +x while.sh
[root@asianux4 ~]# sh while.sh
mapped: 66616K    writeable/private: 808K    shared: 0K
mapped: 66616K    writeable/private: 808K    shared: 0K
mapped: 66616K    writeable/private: 808K    shared: 0K


進程的調試:strace
[root@asianux4 ~]# strace -c -p 6610
Process 6610 attached - interrupt to quit
Process 6610 detached
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
  -nan    0.000000           0        39           select
  -nan    0.000000           0        39           wait4
------ ----------- ----------- --------- --------- ----------------
100.00    0.000000                    78           total

顯示當前系統全部進程的動態連接庫。
[root@asianux4 ~]# lsof |grep vsftpd
vsftpd    3788      root  cwd       DIR              253,0     4096          2 /
vsftpd    3788      root  rtd       DIR              253,0     4096          2 /
vsftpd    3788      root  txt       REG              253,0   159568     176841 /usr/sbin/vsftpd
vsftpd    3788      root  mem       REG              253,0   124624     176040 /lib64/libselinux.so.1
vsftpd    3788      root  mem       REG              253,0   472064     163188 /lib64/libfreebl3.so
...後面已省略

找到動態連接庫文件後,再到rpmfind.net或關盤網址查閱/lib64/libpthread-2.12.so

查看本機監控的端口。
[root@asianux4 ~]# lsof -i
COMMAND    PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rpcbind   1330     rpc    6u  IPv4  10522      0t0  UDP *:sunrpc
rpcbind   1330     rpc    7u  IPv4  10524      0t0  UDP *:rmc
rpcbind   1330     rpc    8u  IPv4  10525      0t0  TCP *:sunrpc (LISTEN)
rpcbind   1330     rpc    9u  IPv6  10527      0t0  UDP *:sunrpc
rpcbind   1330     rpc   10u  IPv6  10529      0t0  UDP *:rmc
rpcbind   1330     rpc   11u  IPv6  10530      0t0  TCP *:sunrpc (LISTEN)
rpc.statd 1459 rpcuser    5u  IPv4  10808      0t0  UDP *:787
rpc.statd 1459 rpcuser    8u  IPv4  10816      0t0  UDP *:37593
rpc.statd 1459 rpcuser    9u  IPv4  10820      0t0  TCP *:58964 (LISTEN)
rpc.statd 1459 rpcuser   10u  IPv6  10824      0t0  UDP *:59776
rpc.statd 1459 rpcuser   11u  IPv6  10828      0t0  TCP *:39172 (LISTEN)
cupsd     1494    root    6u  IPv6  11113      0t0  TCP localhost:ipp (LISTEN)
cupsd     1494    root    7u  IPv4  11114      0t0  TCP localhost:ipp (LISTEN)
cupsd     1494    root    9u  IPv4  11117      0t0  UDP *:ipp
master    1753    root   12u  IPv4  12026      0t0  TCP localhost:smtp (LISTEN)
master    1753    root   13u  IPv6  12028      0t0  TCP localhost:smtp (LISTEN)
vsftpd    3788    root    3u  IPv4  17645      0t0  TCP *:ftp (LISTEN)
sshd      5402    root    3u  IPv4  20693      0t0  TCP asianux4:EtherNet/IP-1->192.168.232.1:ndsconnect (ESTABLISHED)
rsyslogd  5880    root    3u  IPv4  22305      0t0  UDP *:syslog
rsyslogd  5880    root    4u  IPv6  22306      0t0  UDP *:syslog
httpd     6610    root    4u  IPv6  23365      0t0  TCP *:http (LISTEN)
httpd     6613  apache    4u  IPv6  23365      0t0  TCP *:http (LISTEN)
httpd     6614  apache    4u  IPv6  23365      0t0  TCP *:http (LISTEN)
httpd     6615  apache    4u  IPv6  23365      0t0  TCP *:http (LISTEN)
httpd     6616  apache    4u  IPv6  23365      0t0  TCP *:http (LISTEN)
httpd     6617  apache    4u  IPv6  23365      0t0  TCP *:http (LISTEN)
httpd     6618  apache    4u  IPv6  23365      0t0  TCP *:http (LISTEN)
httpd     6619  apache    4u  IPv6  23365      0t0  TCP *:http (LISTEN)
httpd     6620  apache    4u  IPv6  23365      0t0  TCP *:http (LISTEN)
sshd      7652    root    3u  IPv4  26541      0t0  TCP *:ssh (LISTEN)
sshd      7652    root    4u  IPv6  26543      0t0  TCP *:ssh (LISTEN)
sshd      7654    root    3r  IPv4  26547      0t0  TCP asianux4:ssh->192.168.232.1:solid-e-engine (ESTABLISHED)
[root@asianux4 ~]#

[root@asianux4 ~]# netstat -atnup
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1330/rpcbind
tcp        0      0 0.0.0.0:58964               0.0.0.0:*                   LISTEN      1459/rpc.statd
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      3788/vsftpd
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      7652/sshd
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      1494/cupsd

查看遠程主機開放的端口及操做系統。

[root@asianux4 ~]# nmap 10.6.65.181

Starting Nmap 5.51 ( http://nmap.org ) at 2015-09-19 00:46 CST
Nmap scan report for client (10.6.65.181)
Host is up (0.00086s latency).
Not shown: 997 filtered ports
PORT    STATE  SERVICE
21/tcp  closed ftp
22/tcp  open   ssh
443/tcp closed https
MAC Address: 00:0C:29:8C:61:1F (VMware)

Nmap done: 1 IP address (1 host up) scanned in 31.43 seconds

[root@asianux4 ~]# nmap -O 10.6.65.181

Starting Nmap 5.51 ( http://nmap.org ) at 2015-09-19 00:47 CST
Nmap scan report for client (10.6.65.181)
Host is up (0.00079s latency).
Not shown: 997 filtered ports
PORT    STATE  SERVICE
21/tcp  closed ftp
22/tcp  open   ssh
443/tcp closed https
MAC Address: 00:0C:29:8C:61:1F (VMware)
Device type: general purpose|WAP|specialized
Running (JUST GUESSING): Linux 2.6.X|2.4.X (89%), Netgear embedded (89%), Linksys Linux 2.4.X (87%), Asus Linux 2.6.X (87%), Crestron 2-Series (86%)
Aggressive OS guesses: Linux 2.6.23 - 2.6.33 (89%), Linux 2.6.31 - 2.6.34 (89%), Linux 2.6.9 - 2.6.27 (89%), Netgear DG834G WAP (89%), Linux 2.6.27 (Ubuntu 8.10) (88%), Linux 2.6.22 (Fedora Core 6) (88%), Linux 2.6.32 (88%), Linux 2.6.34 (88%), OpenWrt White Russian 0.9 (Linux 2.4.30) (87%), OpenWrt 0.9 - 7.09 (Linux 2.4.30 - 2.4.34) (87%)
No exact OS matches for host (test conditions non-ideal).
Network Distance: 1 hop

[root@asianux4 ~]# telnet 10.6.65.181 22
Trying 10.6.65.181...
Connected to 10.6.65.181.
Escape character is '^]'.
SSH-2.0-OpenSSH_5.3

Connection closed by foreign host.
[root@asianux4 ~]#

查看主機的socket鏈接信息。ss和netstat命令類似
[root@asianux4 ~]# netstat -atnup|grep :21
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      3788/vsftpd
[root@asianux4 ~]# ss -antup|grep :21
tcp    LISTEN     0      32                     *:21                    *:*      users:(("vsftpd",3788,3))
[root@asianux4 ~]#

抓包工具:tcpdump
抓取FTP服務器的用戶名和密碼。
[root@asianux4 ~]# tcpdump -i eth0 -nn -X 'port 21'

抓取ssh服務器的通訊包。
[root@asianux4 ~]# tcpdump -i eth0 host 192.168.232.1 and port 22

監控網絡流量iptraf
[root@asianux4 ~]# yum install iptraf -y
[root@asianux4 ~]# unset LANG
[root@asianux4 ~]# iptraf    查看網絡流量
9.18-2
相關文章
相關標籤/搜索