博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

LINUX学习

Posted on 2016-07-04 14:54  徐正柱-  阅读(1084)  评论(0编辑  收藏  举报
9.14课后练习:
一、安装asianux server 4系统,具体要求如下。
1、/boot分区,大小为100M;根分区和swap分区创建在LVM逻辑卷上,其中swap分区的名称为lv_swap,大小为2048M;根分区的名称为lv_root,大小为剩余空间。
2、设置超级管理员root密码为123456.
3、选择“基本服务器”套件包,并选择安装开发工具包,ftp服务器包,dns服务器软件包。

二、创建/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


三、创建/root/file.txt文件,设置权限为所有用户都具有读写权限。
touch /root/file.txt
chmod 666 /root/file.txt


四、将/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 解压到指定目录


五、查找/root目录下所有空文件和空目录,并将其显示结果保存到/root/empty.txt文件中
find /root -empty >>/root/empty.txt

>  覆盖
>> 追加

六、查找权限为644且属有root用户的文件,并将其显示结果保存到/root/root.txt文件中。
find . -perm 644 -a -user root >> /root/root.txt


七、将/backup/etc-20150914.tar.xz文件解压到/root/dir1目录下。
tar -xJf /backup/etc-20150914.tar.xz -C /root/dir1

八、复制/var/log目录下的所有文件到/root/dir1目录下。并改log目录改名为logic。
cp -rap /var/log /root/dir1
mv /root/dir1/log /root/dir1/logic

九、通过快速查找命令locate,查找file.txt文件的位置。
updatedb
locate file.txt

十、创建/root/dir2目录,设置sgid权限,属有者为root,工作组为bin。
mkdir /root/dir2
chmod g+s /root/dir2
chown root.bin /root/dir2

9.15课后练习:
一、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


二、创建用户alex,用户ID号为2345,密码为123321
useradd -u 2345 alex
echo 123321 |passwd --stdin alex 或 passwd alex

三、创建用户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

四、创建用户user2,要求只允许访问授权的资源,不允许管理linux系统。
useradd -s /sbin/nologin user2

五、创建用户admin,要求具有root一样的权限。
useradd -u 0 -o -g 0 admin
useradd -u 0 -o -g 0 -c "root" -d /root -s /bin/bash admin

六、创建work1工作组,并将alex,user1,user2,admin用户添加以work1工作组。其中admin为work1工作组的管理员。
groupadd work1
gpasswd -M alex,user1,user2,admin work1
gpasswd -A admin work1

七、设置grub的加密密码为123456,超时时间为5秒。设置运行级别为级别3。
vim /boot/grub/grub.conf
password --md5 <grub-md5-crypt>  写在标题的上方。
timeout=5
vim /etc/inittab
id:3:initdefault:

八、每周六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

九、通过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


十、设置光盘开机时自动挂载。
[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课后练习:
一、对/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


二、打开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连接

三、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

四、设置eth0网卡的第二个IP地址为192.168.x.100/24,并设置开机时自动激活。
[root@asianux4 ~]# vim /etc/rc.local 在文件末尾添加以下行。
ifconfig eth0:1 192.168.232.100/24

五、安装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


六、设置到达202.1.2.3主机的所有请求包,通过eth0转发。
[root@asianux4 ~]# route add -host 202.1.2.3 dev eth0


9.17课后练习
一、在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


二、在线对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

三、忘记root密码和grub密码时,需要如何破解。
进入救援模式,一路回车,chroot /mnt/sysimage-->vi /boot/grub/grub.conf将password行删除,并保存-->passwd root更改root密码-->exit--->reboot重启。


四、备份/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


五、将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的命
一、目录相关的命令:
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的命令
一、目录相关的命令:
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地址。
2、在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,输入密码即可登录。
5、登录到linux系统后,再执行su -命令切换到root用户。
[aa@server4 ~]$ su -
[root@server4 ~]# 


二、帮助类的命令
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

三、权限控制命令
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
四、显示文件内容的命令
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中止)

五、关机和重启命令
shutdown 关机或重启命令
reboot  重启
init     切换运行级别。0表示关机,6表示重启
halt     关机


关机命令:
shutdown -h now
halt
init 0
poweroff

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


六、压缩和打包类的命令
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 ~]# 

七、查找的类命令。
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
选项:
1、查找文件类型:-type f|d|b|c|l|s|p
 find . -type f

2、按权限查找:-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
课后练习:
一、安装asianux server 4系统,具体要求如下。
1、/boot分区,大小为100M;根分区和swap分区创建在LVM逻辑卷上,其中swap分区的名称为lv_swap,大小为2048M;根分区的名称为lv_root,大小为剩余空间。
2、设置超级管理员root密码为123456.
3、选择“基本服务器”套件包,并选择安装开发工具包,ftp服务器包,dns服务器软件包。

二、创建/root/dir1目录,设置dir1目录的权限为属有者和工作组具有所有权限,其它用户具有只读权限,属有者为bin,工作组为root.

三、创建/root/file.txt文件,设置权限为所有用户都具有读写权限。

四、将/etc/目录的所有文件打包压缩后备份到/backup目录下,并设置文件为etc-20150914.tar.xz

五、查找/root目录下所有空文件和空目录,并将其显示结果保存到/root/empty.txt文件中

六、查找权限为644且属有root用户的文件,并将其显示结果保存到/root/root.txt文件中。

七、将/backup/etc-20150914.tar.xz文件解压到/root/dir1目录下。

八、复制/var/log目录下的所有文件到/root/dir1目录下。并改log目录改名为logic。

九、通过快速查找命令locate,查找file.txt文件的位置。

十、创建/root/dir2目录,设置sgid权限,属有者为root,工作组为bin。
9.14-课后练习
课后练习:
一、安装asianux server 4系统,具体要求如下。
1、/boot分区,大小为100M;根分区和swap分区创建在LVM逻辑卷上,其中swap分区的名称为lv_swap,大小为2048M;根分区的名称为lv_root,大小为剩余空间。
2、设置超级管理员root密码为123456.
3、选择“基本服务器”套件包,并选择安装开发工具包,ftp服务器包,bind服务器软件包。

二、创建/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


三、创建/root/file.txt文件,设置权限为所有用户都具有读写权限。
touch /root/file.txt
chmod 666 /root/file.txt 或 chmod 777 /root/file.txt

四、将/etc/目录的所有文件打包压缩后备份到/backup目录下,并设置文件为etc-20150914.tar.xz
mkdir /backup
tar -cvJf /backup/etc-20150914.tar.xz /etc
ls /backup/

五、查找/root目录下所有空文件和空目录,并将其显示结果保存到/root/empty.txt文件中
find /root -empty -exec ls -l {} \; > /root/empty.txt
或
find /root -empty > /root/empty.txt

六、查找权限为644且属有root用户的文件,并将其显示结果保存到/root/root.txt文件中。
find . -perm 644 -a -user root > /root/root.txt

七、将/backup/etc-20150914.tar.xz文件解压到/root/dir1目录下。
tar -xJf /backup/etc-20150914.tar.xz -C /root/dir1

八、复制/var/log目录下的所有文件到/root/dir1目录下。并改log目录改名为logic。
cp -rap /var/log /root/dir1
mv /root/dir1/log /root/dir1/logic

九、通过快速查找命令locate,查找file.txt文件的位置。
updatedb
locate file.txt

十、创建/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、编辑模式:在左下角有插入、替换之类的内容。主要用于录入文字。
3、末行模式:左下角有: / ? 字符。主要用于处理文字和设置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服务
2、创建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

2、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

4、源码:webmin-1.680.tar.gz,没有操作系统和硬件环境的要求。


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

通过yum命令管理软件包。
1、创建yum源 /etc/yum.repos.d/*.repo
2、清除本地yum源的信息。 yum clean all
3、下载远程yum源的信息到本地 yum repolist;或yum list 
4、搜索软件包关健字 yum search gcc; yum search tomcat
5、安装软件包,卸载软件,更新软件包。 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
课后练习:
一、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


二、创建用户alex,用户ID号为2345,密码为123321

三、创建用户user1,用户ID号1000,初始组为bin,添加到root,postfix工作组中。自家目录为/tmp/user1,shell为/bin/bash.

四、创建用户user2,要求只允许访问授权的资源,不允许管理linux系统。

五、创建用户admin,要求具有root一样的权限。

六、创建work1工作组,并将alex,user1,user2,admin用户添加以work1工作组。其中admin为work1工作组的管理员。

七、设置grub的加密密码为123456,超时时间为5秒。设置运行级别为级别3。

八、每周六02:01以root身份执行备份/var/log日志到/backup目录下,备份文件名的格式为log-20150915.tar.bz2.

九、通过yum安装gcc,tomcat6软件包。并将gcc,tomcat软件包的版本信息写入到/root/rpm.txt文件中。

十、设置光盘开机时自动挂载。
9.15-课后练习
课后练习:
一、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中止执行


二、创建用户alex,用户ID号为2345,密码为123321
[root@asianux4 ~]# useradd -u 2345 alex
[root@asianux4 ~]# passwd alex
或
[root@asianux4 ~]# echo "123321" |passwd --stdin alex

三、创建用户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


四、创建用户user2,要求只允许访问授权的资源,不允许管理linux系统。
[root@asianux4 ~]# useradd -s /sbin/nologin user2
[root@asianux4 ~]# cat /etc/passwd |grep user2

五、创建用户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


六、创建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

七、设置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)


八、每周六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

九、通过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 追加

十、设置光盘开机时自动挂载。
[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命令管理
 1、配置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

安装软件包的步骤:
1、下载源码包。http://sourceforge.net
2、解压和解包源码到指定的目录上。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系统。
1、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
课后练习:
一、对/dev/sdb磁盘做分区,要求主分区大小为1G,linux系统类型;扩展分区大小为剩余空间,在扩展分区上创建两个逻辑分区,第一个逻辑分区大小为2G,LVM系统类型;第二个逻辑分区大小剩余空间,swap系统类型。将/dev/sdb1格式化为ext4,并设置开机时自动挂载到/mnt/sdb1目录上。

二、打开user10的VNC服务器端口:3.并在windows上使用vncviewer工具测试通过。

三、openssh配置实验,要求实现以下功能。
1、打开root用户远程登录功能
2、采用DSA密钥对方式登录,打开本地密码登录。
3、设置ssh的监听端口为2222。

四、设置eth0网卡的第二个IP地址为192.168.x.100/24,并设置开机时自动激活。

五、安装proftpd服务器,并将/var/log目录的备份到/var/ftp/log.tar.gz文件中,并通过ftp下载到windows上。

六、设置到达202.1.2.3主机的所有请求包,通过eth0转发。
9.16-课后练习
课后练习:
一、对/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]#

二、打开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]#

三、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 ~]#


四、设置eth0网卡的第二个IP地址为192.168.x.100/24,并设置开机时自动激活。
[root@asianux4 ~]# vim /etc/rc.local
  8 ifconfig eth0:1 192.168.232.100/24


五、安装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


六、设置到达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的硬盘。
2、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
4、mkfs.ext4 /dev/sdb1 创建文件系统
5、mkdir /mnt/sdb1;mount /dev/sdb1 /mnt/sdb1 创建挂载点,并将其挂载。
6、echo "/dev/sdb1    /mnt/sdb1    ext4    defaults    0 0" >>/etc/fstab

RAID磁盘阵列
分类:
1、软件的RAID:通过软件实现的RAID。mdadm软件实现RAID,CPU
2、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、1、5、6、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的创建过程:
1、创建物理卷 pvcreate /dev/md0
2、创建卷组 vgcreate vg00 /dev/md0
3、在卷组上,创建逻辑卷 lvcreate -L 1G -n lv01 vg00
4、创建文件系统 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、密码丢失:
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 ~]#



2、内核文件或映像文件丢失、错误。
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

解决办法:
1、从其它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

5、备份/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]#

6、/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


7、/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
课后练习
一、在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目录上。

二、在线对lv01逻辑卷扩容,扩容到4G。同时将/dev/sdb5扩容到vg00组中。

三、忘记root密码和grub密码时,需要如何破解。

四、备份/boot分区和mbr主引导记录信息,执行rm /boot/* -rf命令后重启,并恢复故障。

五、将ssh服务器的debug及以上的错误日志信息,写入到/var/log/sshd.log文件。
9.17-课后练习
课后练习
一、在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]#

二、在线对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]#

三、忘记root密码和grub密码时,需要如何破解。
答:进入救援模式
chroot /mnt/sysimage
vim /boot/grub/grub.conf 将password行删除
passwd root 更改root密码
exit
reboot

四、备份/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


五、将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    关闭

4、基于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