linux的rsync服务同步、系统日志及screen工具介绍

1. rsync通过服务同步

在远程主机上建立一个rsync的服务器,在服务器上配置好rsync的各种应用,然后将本机作为rsync的一个客户端连接远程的rsync服务器。

实验测试:

  1. 找两台主机,一个IP是172.16.111.100,另一个是172.16.111.110。
  2. 在100主机上建立并配置rsync的配置文件/etc/rsyncd.conf,如下所示:
虚拟机1操作
[root@gary-tao ~]# vi /etc/rsyncd.conf //(把以下内容编辑到rsyncd.conf文件里)
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=172.16.111.100
[test]
path=/tmp/rsync
use chroot=true
max connections=4
read only=no
list=true
uid=root
gid=root
#auth users=test
#secrets file=/etc/rsyncd.passwd
hosts allow=172.16.111.110
[root@gary-tao ~]# rsync --daemon  //启动服务,如果地址不在默认目录下,启动服务时需要添加地址
[root@gary-tao ~]# ps aux |grep rsync  //检测服务是否启动
root       4009  0.0  0.0 114656   524 ?        Ss   18:18   0:00 rsync --daemon
root       4017  0.0  0.0 112676   976 pts/0    S+   18:20   0:00 grep --color=auto rsync
[root@gary-tao ~]# netstat -lntp  //检查监听的端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      771/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1019/master         
tcp        0      0 172.16.111.100:873      0.0.0.0:*               LISTEN      4009/rsync          
tcp6       0      0 :::22                   :::*                    LISTEN      771/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1019/master 

虚拟机2操作
[root@localhost ~]# telnet 172.16.111.110 873 //检测端口是否通信
[root@localhost ~]# iptables -nvL //查看iptables
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
10951   16M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
    4   286 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
   15   868 INPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
   15   868 INPUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
   15   868 INPUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
[root@localhost ~]# systemctl stop firewalld //关闭firewalld服务
[root@localhost ~]# iptables -nvL //查看服务已关闭
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination   
[root@localhost ~]# rsync -avP /tmp/aming.txt 172.16.111.100::test/aming-02.txt  //同步文件并更改名称,如果这里需要输入密码,可以在虚拟机1里把配置文件密码注释掉。
sending incremental file list
aming.txt
        1386 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 1459 bytes  received 27 bytes  2972.00 bytes/sec
total size is 1386  speedup is 0.93
虚拟机1操作,验证文件同步成功
[root@gary-tao ~]# ls /tmp/rsync/
aming-02.txt

上面的 port,log file,pid file,address都属于全局配置。[]内的test是模块名,以下属于模块配置。一个配置文件可以有多个不同模块。各个参数含义如下:

port:指定在哪个端口启动rsyncd服务,默认是873端口。
log file:指定日志文件。
pid file:指定pid文件,这个文件的作用涉及服务的启动、停止等进程管理操作。
address:指定启动rsyncd服务的IP。假如你的机器有多个IP,就可以指定由其中一个启动rsyncd服务,如果不指定该参数,默认是在全部IP上启动。
[]:指定模块名,里面内容自定义。
path:指定数据存放的路径。
use chroot true|false:表示在传输文件前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true,如果你的数据当中有软连接文件,阿铭建议你设置成false。
max connections:指定最大的连接数,默认是0,即没有限制。
read only ture|false:如果为true,则不能上传到该模块指定的路径下。
list:表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出,false则隐藏。
uid/gid:指定传输文件时以哪个用户/组的身份传输。
auth users:指定传输时要使用的用户名。

secrets file:指定密码文件,该参数连同上面的参数如果不指定,则不使用密码验证。注意该密码文件的权限一定要是600。格式:用户名:密码
hosts allow:表示被允许连接该模块的主机,可以是IP或者网段,如果是多个,中间用空格隔开。 

举例说明:当设置了auth users和secrets file后,客户端连服务端也需要用用户名密码了,若想在命令行中带上密码,可以设定一个密码文件
rsync -avL test@172.16.111.100::test/test1/ /tmp/test8/ --password-file=/etc/pass,其中/etc/pass内容就是一个密码,权限要改为600

2. linux系统日志

日志记录了系统每天发生的各种各样的事情,比如监测系统状况、排查系统故障等,你可以通过日志来检查错误发生的原因,或者受到***时***留下的痕迹。日志的主要功能是审计和监测,还可以实时的监测系统状态,监测和追踪侵入者等。

  • /var/log/messages 是核心的系统日志,包含了系统启动时的引导消息,以及系统运行时的其他状态信息,I/0错误、网络错误和其他系统错误都会记录到这个文件中来。通常情况下,/var/log/messages是做故障诊断时首先要查看的文件。

  • 日志切割配置文件
    系统上有很多服务,如果都把服务日志放到这个文件/var/log/messages里那岂不是会很大,所以系统有一个日志轮询的机制,每星期切换一个日志,切换后的日志名字类似于messages-20171206,会存放在/var/log/目录下面,连同messages一共有5个这样的日志文件,这里的20171206就是日期,它表示日志切割的年月日。它的配置文件是/etc/logrotate.conf(如果没有特殊要求,请不要修改这个配置文件),示例如下:
[root@gary-tao ~]# cat /etc/logrotate.conf 
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
    minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.

备注:/var/log/messages是由rsyslogd这个守护进程产生的,如果停止这个服务则系统不会产生/var/log/messages,所以这个服务不要停止。rsyslogd服务的配置文件为/etc/rsyslog,conf,这个文件定义了日志的级别,若没有特殊需求,这个配置文件是不需要修改的。

参考文章:https://my.oschina.net/u/2000675/blog/908189

dmesg

dmesg 这个命令显示系统的启动信息,在开机时启动,并存储在内存中。如果某个硬件有问题(如网卡),可以在这里看到,示例如下。

[root@gary-tao ~]# dmesg |head
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 3.10.0-514.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) ) #1 SMP Tue Nov 22 16:42:41 UTC 2016
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-514.el7.x86_64 root=UUID=cec56a03-0f96-43bf-8516-0027999cbe76 ro crashkernel=auto rhgb quiet LANG=zh_CN.UTF-8
[    0.000000] Disabled fast string operations
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009ebff] usable
[    0.000000] BIOS-e820: [mem 0x000000000009ec00-0x000000000009ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000000dc000-0x00000000000fffff] reserved
安全日志

last命令 用来查看登录linux的系统信息,示例如下:

[root@gary-tao ~]# last
root     pts/0        172.16.111.1     Tue Dec  5 15:58   still logged in   
root     tty1                          Tue Dec  5 15:57   still logged in   
reboot   system boot  3.10.0-514.el7.x Tue Dec  5 15:53 - 19:43 (1+03:50)   
root     tty1                          Tue Dec  5 15:44 - 15:53  (00:09)    
reboot   system boot  3.10.0-514.el7.x Tue Dec  5 15:43 - 15:53  (00:09)    
root     tty1                          Tue Dec  5 15:42 - 15:43  (00:00)    
reboot   system boot  3.10.0-514.el7.x Tue Dec  5 15:38 - 15:43  (00:05)    
root     tty1                          Tue Dec  5 15:34 - 15:37  (00:03)    
reboot   system boot  3.10.0-514.el7.x Tue Dec  5 15:33 - 15:37  (00:04)    
root     tty1                          Tue Dec  5 15:29 - 15:30  (00:01)    
reboot   system boot  3.10.0-514.el7.x Tue Dec  5 15:28 - 15:37  (00:09)    
root     tty1                          Tue Dec  5 15:19 - 15:23  (00:03)    
root     pts/0        192.168.4.84     Thu Nov 30 14:00 - down  (5+01:22)   
reboot   system boot  3.10.0-514.el7.x Thu Nov 30 14:00 - 15:23 (5+01:22)   
reboot   system boot  3.10.0-514.el7.x Thu Nov 30 13:56 - 13:58  (00:02)    
root     tty1                          Thu Nov 30 13:40 - 13:55  (00:15)    
reboot   system boot  3.10.0-514.el7.x Thu Nov 30 13:30 - 13:55  (00:25)    
root     pts/0        192.168.4.84     Wed Nov 29 19:58 - crash  (17:31)    
root     pts/0        192.168.4.84     Wed Nov 29 19:33 - 19:58  (00:24) 

last命令实际读取的是/var/log/wtmp文件,但这个文件不能直接用cat,vi,vim,head,tail查看。

/var/log/secure

/var/log/secure记录的也是和登录有关的信息,如ssh登录失败的信息在这里都可以记录到。

[root@gary-tao ~]# ls /var/log/secure
/var/log/secure

3. screen工具

有时我们要执行一个命令或者脚本,需要几小时甚至几天,在这个过程中,如果中途断网或出现其他意外情况怎么办?以下两种方法就是来避免这状况发生的。

nohup

首先写一个sleep.sh脚本,然后把它放到后台执行,示例如下:

[root@gary-tao ~]# nohup /usr/local/sbin/sleep.sh &

上例中,直接在sleep.sh后面加&虽然可以在后台运行,但是当退出该终端时,这个脚本很有可能也会退出,所以在前面加上nohup就没有问题了,执行后在当前目录下生成一个nohup的文件,它的作用就是防止进程意外中断,并且会把输出信息记录到nohup文件中。

screen工具的使用

screen是一个虚拟终端,简单来说,screen是一个可以在多个进程之间多路复用一个物理终端的窗口管理器,screen中有会话的概念,用户可以在一个screen会话中创建多个screen会话窗口,在每一个screen窗口中就像操作一个真实的ssh连接窗口一样。

操作示例:

首先screen安装  yum install -y screen

[root@gary-tao ~]# screen   //打开一个会话
[detached from 4151.pts-0.gary-tao]
[root@gary-tao ~]# screen
[detached from 4186.pts-0.gary-tao]
[root@gary-tao ~]# screen
[detached from 4207.pts-0.gary-tao]
[root@gary-tao ~]# screen -ls
There are screens on:
    4207.pts-0.gary-tao (Detached)
    4186.pts-0.gary-tao (Detached)
    4151.pts-0.gary-tao (Detached)
3 Sockets in /var/run/screen/S-root.

[root@gary-tao ~]# screen -r 4207 //退出后想再次进入会话,加-r,4207表示会话窗口编号,如果只打开一个窗口,后面可以省略编号

解释说明:退出一个会话按Ctrl+A键,再按d退出该screen会话(只是退出,并没有结束,结束screen会议要按Ctrl+D键或输入exit)。

备注:当你在某个需要长时间运行的命令或者脚本时,就打开一个screen会话,然后运行该任务,按Ctrl+A键再按d退出会话,这样不影响终端窗口上的任何操作。

转载于:https://blog.51cto.com/taoxie/2048094

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值