linux10&15

###################

####10.系统日志####

###################

####1.系统日志默认分类####

/var/log/messages ##系统服务及日志,包括服务的信息,报错等等

/var/log/secure ##系统认证信息日志

/var/log/maillog ##系统邮件服务信息

/var/log/cron ##系统定时任务信息

/var/log/boot.log ##系统启动信息

[root@localhost ~]# > /var/log/messages    ##清空日志信息

[root@localhost ~]# cat /var/log/messages   ##显示日志信息

[root@localhost ~]# systemctl restart rsyslog.service ##重启rsyslog

[root@localhost ~]# cat /var/log/messages   ##显示日志信息

Mar 30 19:44:58 localhost rsyslogd-3000: sd_journal_get_cursor() failed: 'Cannot assign requested address'

Mar 30 19:44:58 localhost rsyslogd: [origin software="rsyslogd" swVersion="7.4.7" x-pid="2776" x-info="http://www.rsyslog.com"] exiting on signal 15.

Mar 30 19:44:58 localhost rsyslogd: [origin software="rsyslogd" swVersion="7.4.7" x-pid="2798" x-info="http://www.rsyslog.com"] start

Mar 30 19:44:58 localhost rsyslogd-2307: warning: ~ action is deprecated, consider using the 'stop' statement instead [try http://www.rsyslog.com/e/2307 ]

####2.日志管理服务rsyslog####

1.rsyslog负责采集日志和分类存放日志

2.rsyslog日志分类

vim /etc/rsyslog.conf ##主配置文件

服务.日志级别 /存放文件

*.* /var/log/yy   ##将日志存放到/var/log/yy目录中

systemctl restart rsyslog

###格式###

日志设备(类型).(连接符号)日志级别   日志处理方式(action)

####日志设备(可以理解为日志类型):####

auth         ##pam产生的日志

authpriv     ##ssh,ftp等登录信息的验证信息

cron         ##时间任务相关

kern         ##内核

lpr          ##打印

mail         ##邮件

mark(syslog)–rsyslog ##服务内部的信息,时间标识

news         ##新闻组

user         ##用户程序产生的相关信息

uucp         ##unix to unix copy, unix主机之间相关的通讯

local 1~7    ##自定义的日志设备

####日志级别####

———————————————————————-

debug        ##有调式信息的,日志信息最多

info         ##般信息的日志,最常用

notice       ##最具有重要性的普通条件的信息

warning      ##警告级别

err          ##错误级别,阻止某个功能或者模块不能正常工作的信息

crit         ##严重级别,阻止整个系统或者整个软件不能正常工作的信息

alert        ##需要立刻修改的信息

emerg        ##内核崩溃等严重信息

none         ##什么都不记录

##注意:从上到下,级别从低到高,记录的信息越来越少

##详细的可以查看手册: man 3 syslog

####连接符号####

———————————————————————-

.xxx: 表示大于等于xxx级别的信息

.=xxx:表示等于xxx级别的信息

.!xxx:表示在xxx之外的等级的信息

#####实例####

1. 记录到普通文件或设备文件::

*.*     /var/log/file.log    # 绝对路径

eg:

[root@localhost ~]# vim /etc/rsyslog.conf

[root@localhost ~]# ll /var/log/westos

ls: 无法访问/var/log/westos: 没有那个文件或目录

[root@localhost ~]# systemctl restart rsyslog.service

[root@localhost ~]# ll /var/log/westos

-rw-r--r--. 1 root root 1403 3月  29 08:57 /var/log/westos

*.*     /dev/pts/0

[root@localhost ~]# vim /etc/rsyslog.conf

[root@localhost ~]# systemctl restart rsyslog.service

[root@localhost ~]# ll /dev/pts/0

crw--w----. 1 root tty 136, 0 3月  29 08:59 /dev/pts/0

测试: logger -p local3.info ‘KadeFor is testing the rsyslog and logger ‘   logger 命令用于产生日志

2. 发送给用户(需要在线才能收到)

*.*   root

*.*   root,kadefor,up01      # 使用,号分隔多个用户

*.*   *           # *号表示所有在线用户

3. 忽略,丢弃

local3.*   ~          # 忽略所有local3类型的所有级别的日志

4. 执行脚本::

local3.*    ^/tmp/a.sh       # ^号后跟可执行脚本或程序的绝对路径

                               # 日志内容可以作为脚本的第一个参数.

                                 #  可用来触发报警

####日志同步####

systemctl stop firewalld ##关闭两台主机的火墙

配置日志发送方

vim /etc/rsyslog.conf ##主配置文件

*.*             @172.25.0.11 ##通过udp协议把日志发送到11主机,@udp,@@tcp

systemctl restart rsyslog.service  ##

systemctl stop firewalld ##关闭两台主机的火墙

配置日志接受方

vim /etc/rsyslog.conf ##主配置文件

15 $ModLoad imudp ##日志接收插件

16 $UDPServerRun 514 ##日志接收插件使用端口

systemctl restart rsyslog.service  ##

systemctl stop firewalld ##关闭两台主机的火墙

netstat -anulpe | grep rsyslog

udp        0      0 0.0.0.0:514             0.0.0.0:*                           0          122073     32654/rsyslogd      

udp6       0      0 :::514                  :::*                                0          122074     32654/rsyslogd      

测试

> /var/log/messages ##两边都作清空日志

logger test message  ##日志发送方

tail -f /var/log/message ##日志接收方,-f时间监控

eg:

将你主机上的所有日志信息同步到真机上;

配置日志发送方

vim /etc/rsyslog.conf

*.*             @172.25.254.41 ##通过udp协议把日志发送到11主机,@udp,@@tcp

systemctl restart rsyslog.service

systemctl stop firewalld

配置日志接受方

vim /etc/rsyslog.conf

15 $ModLoad imudp ##日志接收插件

16 $UDPServerRun 514 ##日志接收插件使用端口

systemctl restart rsyslog.service

systemctl stop firewalld

####日志采集格式####

$template QQ, "%timegenerated% %FROMHOST-IP% %syslogtag% %msg% "

 *.*                      /var/log/yy;QQ     ##以QQ这样的格式采集日志

%timegenerated% ##显示日志时间

%FROMHOST-IP% ##显示主机ip

%syslogtag% ##日志记录目标

%msg% ##日志内容

##换行

cd####

systemd-journald ##进程名称

journalctl ##直接执行,浏览系统日志

-n 3 ##显示最新3条

-p err ##显示报错

-f ##监控日志

--since --until ## --since "[YYYY-MM-DD] [hh:mm:ss]" 从什么时间到什么时间的日志

eg:

[root@localhost ~]# journalctl  --since 08:12:02

-- Logs begin at 三 2017-03-29 08:12:02 EDT, end at 三 2017-03-29 08:42:26 EDT.

3月 29 08:12:02 localhost systemd-journal[112]: Runtime journal is using 8.0M (m

3月 29 08:12:02 localhost systemd-journal[112]: Runtime journal is using 8.0M (m

3月 29 08:12:02 localhost kernel: Initializing cgroup subsys cpuset

3月 29 08:12:02 localhost kernel: Initializing cgroup subsys cpu

3月 29 08:12:02 localhost kernel: Initializing cgroup subsys cpuacct

3月 29 08:12:02 localhost kernel: Linux version 3.10.0-123.el7.x86_64 (mockbuild

3月 29 08:12:02 localhost kernel: Command line: BOOT_IMAGE=/boot/vmlinuz-3.10.0-

3月 29 08:12:02 localhost kernel: e820: BIOS-provided physical RAM map:

3月 29 08:12:02 localhost kernel: BIOS-e820: [mem 0x0000000000000000-0x000000000

-o verbose ##显示日志能够使用的详细进程参数

##_SYSTEMD_UNIT=sshd.service服务名称

##_PID=1182进程pid

[root@localhost ~]# ps aux | grep journald

root       350  0.0  0.1  42992  3088 ?        Ss   08:12   0:00 /usr/lib/systemd/systemd-journald

root      3507  0.0  0.0 112660   956 pts/1    R+   08:45   0:00 grep --color=auto journald

-o verbose ##显示日志能够使用的详细进程参数

##_SYSTEMD_UNIT=sshd.service服务名称

##_PID=1182进程pid

systemd-journald管理

##默认情况下此程序会忽略重启前的日志信息,如不忽略:

mkdir /var/log/journal

chown root:systemd-journal /var/log/journal

chmod 2755 /var/log/journal

killall -1 systemd-journald  ****************************

ls /var/log/journal/4513ad59a3b442ffa4b7ea88343fa55f

system.journal user-1000.journal

####4.时间同步####

1.服务端

yum install chrony -y ##安装服务

vim /etc/chrony.conf ##主配置文件

21 # Allow NTP client access from local network.

22 allow 172.25.0.0/24 ##允许谁去同步我的时间

27 # Serve time even if not synchronized to any NTP server.

28 local stratum 10 ##不去同步任何人的时间,时间同步服务器级别

systemctl restart chronyd

systemctl stop firewalld

2.客户端

vim /etc/chrony.conf

  3 server 0.rhel.pool.ntp.org iburst

  4 server 1.rhel.pool.ntp.org iburst====> server ntpserverip iburst

  5 server 2.rhel.pool.ntp.org iburst====>

  6 server 3.rhel.pool.ntp.org iburst

systemctl restart chronyd

测试:

[root@localhost ~]# chronyc sources -v

210 Number of sources = 1

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.

 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,

| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.

||                                                 .- xxxx [ yyyy ] +/- zzzz

||                                                /   xxxx = adjusted offset,

||         Log2(Polling interval) -.             |    yyyy = measured offset,

||                                              |    zzzz = estimated error.

||                                   |           |                         

MS Name/IP address         Stratum Poll Reach LastRx Last sample

===============================================================================

^* 172.25.0.11                  10   6   377    41   +170us[ +201us] +/-  191us

eg:

实现你的虚拟机和你的真机时间同步

1.服务端

vim /etc/chrony.conf    ##主配置文件

21 # Allow NTP client access from local network.

22 allow 172.25.254.141/24 ##允许谁去同步我的时间

27 # Serve time even if not synchronized to any NTP server.

28 local stratum 10     ##不去同步任何人的时间,时间同步服务器级别

systemctl restart chronyd

systemctl stop firewalld

2.客户端

date 11111111

clock -w

clock -c

vim /etc/chrony.conf

  3 server 172.25.254.41 iburst

  4# server 1.rhel.pool.ntp.org iburst

  5 #server 2.rhel.pool.ntp.org iburst

  6 #server 3.rhel.pool.ntp.org iburst

systemctl restart chronyd

systemctl stop firewalld

####5.timedatectl命令####

timedatectl status ##显示当前时间信息

set-time ##设定当前时间

set-timezone ##设定当前时区

set-local-rtc 0|1 ##设定是否使用utc时间

eg:

[root@localhost ~]# timedatectl set-timezone Asia/Shanghai ##设定亚洲上海为当前时区

[root@localhost ~]# timedatectl set-local-rtc 1  ##不使用伦敦时间

[root@localhost ~]# vim /etc/adjtime ###############

[root@localhost ~]# timedatectl set-time "11:36:56"  ##设定当前时间

[root@localhost ~]# date

2017年 11月 12日 星期日 11:37:03 CST

#########################

####15.系统虚拟机管理####

#########################

####1.安装####

#!/bin/bash ##命令运行环境的指定

virt-install ##安装虚拟机

--name $1 ##虚拟机名称指定,$1表示脚本后的第一串字符   #####

--ram 1000 ##内存

--file /var/lib/libvirt/images/$1.img ##硬盘文件

--file-size 8 ##硬盘文件大小

--cdrom /var/ftp/pub/iso/rhel-server-7.1-x86_64-dvd.iso &  ##安装源指定

####2.管理####

virt-manager ##开启图形管理工具

virt-viewer vmname ##显示虚拟机,vmname表示虚拟机名称

virsh list ##列出正在运行的vm

virsh list --all ##列出所有vm

virsh start vmname ##运行指定vm

virsh shutdown vmname ##正常关闭指定vm

virsh destroy vmname ##强行结束指定vm

virsh create vmname.xml ##临时恢复指定vm,vmname表示前端管理文件

virsh define vmname.xml ##永久恢复vm

virsh undefine  vmname ##删除vm的前端管理,不会删除存储

####3.虚拟机快照####

qemu-img create -f qcow2 -b vm2.qcow2 node1.qcow2

!/bin/bash

virsh destroy $1 &> /dev/null

virsh undefine $1 &> /dev/null

qemu-img create -f qcow2 -b /var/lib/libvirt/images/$1.qcow2  /var/lib/libvirt/images/$2.qcow2 &> /dev/null

virt-install

--name $2

--ram 1000

--disk /var/lib/libvirt/images/$2.qcow2

--import &> /dev/null &

#########安装虚拟机脚本

[root@foundation41 Desktop]# vim vm_create.sh

#!/bin/bash

virt-install

--name $*

--memory 1024

--vcpus 2

--file /var/lib/libvirt/images/$*.qcow2

--file-size 8

--cdrom /home/kiosk/Desktop/rhel-server-7.2-x86_64-dvd.iso &

~                                                               

[root@foundation41 Desktop]# chmod +x vm_create.sh   ##使脚本有执行权限

[root@foundation41 Desktop]# ./vm_create.sh    ##执行脚本

[root@foundation41 ~]# cd /etc/libvirt/qemu/   ##/etc/libvirt/qemu/存放前端管理文件

[root@foundation41 qemu]# ls

desktop.xml  networks  server.xml  westos.xml

[root@foundation41 qemu]# cd /mnt/

[root@foundation41 mnt]# ls

build.txt  etc  pycharm-community-4.0.1

[root@foundation41 mnt]# virsh define westosred.xml   ##永久恢复westosred前端文件

error: Failed to open file 'westosred.xml': No such file or directory

[root@foundation41 mnt]# scp root@172.25.254.42:/etc/libvirt/qemu/

usage: scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]

           [-l limit] [-o ssh_option] [-P port] [-S program]

           [[user@]host1:]file1 ... [[user@]host2:]file2

[root@foundation41 mnt]# scp root@172.25.254.42:/etc/libvirt/qemu/westosred.xml /mnt/    ##将172.25.254.42主机的westosred前端文件考到本主机的/mnt目录下

The authenticity of host '172.25.254.42 (172.25.254.42)' can't be established.

ECDSA key fingerprint is f2:5e:9f:f4:87:12:4b:10:7a:42:16:1f:a0:3a:9a:53.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '172.25.254.42' (ECDSA) to the list of known hosts.

root@172.25.254.42's password:

Permission denied, please try again.

root@172.25.254.42's password:

westosred.xml                                 100% 4271     4.2KB/s  

00:00     ##复制 完成

[root@foundation41 mnt]# scp root@172.25.254.42:/var/lib/libvirt/images/westosred.qcow2 /mnt/ 将172.25.254.42主机的westosred硬盘文件考到本主机的/mnt目录下

root@172.25.254.42's password:

westosred.qcow2                                                                 westosred.qcow2                               100% 9218MB  10.9MB/s   14:05    

[root@foundation41 mnt]# cd /etc/libvirt/qemu/

[root@foundation41 qemu]# virsh create westosred.xml

error: Failed to open file 'westosred.xml': No such file or directory

[root@foundation41 qemu]# mv westosred.qcow2 /var/lib/libvirt/images/

mv: cannot stat ‘westosred.qcow2’: No such file or directory

[root@foundation41 qemu]# cd /mnt/

[root@foundation41 mnt]# mv westosred.qcow2 /var/lib/libvirt/images/ ##将westosred的镜像移动到/var/lib/libvirt/images/目录下

[root@foundation41 mnt]# ls

build.txt  etc  pycharm-community-4.0.1  westosred.xml

[root@foundation41 mnt]# virsh define westosred.xml

Domain westosred defined from westosred.xml

[root@foundation41 mnt]# virsh start westosred.xml

error: failed to get domain 'westosred.xml'

error: Domain not found: no domain with matching name 'westosred.xml'

[root@foundation41 mnt]# virsh start westosred  ##开启westosred快照

Domain westosred started

[root@foundation41 mnt]# virt-manager

[root@foundation41 mnt]# virsh destroy westosred ##关闭westosred快照

Domain westosred destroyed

[root@foundation41 mnt]# rm -fr /var/lib/libvirt/images/westosred.qcow2  ##删除westosred快照硬盘

[root@foundation41 mnt]# virsh undefine westosred ##删除westosred快照前端

Domain westosred has been undefined

##########快照脚本

[root@foundation41 images]# vim vm_snopshot.sh

#!/bin/bash

qemu-img create -fqcow3 -b /var/lib/libvirt/images/$1.qcow2 /var/lib/libvirt/images/$2.qcow2

virt-install

--name $2

--memory 1024

--file  /var/lib/libvirt/images/$2.qcow2

--import &

#############重启脚本

[root@foundation41 images]# vim vm_reset.sh

#!/bin/bash

virsh destroy $1

rm -fr  /var/lib/libvirt/images/$1.qcow2

qemu-img create -f qcow2 -b /var/lib/libvirt/images/$2.qcow2  /var/lib/libvirt/images/$1.qcow2

virsh start $1

virt-viewer $1

原文地址:https://www.cnblogs.com/yangying2017/p/6648480.html