本文索引:
- rsync通过服务同步
- 配置文件/etc/rsyncd.conf
- 启动rsync服务
- rsyncd.conf参数说明
- 错误和排查
- Linux系统日志
- /var/log/messages文件
- dmesg命令
- last/lastb命令
- /var/log/secure文件
- screen工具基本使用
rsync通过服务同步
需要开启一个服务,采用C/S架构,服务器端开启rsync服务,并开启端口(默认873);然后客户端就可以通过该端口与服务器端开始通信。
配置文件/etc/rsyncd.conf
启动服务前需要编辑配置文件
# rsyncd.conf样例
# 服务的默认端口号,可以修改,但需重启才能生效
# 修改默认端口后,同步时需加--port参数
port=873
# 指定log日志文件
log file=/var/log/rsync.log
# 指定pid文件
pid file=/var/run/rsyncd.pid
# 指定监听的ip
address=192.168.133.130
# 模块名
[test]
# 指定数据存放的目录
path=/root/rsync
# 表示在传输文件前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,
# 但缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。
# 修改该值无需重启服务就会生效
use chroot=true
# 最大同时连接客户端数,默认为0,及不限制
max connections=4
#
read only=true
# list为true时,执行rsync 192.168.65.133:: 回车会返回模块名;若为false就不会显示
list=true
# 指定传输文件时使用的身份
uid=root
gid=root
# 指定传输时要使用的用户名
auth users=test
# 指定密码文件,配合auth使用
secrets file=/etc/rsyncd.passwd
# 允许连接的主机(可以是多个,也可以是一个网段的)
hosts allow=192.168.133.134 1.1.1.1 2.2.2.2 192.168.133.0/24
创建配置文件中指定的目录,并修改权限
[root@server ~]# mkdir /tmp/rsync
[root@server ~]# chmod 777 !$
chmod 777 /tmp/rsync
[root@server ~]# ls -ld /tmp/rsync/
drwxrwxrwx. 2 root root 6 12月 6 20:59 /tmp/rsync/
启动rsync服务
服务器端启动服务rsync --daemon(默认情况下 )
[root@server ~]# rsync --daemon
[root@server ~]# ps aux | grep rsync
root 2592 0.0 0.0 114656 528 ? Ss 20:58 0:00 rsync --daemon
root 2594 0.0 0.0 112680 972 pts/0 S+ 20:58 0:00 grep --color=auto rsync
[root@server ~]# netstat -ltnp
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 890/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2153/master
tcp 0 0 192.168.65.134:873 0.0.0.0:* LISTEN 2592/rsync
tcp6 0 0 :::3306 :::* LISTEN 2042/mysqld
tcp6 0 0 :::22 :::* LISTEN 890/sshd
tcp6 0 0 ::1:25 :::* LISTEN 2153/master
## 如果rsyncd.conf文件存储到了其他路径,那么使用时需要加上--conf-file=路径/rsyncd.conf
同步文件(这里已经将连接问题解决了)
[root@client ~]# rsync -avP /tmp/1.txt 192.168.65.134::test/server.txt
sending incremental file list
1.txt
1082 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
sent 1151 bytes received 27 bytes 2356.00 bytes/sec
total size is 1082 speedup is 0.92
rsyncd.conf参数说明
- use chroot
# 默认情况下chroot值为true,如果你的数据当中有软连接文件,建议你设置成false。在rsync命令指定-L参数后,将无法将连接所指的源文件同步
[root@server ~]# ls -l /tmp/rsync/passwd.ln
lrwxrwxrwx. 1 root root 11 12月 6 21:24 /tmp/rsync/passwd.ln -> /etc/passwd
# 加上-L参数也无法将链接文件所指的文件同步过来
[root@client ~]# rsync -avPL 192.168.65.134::test/passwd.ln /tmp/1.ln
receiving incremental file list
rsync: link_stat "/passwd.ln" (in test) failed: No such file or directory (2)
sent 4 bytes received 8 bytes 8.00 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1518) [Receiver=3.0.9]
# 修改chroot=false后,就可以同步源文件了!
[root@client ~]# rsync -avPL 192.168.65.134::test/passwd.ln /tmp/1.ln
receiving incremental file list
passwd.ln
1082 100% 1.03MB/s 0:00:00 (xfer#1, to-check=0/1)
sent 45 bytes received 1186 bytes 2462.00 bytes/sec
total size is 1082 speedup is 0.88
[root@server ~]# ls -l /tmp/rsync/1.ln
-rw-r--r--. 1 root root 1082 11月 28 21:25 /tmp/rsync/1.ln
- read only参数
# 服务端设置了read only为true,客户端将无法推(同步)数据到服务端!
- port参数
# rsync服务默认端口为873
# 如果修改了rsyncd.conf内的端口,可以在同步时添加--port参数来开放端口
# 修改了端口的rsyncd服务需要重启才能生效!!
[root@client ~]# rsync -avP --port 8730 192.168.65.134::test/1.txt /tmp/test
receiving incremental file list
1.txt
1082 100% 1.03MB/s 0:00:00 (xfer#1, to-check=0/1)
sent 45 bytes received 1186 bytes 2462.00 bytes/sec
total size is 1082 speedup is 0.88
- list参数
# 该参数是安全选项,最好设置为false
# 设置为true
[root@client ~]# rsync -avP 192.168.65.134::
test
# 设置为false,不显示模块名,更安全
[root@client ~]# rsync -avP 192.168.65.134::
- uid/gid参数
# 指定传输时使用的用户,本处设置为了root,这样同步后的文件的所有者和所有组都会使root
- auth users和secrets file参数
rsyncd.conf里设置了auth users和secret file参数的,使用下列方式同步,明确用户名,同时需要输入密码:
[root@centos7 ~]# rsync -avP /root/test.cap test@192.168.65.133::test/test.txt
Password:
sending incremental file list
test.cap
546 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
sent 635 bytes received 27 bytes 1324.00 bytes/sec
也可以在服务端通过指定密码文件的方式来免输密码:在/tmp/rsync_pass
[root@client ~]# vi /tmp/rsync_pass
输入 "模块名:密码"
保存退出
#避免客户端手动输入密码的方法:
在客户端创建一个密码文件/etc/rsync_passwd,权限改为600,文件内只放入密码即可;然后同步是使用--password-file=/tmp/rsync_pass 指定
[root@client ~]# vi /tmp/rsync_pass
输入test用户的密码即可(跟服务器密码文件不同)
保存退出
[root@centos7 ~]# rsync -avP /root/test.cap test@192.168.65.133::test/test.txt
sending incremental file list
test.cap
546 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
sent 635 bytes received 27 bytes 1324.00 bytes/sec
- host allow参数
允许哪些主机来进行rsync同步,多个ip使用空格连接
错误和排查
连接出现如下错误
[root@centos7 ~]# rsync -avP /root/test.cap 192.168.65.133::test/test.txtrsync: failed to connect to 192.168.65.133 (192.168.65.133): No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(122) [sender=3.0.9]
检测排错思路:
- 网络是否连通 ping
[root@centos7 ~]# ping 192.168.65.133
PING 192.168.65.133 (192.168.65.133) 56(84) bytes of data.
64 bytes from 192.168.65.133: icmp_seq=1 ttl=64 time=0.248 ms
64 bytes from 192.168.65.133: icmp_seq=2 ttl=64 time=0.295 ms
^C
--- 192.168.65.133 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.248/0.271/0.295/0.028 ms
# 说明ip是通的
- 端口是否通telnet
# 格式 telnet IP PORT 检测目标的端口是否可通
[root@client ~]# telnet 192.168.65.134 873
Trying 192.168.65.134...
telnet: connect to address 192.168.65.134: No route to host
# 说明端口不通
# 若连接成功,使用ctrl+]、ctrl+d退出telnet模式
- 查看iptables
[root@centos7 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
5069 12M ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
36 2356 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
1 52 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
63 3336 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 3933 packets, 233K bytes)
pkts bytes target prot opt in out source destination
# 执行 systemctl stop firewalld命令关闭防火墙(客户端、服务器端都关闭)
# 然后验证telnet
[root@client ~]# telnet 192.168.65.134 873
Trying 192.168.65.134...
Connected to 192.168.65.134.
Escape character is '^]'.
@RSYNCD: 30.0
^]
telnet> Connection closed.
Linux系统日志
/var/log/messages 系统日志(不包括已指定自己的日志的的服务)
记录的内容比较复杂,包括服务的开始、停止等,随时间的变化,这个文件会变的很大,需要进行日志的切割:logrotate命令
# 自动切换后的messages日志
[root@client ~]# ls -l /var/log/messages*
-rw-------. 1 root root 9259 12月 6 21:50 /var/log/messages
-rw-------. 1 root root 468677 11月 13 21:38 /var/log/messages-20171113
-rw-------. 1 root root 274123 11月 21 20:10 /var/log/messages-20171121
-rw-------. 1 root root 136526 11月 28 20:40 /var/log/messages-20171128
-rw-------. 1 root root 1081595 12月 6 21:46 /var/log/messages-20171206
- logrotate命令的配置文件
[root@client ~]# cat /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs 保留4个星期(即1个月)的日志文件
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.
- 对系统日志的自动切割操作
[root@client ~]# cat /etc/logrotate.d/syslog
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
missingok
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
dmesg命令:列出系统中硬件相关的日志
硬盘损坏、网卡故障等会出现在该日志中
demsg命令显示的日志是存储在内存中的,使用dmesg -c
命令可以清空,后续出现故障或重启主机后,该日志将不再为空
[root@client ~]# dmesg
...
[ 37.108427] IPv6: ADDRCONF(NETDEV_UP): ens37: link is not ready
[ 37.113334] e1000: ens37 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[ 110.845838] e1000: ens33 NIC Link is Down
[ 115.218991] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[ 1086.395698] sched: RT throttling activated
[ 3673.122093] e1000: ens33 NIC Link is Down
[ 3679.139262] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
/var/log/dmesg
last命令:成功登录服务器的用户记录
[root@client ~]# last
root pts/1 192.168.65.1 Wed Dec 6 20:37 - 20:38 (00:00)
root pts/0 192.168.65.1 Wed Dec 6 20:23 still logged in
root pts/0 192.168.65.1 Tue Dec 5 18:50 - 19:43 (00:53)
...
last命令调用的是/var/log/wtmp文件,该文件是二进制文件,无法直接查看
与之相对应的还有一个命令:lastb --> 用来查看登录失败的用户记录;其调用的文件是/var/log/btmp,同样无法直接查看内容。
/var/log/secure文件
记录登录系统的信息,包括登录成功的、失败的,通过查看该文件可以发现服务器是否非正常登录(暴力破解)
screen工具基本使用
screen命令可以看做是一个虚拟的屏幕(终端),可以在远程登录过程中不会因为意外(网络)而导致任务的中断,既可以在后台运行,也可以调到前台运行。
- 安装
[root@client ~]# yum install -y screen
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.zju.edu.cn
* epel: mirrors.ustc.edu.cn
* extras: mirrors.zju.edu.cn
* updates: mirrors.zju.edu.cn
...
已安装:
screen.x86_64 0:4.1.0-0.23.20120314git3c2946.el7_2
完毕!
- 使用 直接输入screen后回车,就进入了screen虚拟终端
将screen放到后台运行:ctrl+A 再+d
[root@centos ~]# screen
[detached from 4530.pts-0.client]
- 查看当前由多少个screen在后台运行
[root@client ~]# screen -ls
There are screens on:
4565.pts-0.client (Detached)
4530.pts-0.client (Detached)
2 Sockets in /var/run/screen/S-root.
- 进入指定的screen
[root@client ~]# screen -r 4530
[detached from 4530.pts-0.client]
- 定义自定义的screen
[root@client ~]# screen -S "test"
[detached from 4594.test]
[root@client ~]# screen -ls
There are screens on:
4594.test (Detached)
4565.pts-0.client (Detached)
4530.pts-0.client (Detached)
3 Sockets in /var/run/screen/S-root.
[root@client ~]# screen -r test
[detached from 4594.test] # 未退出
- 退出screen:进入后输入exit退出
[root@client ~]# screen -r test
[screen is terminating] #退出状态