linux学习lesson38

目录

1 rsync通过服务同步

2 linux系统日志

3 screen工具


1 rsync通过服务同步

要编辑配置文件/etc/rsyncd.conf

  • 启动服务rsync --daemon
[root@linux01 ~]# rsync --daemon
  • 启动后可以查看一下日志,并查看端口是否启动
[root@linux01 ~]# vim /etc/rsyncd.conf
[root@linux01 ~]# cat /var/log/rsync.log
2018/10/28 21:39:59 [2141] rsyncd version 3.1.2 starting, listening on port 873
2018/10/28 21:42:37 [2153] connect from linux02 (192.168.139.112)
2018/10/28 13:42:37 [2153] rsync on test/dir/ from linux02 (192.168.139.112)
2018/10/28 13:42:37 [2153] building file list
2018/10/28 13:42:37 [2153] rsync: change_dir "/dir" (in test) failed: No such file or directory (2)
2018/10/28 21:43:00 [2154] connect from linux02 (192.168.139.112)
2018/10/28 13:43:00 [2154] rsync on test/test1/ from linux02 (192.168.139.112)
2018/10/28 13:43:00 [2154] building file list
2018/10/28 13:43:00 [2154] sent 425 bytes  received 138 bytes  total size 13
[root@linux01 ~]# netstat -lntp | grep rsync
tcp        0      0 192.168.139.111:873     0.0.0.0:*               LISTEN      2141/rsync   
  • 在linux01,机器上创建目录和文件
[root@linux01 ~]# mkdir rsync
[root@linux01 ~]# cd rsync/
[root@linux01 rsync]# mkdir test1
[root@linux01 rsync]# cd test1/
[root@linux01 test1]# touch 1.txt 2.txt 3.txt /root/123.txt
[root@linux01 test1]# ln -s /root/123.txt ./123.txt
[root@linux01 test1]# mkdir dir
[root@linux01 test1]# touch dir/4.txt dir/5.txt

如果配置文件是别的路径,启动服务需要添加指定路径:rsync --daemon --config=/usr/local/src/rsyncd.conf

格式:rsync -av test1/ 192.168.133.130::module/dir/

rsyncd.conf样例
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.139.111
[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=192.168.139.112 1.1.1.1 2.2.2.2  192.168.139.0/24
  • rsyncd.conf配置文件详解

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或者网段,如果是多个,中间用空格隔开。

  • 客户端上同步文件
  • 配置文件配置如下
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.139.111
[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=192.168.139.112 1.1.1.1 2.2.2.2  192.168.139.0/24
  • 测试rsyncd的服务端口是否连通
[root@linux02 ~]# telnet 192.168.139.111 873
Trying 192.168.139.111...
Connected to 192.168.139.111.
Escape character is '^]'.
@RSYNCD: 31.0
^]
telnet> quit
Connection closed.

这样是正常连上到rsyncd服务873端口的

  • 开始同步文件
[root@linux02 ~]# rsync -av 192.168.139.111::test/test1/ /tmp/test5/

  • 带-L参数同步文件
[root@linux02 ~]# rsync -avL 192.168.139.111::test/test1/ /tmp/test5/

出现了传输报错,需要修改配置文件

use chroot=true

改为

use chroot=false

或者使用sed命令修改

[root@linux01 test1]# sed -i 's/use chroot=true/use chroot=false/' /etc/rsyncd.conf
[root@linux02 ~]# rsync -avL 192.168.139.111::test/test1/ /tmp/test5/

  • 修改rsyncd的服务器配置文件端口为8730
[root@linux01 test1]# yum install -y psmisc  //killall命令的软件包
[root@linux01 test1]# killall rsync  //停止rsyncd服务
[root@linux01 test1]# rsync --daemon //启动rsyncd服务

再次同步文件

[root@linux02 ~]# rsync -avL --port 8730 192.168.139.111::test/test1/ /tmp/test5/
receiving incremental file list

sent 21 bytes  received 189 bytes  140.00 bytes/sec
total size is 0  speedup is 0.00
  • 隐藏rsyncd服务器的模块名
[root@linux02 ~]# rsync -avL --port 8730 192.168.139.111::
test

将list=true改为list=false

[root@linux02 ~]# rsync -avL --port 8730 192.168.139.111::
[root@linux02 ~]#
  • 将uid=root,gid=root改为uid=nobody,gid=nobody时候,会出现权限不够同步不了文件

所以因为设置为uid=root,gid=root

  • 去掉注释开启密码认证

auth users=test

secrets file=/etc/rsyncd.passwd

[root@linux01 test1]# vim /etc/rsyncd.passwd //(格式为test:123456,冒号后才是密码)
[root@linux01 test1]# chmod 600 /etc/rsyncd.passwd

再次同步文件

[root@linux02 ~]# rsync -av --port 8730 [email protected]::test/test1/ /tmp/test5/

  • 客户端免密码同步文件

在客户端上(linux02)

[root@linux02 ~]# vim /etc/pass (只需要输入密码123456)
[root@linux02 ~]# chmod 600 /etc/pass
[root@linux02 ~]# rsync -av --port 8730 --password-file=/etc/pass [email protected]::test/test1/ /tmp/test5/

参数hosts allow=192.168.139.112 1.1.1.1 2.2.2.2  192.168.139.0/24这个是指定可以访问服务器的ip和网段

2 linux系统日志

日志文件为/var/log/messages,它是核心系统日志文件,包含了系统启动时的引导消息,以及系统运行时的其他状态消息

  • /etc/logrotate.conf 日志切割配置文件
[root@linux02 ~]# 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命令

  • 可以显示系统的启动信息,如果某个硬件有问题(比如网卡),用这个命令也是可以看到/var/log/dmesg 日志
[root@linux01 ~]# 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-862.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Fri Apr 20 16:44:24 UTC 2018
[    0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-862.el7.x86_64 root=UUID=36a0e938-9ce7-4057-be14-67b1a1901480 ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8
[    0.000000] Disabled fast string operations
[    0.000000] e820: BIOS-provided physical RAM map:
[    0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009efff] usable
[    0.000000] BIOS-e820: [mem 0x000000000009f000-0x000000000009ffff] reserved
[    0.000000] BIOS-e820: [mem 0x00000000000ca000-0x00000000000cbfff] reserved
[root@linux01 ~]# cat /var/log/dmesg | tail
[    9.625246] sr 2:0:0:0: Attached scsi generic sg1 type 5
[    9.638571] cryptd: max_cpu_qlen set to 100
[    9.702589] AVX version of gcm_enc/dec engaged.
[    9.702592] AES CTR mode by8 optimization enabled
[    9.707723] alg: No test for __gcm-aes-aesni (__driver-gcm-aes-aesni)
[    9.708394] alg: No test for __generic-gcm-aes-aesni (__driver-generic-gcm-aes-aesni)
[    9.910509] Adding 1048572k swap on /dev/sda2.  Priority:-1 extents:1 across:1048572k FS
[    9.983429] XFS (sda1): Mounting V5 Filesystem
[   11.017190] XFS (sda1): Ending clean mount
[   11.711109] type=1305 audit(1540716511.252:3): audit_pid=486 old=0 auid=4294967295 ses=4294967295 res=1

last命令,调用的文件/var/log/wtmp

  • last命令用来查看登录Linux的历史信息
[root@linux01 ~]# last | head
root     pts/1        linux02          Sun Oct 28 21:28   still logged in   
root     pts/0        192.168.139.1    Sun Oct 28 16:49   still logged in   
reboot   system boot  3.10.0-862.el7.x Sun Oct 28 16:48 - 22:52  (06:04)    
root     pts/0        192.168.139.1    Thu Oct 25 10:13 - crash (3+06:35)   
reboot   system boot  3.10.0-862.el7.x Thu Oct 25 10:12 - 22:52 (3+12:40)   
root     pts/2        linux04          Thu Oct 25 10:09 - 10:10  (00:00)    
root     pts/1        linux04          Thu Oct 25 10:08 - 10:10  (00:01)    
root     pts/0        192.168.139.1    Thu Oct 25 09:59 - crash  (00:12)    
reboot   system boot  3.10.0-862.el7.x Thu Oct 25 09:58 - 22:52 (3+12:54)   
root     pts/0        192.168.139.1    Thu Oct 25 09:57 - down   (00:01)    
  • lastb命令查看登录失败的用户,对应的文件时/var/log/btmp /var/log/secure
[root@linux01 ~]# lastb
root     ssh:notty    192.168.139.100  Thu Oct 25 10:19 - 10:19  (00:00)    
root     ssh:notty    192.168.139.100  Thu Oct 25 10:17 - 10:17  (00:00)    
root     ssh:notty    192.168.139.100  Thu Oct 25 10:14 - 10:14  (00:00)    

btmp begins Thu Oct 25 10:14:11 2018

3 screen工具

为了不让一个任务意外中断

  • nohup command &
[root@linux01 ~]# vim sleep.sh
[root@linux01 ~]# cat sleep.sh
#!/bin/bash
sleep 1000
[root@linux01 ~]# nohup sh sleep.sh &

screen是一个虚拟终端

  • 安装screen软件包
[root@linux01 ~]# yum install -y screen
  • screen直接回车就进入了虚拟终端
[root@linux01 ~]# screen
  • ctral a组合键再按d退出虚拟终端,但不是结束
  • screen -ls 查看虚拟终端列表
[root@linux01 ~]# screen -ls
There are screens on:
    2386.pts-1.linux01    (Detached)
    2355.pts-1.linux01    (Detached)
2 Sockets in /var/run/screen/S-root.
  • screen -r id 进入指定的终端
[root@linux01 ~]# screen -r 2386
  • screen -S linux自定义screen终端名字
[root@linux01 ~]# screen -S linux
[screen is terminating]
[root@linux01 ~]# screen -ls
There is a screen on:
    2355.pts-1.linux    (Detached)
1 Socket in /var/run/screen/S-root.
  • screen -r linux切换到名为linux
[root@linux01 ~]# screen -r linux

 扩展
1. Linux日志文件总管logrotate http://linux.cn/article-4126-1.html
2. xargs用法详解 http://blog.csdn.net/zhangfn2011/article/details/6776925

猜你喜欢

转载自blog.csdn.net/InfiniteIdea_Go/article/details/83614743
今日推荐