rsync通过服务同步、日志文件、screen工具
2017-12-06 00:00
471 查看
10.32 rsync通过服务同步(上)
在使用该方法进行同步之前需要先在服务的开启相应服务同时要监听一个端口(可自定义),默认是监听873端口,开启服务之前编辑rsync配置文件“/etc/rsyncd.conf”(将服务添加进去)然后直接使用命令:“rsync --daemon(服务名称)”进行同步。或者将配置文件写到一个自定义文件中,使用命令“rsync --configfile”进行同步。配置完成后客户端可以通过指定端口与服务端进行通信。
配置服务
编辑配置文件:
[root@cham002 ~]# vi !$ vi /etc/rsyncd.conf port=873 #指定启动rsync服务的端口,如果不指定,默认为873端口 log file=/var/log/rsync.log #指定日志文件 pid file=/var/run/rsyncd.pid #指定pid文件,这个文件的作用涉及服务的启动、停止等进程操作。 address=192.168.133.130 #指定启动rsyncd服务的IP。假如你的机器有多个IP,就可以指定由其中一个启动rsyncd服务,如果不指定该参数,默认是在全部IP上启动。 [test] #指定模块名,里面内容自定义 path=/root/rsync #指定数据存放的路径 use chroot=true #use chroot=true/false:安全参数,表示在传输文件前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true,如果你的数据当中有软连接文件,建议设置成false。 max connections=4 #指定最大的连接数,默认是0,即没有限制。 read only=no #read only ture|false:如果为true,则不能上传到该模块指定的路径下。 list=true #表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出(此时功能类似于tab),false则隐藏。 uid=root gid=root #指定传输文件时以哪个uid和gid的身份进行。 auth users=test #指定传输时要使用的用户名 secrets file=/etc/rsyncd.passwd #指定密码文件,该参数连同上面的参数如果不指定,则不使用密码验证。 #注意:该密码文件的权限一定要是600。格式:“用户名:密码”如:test:champin hosts allow=192.168.133.132 1.1.1.1 2.2.2.2 192.168.133.0/24 #表示被允许连接该模块的主机,可以是IP或者网段,如果是多个,中间用空格隔开。
说明: 当设置了auth users和secrets file后,客户端连服务端也需要用用户名密码了,若想在命令行中带上密码,可以设定一个密码文件“/etc/pass”,然后执行命令:“ rsync -avP /tmp/123123 --port=8730 test@192.168.230.135::test/tmp/yy.log”,其中/etc/pass内容就是一个密码,权限要改为600(文件内容格式为:“用户名:密码”)。
[root@cham002 ~]# vim /etc/rsyncd.passwd 密码格式test:cham [root@cham002 ~]# chmod 600 /etc/rsyncd.passwd [root@cham1 ~]# rsync -avP /tmp/123123 --port=8730 test@192.168.230.135::test/tmp/yy.log Password: sending incremental file list 123123 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) sent 66 bytes received 27 bytes 37.20 bytes/sec total size is 0 speedup is 0.00 手动输入密码写在脚本里不方便 客户端 [root@cham1 ~]# vi /etc/rsync_pass.txt 写入密码 [root@cham1 ~]# chmod 600 !$ [root@cham1 ~]# rsync -avP /tmp/yy --port=8730 --password-file=/etc/rsync_pass.txt test@192.168.230.135::test/tmp/y123 sending incremental file list created directory /tmp/y123 yy/ yy/123.txt 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/2) sent 91 bytes received 31 bytes 81.33 bytes/sec total size is 0 speedup is 0.00
启动服务
[root@cham002 ~]# vi /etc/rsyncd.conf [root@cham002 ~]# rsync --daemon [root@cham002 ~]# pa aux |grep rsync -bash: pa: 未找到命令 [root@cham002 ~]# ps aux |grep rsync root 3064 0.0 0.0 114644 544 ? Ss 20:41 0:00 rsync --daemon root 3068 0.0 0.0 112664 972 pts/0 S+ 20:41 0:00 grep --color=auto rsync [root@cham002 ~]# 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 1703/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2419/master tcp 0 0 192.168.230.135:873 0.0.0.0:* LISTEN 3064/rsync tcp6 0 0 :::22 :::* LISTEN 1703/sshd tcp6 0 0 ::1:25 :::* LISTEN 2419/master [root@cham002 ~]# cat /etc/rsyncd.conf # /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for more options. # configuration example: # uid = nobody # gid = nobody # use chroot = yes # max connections = 4 # pid file = /var/run/rsyncd.pid # exclude = lost+found/ # transfer logging = yes # timeout = 900 # ignore nonreadable = yes # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # [ftp] # path = /home/ftp # comment = ftp export area port=873 log file=/var/log/rsync.log pid file=/var/run/rsyncd.pid address=192.168.230.135 [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.230.130 1.1.1.1 2.2.2.2 192.168.230.0/24 [root@cham002 ~]# mkdir /tmp/rsync [root@cham002 ~]# chmod 777 /tmp/rsync
注: 在配置文件中设定path时注意所给予的权限,安全起见不要直接使用root目录!!!
配置文件中“[]”为模块,里面内容为模块名称,该模块代表的路径即为path指定的路径。
使用服务进行同步
在客户端进行同步:同步到服务端 [root@cham1 ~]# rsync -avp /tmp/123123 192.168.230.135::test/123123.txt sending incremental file list 123123 sent 66 bytes received 27 bytes 62.00 bytes/sec total size is 0 speedup is 0.00 [root@cham002 ~]# ls /tmp/rsync 123123.txt 将服务端数据同步到本机(客户端): [root@cham1 ~]# rsync -avp 192.168.230.135::test/123123.txt /tmp/321.txt receiving incremental file list 123123.txt sent 45 bytes received 101 bytes 292.00 bytes/sec total size is 0 speedup is 0.00 [root@cham1 ~]# ls /tmp 123123 222_dest 321.txt systemd-private-85eb551e7da440e89503daf8ae845e90-vmtoolsd.service-bVO4ZE test111
telnet命令
>telnet命令用于登录远程主机,对远程主机进行管理。telnet因为采用明文传送报文,安全性不好,很多Linux服务器都不开放telnet服务,而改用更安全的ssh方式了。语法: telnet [options] [参数]
应用: 检测端口是否可通信
[root@cham1 ~]# telnet 192.168.230.135 873 Trying 192.168.230.135... Connected to 192.168.230.135. Escape character is '^]'. @RSYNCD: 30.0 #退出该命令时使用Ctrl+],然后输入quit即可! @ERROR: protocol startup error Connection closed by foreign host.
10.33 rsync通过服务同步(下)
编辑rsync配置文件,将端口改为port=8730重启rsync服务
安装killall工具:“yum install -y psmisc”。[root@cham002 ~]# killall rsync [root@cham002 ~]# !ps ps aux |grep rsync root 3182 0.0 0.0 112664 968 pts/0 S+ 21:22 0:00 grep --color=auto rsync [root@cham002 ~]# rsync --daemon [root@cham002 ~]# ps aux |grep rsyn root 3185 0.0 0.0 114644 548 ? Ss 21:22 0:00 rsync --daemon root 3191 0.0 0.0 112664 972 pts/0 R+ 21:22 0:00 grep --color=auto rsyn [root@cham002 ~]# !net 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 1703/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2419/master tcp 0 0 192.168.230.135:8730 0.0.0.0:* LISTEN 3185/rsync tcp6 0 0 :::22 :::* LISTEN 1703/sshd tcp6 0 0 ::1:25 :::* LISTEN 2419/master
进行同步
说明: 因未开放873端口,所以此时同步数据会报错!那么如何进行同步呢?方法如下: 使用--port选项指定端口号
[root@cham1 ~]# rsync -avp 192.168.230.13::test/123123.txt /tmp/321.txt rsync: failed to connect to 192.168.230.13 (192.168.230.13): No route to host (113) rsync error: error in socket IO (code 10) at clientserver.c(122) [Receiver=3.0.9] [root@cham1 ~]# rsync -avp --port 8730 192.168.230.135::test/123123.txt /tmp/321.txt receiving incremental file list sent 26 bytes received 62 bytes 58.67 bytes/sec total size is 0 speedup is 0.00
10.34 Linux系统日志
/var/log/messages 系统总日志/etc/logrotate.conf 日志切割配置文件
/var/log/dmesg 系统启动日志
/var/log/scure 安全日志文件
dmesg命令
>查看系统硬件相关的日志。dmesg命令被用于检查和控制内核的环形缓冲区。kernel会将开机信息存储在ring buffer中。您若是开机时来不及查看信息,可利用dmesg来查看。##开机信息保存在:/var/log/dmesg文件里。
在该日志中可以查看系统有哪些硬件故障。
语法: dmesg [option]
Options:
-c:清除日志(系统重启会重新生成)
-n:指定记录信息的层级
last命令
>last命令用于显示用户最近登录信息。单独执行last命令,它会读取/var/log/wtmp的文件,并把该给文件的内容记录的登入系统的用户名单全部显示出来。lastb命令
>lastb命令用于显示用户错误的登录列表,此指令可以发现系统的登录异常。单独执行lastb命令,它会读取位于/var/log目录下,名称为btmp的文件,并把该文件内容记录的登入失败的用户名单,全部显示出来。(/var/log/btmp)[root@cham002 ~]# less /var/log/secure [root@cham002 ~]# tail -f !$ tail -f /var/log/secure Dec 5 20:43:32 cham002 sshd[2347]: pam_unix(sshd:session): session opened for user root by (uid=0) Dec 5 20:54:37 cham002 sshd[2562]: Accepted password for root from 192.168.230.135 port 55872 ssh2 Dec 5 20:54:37 cham002 sshd[2562]: pam_unix(sshd:session): session opened for user root by (uid=0) Dec 5 20:54:37 cham002 sshd[2562]: Received disconnect from 192.168.230.135: 11: disconnected by user Dec 5 20:54:37 cham002 sshd[2562]: pam_unix(sshd:session): session closed for user root Dec 5 23:04:23 cham002 sshd[2347]: pam_unix(sshd:session): session closed for user root Dec 6 20:29:01 cham002 sshd[3018]: Accepted password for root from 192.168.230.1 port 52981 ssh2 Dec 6 20:29:01 cham002 sshd[3018]: pam_unix(sshd:session): session opened for user root by (uid=0) Dec 6 20:53:31 cham002 polkitd[523]: Registered Authentication Agent for unix-process:3089:999364 (system bus name :1.113 [/usr/bin/pkttyagent --notify-fd 5 --fallback], object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale zh_CN.UTF-8) Dec 6 20:53:32 cham002 polkitd[523]: Unregistered Authentication Agent for unix-process:3089:999364 (system bus name :1.113, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale zh_CN.UTF-8) (disconnected from bus) Dec 6 22:26:10 cham002 sshd[3453]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.230.130 user=root Dec 6 22:26:10 cham002 sshd[3453]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root" Dec 6 22:26:11 cham002 sshd[3453]: Failed password for root from 192.168.230.130 port 40040 ssh2 ^C
扩展:logrotate命令
>logrotate命令用于对系统日志进行轮转、压缩和删除,也可以将日志发送到指定邮箱。使用logrotate指令,可让你轻松管理系统所产生的记录文件。每个记录文件都可被设置成每日,每周或每月处理,也能在文件太大时立即处理。您必须自行编辑,指定配置文件,预设的配置文件存放在/etc/logrotate.conf文件中。安装logrotate工具: yum install logrotate crontabs
Logrotate可配置参数,可使用man命令来查询:
compress 通过gzip压缩转储以后的日志 nocompress 不压缩 copytruncate 用于还在打开中的日志文件,把当前日志备份并截断 nocopytruncate 备份日志文件但是不截断 create mode owner group 转储文件,使用指定的文件模式创建新的日志文件 nocreate 不建立新的日志文件 delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩 nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。 errors address 专储时的错误信息发送到指定的Email 地址 ifempty 即使是空文件也转储,这个是 logrotate 的缺省选项。 notifempty 如果是空文件的话,不转储 mail address 把转储的日志文件发送到指定的E-mail 地址 nomail 转储时不发送日志文件 olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统 noolddir 转储后的日志文件和当前日志文件放在同一个目录下 prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行 postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行 daily 指定转储周期为每天 weekly 指定转储周期为每周 monthly 指定转储周期为每月 rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份 tabootext [+] list 让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~ size size 当日志文件到达指定的大小时才转储,bytes(缺省)及KB(sizek)或MB(sizem)
10.35 screen工具
>Screen是一款由GNU计划开发的用于命令行终端切换的自由软件。用户可以通过该软件同时连接多个本地或远程的命令行会话,并在其间自由切换。GNU Screen可以看作是窗口管理器的命令行界面版本。它提供了统一的管理多个会话的界面和相应的功能。• screen直接回车就进入了虚拟终端
• ctral a组合键再按d退出虚拟终端,但不是结束
• screen -ls 查看虚拟终端列表
• screen -r id 进入指定的终端
• screen -S aming
• screen -r aming
安装screen工具:yum install -y screen
将一个任务放入后台
# nohup command >[日志文件] 2>[日志文件] &
screen语法:
screen [option] [参数]
在每个screen session 下,所有命令都以 ctrl+a(C-a) 开始:
C-a d -> detach,暂时离开当前session,将目前的 screen session (可能含有多个 windows) 丢到后台执行,并会回到还没进 screen 时的状态,此时在 screen session 里,每个 window 内运行的 process (无论是前台/后台)都在继续执行,即使 logout 也不影响。
查看当前screen下的任务
[root@cham002 ~]# screen -ls There is a screen on: 3504.pts-0.cham002 (Detached) 1 Socket in /var/run/screen/S-root.
返回screen session
[root@cham002 ~]# screen -r 3504
终止screen:在screen 执行如下命令exit即可
[root@cham002 ~]# screen -ls No Sockets found in /var/run/screen/S-root.
为screen指定命名
[root@cham002 ~]# screen -S "test_screen" [detached from 3577.test_screen] [root@cham002 ~]# screen -ls There are screens on: 3577.test_screen (Detached) 3555.pts-0.cham002 (Detached) 3540.pts-0.cham002 (Detached) 3525.pts-0.cham002 (Detached) 4 Sockets in /var/run/screen/S-root.
说明: 这样做的目的是方便区分每个screen任务
相关文章推荐
- rsync通过服务同步、日志文件、screen工具
- rsync通过服务同步、日志文件、screen工具
- rsync通过服务同步、日志文件、screen工具
- 171206 8周3次课 rsync通过服务同步、linux系统日志、screen工具
- rsync通过服务的方式同步、linux系统日志、screen工具
- 10.32/10.33 rsync通过服务同步 10.34 linux系统日志 10.35 screen工具
- Linux学习第三十四篇:rsync通过服务同步,Linux系统日志,screen工具
- rsync通过服务同步、linux系统日志、screen工具
- rsync通过服务同步、linux系统日志、screen工具
- 10.32/10.33 rsync通过服务同步 10.34 linux系统日志 screen工具
- rsync服务同步、日志文件、screen工具
- rsync通过服务同步、 linux系统日志 、screen工具
- 12.6 rsync通过服务同步 linux系统日志 screen工具
- rsync通过服务同步、linux系统日志、screen工具
- rsync通过服务同步、linux系统日志和screen工具
- rsync通过服务同步、Linux系统日志、screen工具
- rsync通过服务同步,Linux系统日志,screen工具
- rsync服务同步、系统日志及screen工具介绍
- 32.rsync服务同步,Linux系统日志,screen工具
- rsync服务同步,linux日志,screen工具