SFTP(SSH File Transfer Protocol),安全文件傳送協議。有時也被稱作 Secure File Transfer Protocol 或 SFTP。它和SCP的區別是它允許用戶中斷傳輸,SCP拷貝速度稍快一些。SFTP可以為傳輸文件提供一種安全的加密方法。SFTP與FTP有著幾乎一樣的語法和功能。SFTP 為 SSH的一部分,是一種傳輸檔案至Blogger伺服器的安全方式。其實在SSH軟件包中,已經包含了一個叫作SFTP的安全文件傳輸子系統,SFTP本身沒有單獨的守護進程,它必須使用sshd守護進程(端口號默認是22)來完成相應的連接操作,所以從某種意義上來說,SFTP并不像一個服務器程序,而更像是一個客戶端程序。SFTP同樣是使用加密傳輸認證信息和傳輸的數據,所以,使用SFTP是非常安全的。但是,由于這種傳輸方式使用了加密/解密技術,所以傳輸效率比普通的FTP要低得多,如果您對網絡安全性要求更高時,可以使用SFTP代替FTP。
下面說下SFTP+Keepalived雙機高可用方案部署記錄: sftp-test01 172.16.51.191 sftp-test02 172.16.51.192 VIP 172.16.51.193 一、sftp-test01服務器操作: 1)查看openssh的版本 使用ssh -V 命令來查看openssh的版本,版本必須大于4.8p1,低于的這個版本需要升級。 [root@sftp-test01 ~]# ssh -V OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013 2)創建sftp組 [root@sftp-test01 ~]# groupadd sftp 3)創建一個sftp用戶,用戶名為mysftp,密碼為mysftp 修改用戶密碼和修改Linux用戶密碼是一樣的。 [root@sftp-test01 ~]# useradd -g sftp -s /bin/false mysftp [root@sftp-test01 ~]# passwd mysftp 4)sftp組的用戶的home目錄統一指定到/data/sftp下,按用戶名區分,這里先新建一個mysftp目錄,然后指定mysftp的home為/data/sftp/mysftp [root@sftp-test01 ~]# mkdir -p /data/sftp/mysftp [root@sftp-test01 ~]# usermod -d /data/sftp/mysftp mysftp 5)配置sshd_config,這個配置里的sftp要嚴格按照下面的配置來操作,否則重啟sshd服務后,會造成sftp登錄成功,但ssh遠程登錄失敗的現象! 修改或添加下面配置。可以直接下載sshd_config配置進行使用。下載地址:https://pan.baidu.com/s/1o3ILTiC7nClF9NxCmS8YHw 提取密碼:de3x [root@sftp-test01 ~]# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak2 [root@sftp-test01 ~]# vim /etc/ssh/sshd_config ...... #Subsystem sftp /usr/libexec/openssh/sftp-server Subsystem sftp internal-sftp Match Group sftp ChrootDirectory /data/sftp/%u ForceCommand internal-sftp AllowTcpForwarding no X11Forwarding no 設定Chroot目錄權限 [root@sftp-test01 ~]# chown root:sftp /data/sftp/mysftp [root@sftp-test01 ~]# chmod 755 /data/sftp/mysftp 6)建立SFTP用戶登入后可寫入的目錄 按照上面設置后,在重啟sshd服務后,用戶mysftp已經可以登錄。但使用chroot指定根目錄后,根應該是無法寫入的,所以要新建一個目錄供mysftp上傳文件。 這個目錄所有者為mysftp,所有組為sftp,所有者有寫入權限,而所有組無寫入權限。 [root@sftp-test01 ~]# mkdir /data/sftp/mysftp/upload [root@sftp-test01 ~]# chown mysftp:sftp /data/sftp/mysftp/upload [root@sftp-test01 ~]# chmod 755 /data/sftp/mysftp/upload 7)關閉防火墻 [root@sftp-test01 ~]# /etc/init.d/iptables stop [root@sftp-test01 ~]# setenforce 0 setenforce: SELinux is disabled [root@sftp-test01 ~]# cat /etc/sysconfig/selinux ....... SELINUX=disabled 8)重啟sshd服務 [root@sftp-test01 ~]# service sshd restart Stopping sshd: [ OK ] Starting sshd: [ OK ] 9)驗證sftp環境(如對sftp做白名單限制,則就是對sshd做白名單限制。可以在iptables里限制sshd的端口,也可以在/etc/hosts.allow里對sshd服務做限制) 如下顯示,這說明SFTP已經搭建成功(如果ssh是非22端口,比如是6666端口,則連接命令:sftp -o port=6666 mysftp@172.16.51.191) [root@sftp-test01 ~]# sftp mysftp@172.16.51.191 Connecting to 172.16.51.191... The authenticity of host '172.16.51.191 (172.16.51.191)' can't be established. RSA key fingerprint is c01d3b4a54a76f47:c7. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '172.16.51.191' (RSA) to the list of known hosts. mysftp@172.16.51.191's password: sftp> ls upload sftp> cd upload sftp> ls 10)使用FileZilla FTP Client連接SFTP服務器 輸入主機IP地址172.16.51.191、用戶名mysftp、密碼mysftp、端口(默認為22端口)連接SFTP服務器。 連接后,默認的路徑是/data/sftp/mysftp 二、sftp-test02服務器需要和上面sftp-test01一樣的操作! 三、sftp-test01和sftp-test02兩臺機器的/data/sftp目錄做實時同步(rsync+inotify) 考慮到數據完整性和安全性,實施單向實時同步,即從sftp-test01機器的/data/sftp實時同步到sftp-test02的data/sftp 操作如下: 1)在目標服務器sftp-test02上的部署過程 安裝配置rsync服務端 [root@sftp-test02 ~]# yum install rsync xinetd [root@sftp-test02 ~]# vim /etc/xinetd.d/rsync ...... disable = no ...... 啟動xineted服務 [root@sftp-test02 ~]# /etc/init.d/xinetd start Starting xinetd: [ OK ] 創建/etc/rsyncd.conf文件 [root@sftp-test02 ~]# vim /etc/rsyncd.conf [root@sftp-test02 ~]# cat /etc/rsyncd.conf log file = /var/log/rsyncd.log pidfile = /var/run/rsyncd.pid lock file = /var/run/rsync.lock secrets file = /etc/rsync.pass motd file = /etc/rsyncd.Motd [sftp_upload] path = /data/sftp comment = sftp_upload uid = root gid = sftp port=873 use chroot = no read only = no list = no max connections = 200 timeout = 600 auth users = RSYNC_USER hosts allow = 172.16.51.191 注意:權限不要搞錯了! 上面填寫的uid是root,gid是sftp,是因為/data/sftp/ [root@sftp-test02 ~]# ll -d /data/sftp drwxr-xr-x 3 root root 4096 Nov 21 05:21 /data/sftp [root@sftp-test02 ~]# ll /data/sftp/ total 4 drwxr-xr-x 3 root sftp 4096 Nov 21 07:28 mysftp 創建用戶認證文件 [root@sftp-test02 ~]# vim /etc/rsync.pass RSYNC_USER:123456@rsync 設置文件權限,即rsyncd.conf和rsync.pass認證文件都是600權限! [root@sftp-test02 ~]# chmod 600 /etc/rsyncd.conf [root@sftp-test02 ~]# chmod 600 /etc/rsync.pass 重啟rsync服務 [root@sftp-test02 ~]# /etc/init.d/xinetd restart Stopping xinetd: [ OK ] Starting xinetd: [ OK ] [root@sftp-test02 ~]# lsof -i:873 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME xinetd 3797 root 5u IPv6 16264 0t0 TCP *:rsync (LISTEN) 2)在源服務器172.16.51.191上的部署過程 [root@sftp-test01 ~]# yum install rsync xinetd [root@sftp-test01 ~]# vim /etc/xinetd.d/rsync ...... disable = no ...... [root@sftp-test01 ~]# /etc/init.d/xinetd start Starting xinetd: [ OK ] [root@sftp-test01 ~]# lsof -i:873 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME xinetd 3444 root 5u IPv6 15917 0t0 TCP *:rsync (LISTEN) 創建同步的密碼文件,這個文件名可以跟服務端的認證文件不一樣,但是里面的密碼必須一致!用于rsync同步命令中。 不過,最好兩邊的文件設置成一樣,便于管理 [root@sftp-test01 ~]# cat /etc/rsync.pass 123456@rsync 設置rsync.pass密碼文件為600權限 [root@sftp-test01 ~]# chmod 600 /etc/rsync.pass 查看服務器內核是否支持inotify,出現下面的內容,說明服務器內核支持inotify [root@sftp-test01 ~]# ll /proc/sys/fs/inotify total 0 -rw-r--r-- 1 root root 0 Nov 21 08:12 max_queued_events -rw-r--r-- 1 root root 0 Nov 21 08:12 max_user_instances -rw-r--r-- 1 root root 0 Nov 21 08:12 max_user_watches 注意:Linux下支持inotify的內核最小為2.6.13,可以輸入命令:uname -a查看內核 CentOS 5.X 內核為2.6.18,默認已經支持inotify [root@sftp-test01 ~]# uname -a Linux sftp-test01 2.6.32-696.13.2.el6.x86_64 #1 SMP Thu Oct 5 2116 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux 下面開始安裝inotify-tools [root@sftp-test01 ~]# yum install make gcc gcc-c++ [root@sftp-test01 ~]# cd /usr/local/src/ [root@sftp-test01 src]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz [root@sftp-test01 src]# tar zxvf inotify-tools-3.14.tar.gz [root@sftp-test01 src]# cd inotify-tools-3.14 [root@sftp-test01 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify [root@sftp-test01 inotify-tools-3.14]# make && make install 發現已經成功安裝inotify-tools了 [root@sftp-test01 inotify-tools-3.14]# ll -d /usr/local/inotify/ drwxr-xr-x 6 root root 4096 Nov 21 08:14 /usr/local/inotify/ 設置系統環境變量 [root@sftp-test01 inotify-tools-3.14]# vim /etc/profile ....... export PATH=$PATH:/usr/local/inotify/bin [root@sftp-test01 inotify-tools-3.14]# source /etc/profile 添加庫文件 [root@sftp-test01 inotify-tools-3.14]# vim /etc/ld.so.conf ...... /usr/local/inotify/lib [root@sftp-test01 inotify-tools-3.14]# ldconfig 修改inotify默認參數(inotify默認內核參數值太小) 查看系統默認參數值 [root@sftp-test01 inotify-tools-3.14]# sysctl -a | grep max_queued_events fs.inotify.max_queued_events = 16384 [root@sftp-test01 inotify-tools-3.14]# sysctl -a | grep max_user_watches fs.inotify.max_user_watches = 8192 fs.epoll.max_user_watches = 796344 [root@sftp-test01 inotify-tools-3.14]# sysctl -a | grep max_user_instances fs.inotify.max_user_instances = 128 [root@sftp-test01 inotify-tools-3.14]# sysctl -w fs.inotify.max_queued_events="99999999" fs.inotify.max_queued_events = 99999999 [root@sftp-test01 inotify-tools-3.14]# sysctl -w fs.inotify.max_user_watches="99999999" fs.inotify.max_user_watches = 99999999 [root@sftp-test01 inotify-tools-3.14]# sysctl -w fs.inotify.max_user_instances="65535" fs.inotify.max_user_instances = 65535 參數說明: max_queued_events: inotify隊列最大長度,如果值太小,會出現"** Event Queue Overflow **"錯誤,導致監控文件不準確 max_user_watches: 要同步的文件包含多少目錄,可以用:find /Data/xqsj_upload -type d | wc -l 統計這些源目錄下的目錄數,必須保證max_user_watches值大于統計結果(這里/Data/xqsj_upload為同步的源文件目錄) max_user_instances: 每個用戶創建inotify實例最大值 接著執行同步操作: 在源服務器上執行rsync首次全量同步的操作(加--delete參數,保持目標目錄和源目錄下文件絕對一致) [root@sftp-test01 ~]# rsync -avH --port=873 --progress --delete /data/sftp/ RSYNC_USER@172.16.51.192::sftp_upload --password-file=/etc/rsync.pass 待第一次rsync全量同步完成后,就進行rsync+inotify實時同步腳本操作。 實時同步腳本里添加的是--delete-before參數,而不是--delete參數(第一次全量同步時rsync用的參數),二者區別: --delete參數:表示rsync同步前,暴力刪除目標目錄中的所有文件,然后再執行同步操作。 --delete-before參數:表示rsync同步前,會先對目標目錄進行一次掃描檢索,刪除目標目錄中對比源目錄的多余文件,然后再執行同步操作。顯然比--delete參數安全些。 [root@sftp-test01 data]# cd /data/script/ [root@sftp-test01 script]# vim sftp_data_rsync.sh #!/bin/bash SRCDIR=/data/sftp/ USER=RSYNC_USER IP=172.16.51.192 DESTDIR=sftp_upload /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $SRCDIR | while read file do /usr/bin/rsync -avH --port=873 --progress --delete-before $SRCDIR $USER@$IP::$DESTDIR --password-file=/etc/rsync.pass echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1 done [root@sftp-test01 script]# chmod 755 sftp_data_rsync.sh [root@sftp-test01 script]# nohup sh sftp_data_rsync.sh & //按ctrl+c結束 [1] 8807 [root@sftp-test01 script]# ps -ef|grep inotify root 8808 8807 0 22:55 pts/0 00:00:00 /usr/local/inotify/bin/inotifywait -mrq --timefmt %d/%m/%y %H:%M --format %T %w%f%e -e close_write,modify,delete,create,attrib,move /data/sftp/ root 8811 8451 0 22:55 pts/0 00:00:00 grep inotify 這樣,sftp-test01機器的/data/sftp目錄下的文件就會自動實時同步到sftp-test02機器的/data/sftp目錄下 注意:這是單向實時同步!如果要想做雙向實時同步!那就需要在sftp-test02機器上再做個inotify監控腳本(同時,sftp-test01也要做個rsyncd.conf文件) 四、SFTP結合Keepalived做雙機高可用 1)下載安裝Keepalived(兩臺機器同樣操作) [root@sftp-test01 ~]# cd /usr/local/src/ [root@sftp-test01 src]# wget http://www.keepalived.org/software/keepalived-1.3.2.tar.gz [root@sftp-test01 src]# tar -zvxf keepalived-1.3.2.tar.gz [root@sftp-test01 src]# cd keepalived-1.3.2 [root@sftp-test01 keepalived-1.3.2]# ./configure && make && make install [root@sftp-test01 keepalived-1.3.2]# cp /usr/local/src/keepalived-1.3.2/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/ [root@sftp-test01 keepalived-1.3.2]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ [root@sftp-test01 keepalived-1.3.2]# mkdir /etc/keepalived [root@sftp-test01 keepalived-1.3.2]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ [root@sftp-test01 keepalived-1.3.2]# cp /usr/local/sbin/keepalived /usr/sbin/ [root@sftp-test01 keepalived-1.3.2]# echo "/etc/init.d/keepalived start" >> /etc/rc.local 2)配置Keepalived.conf文件 [root@sftp-test01 keepalived-1.3.2]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak [root@sftp-test01 keepalived-1.3.2]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { wangshibo@wangshibo.com } notification_email_from notice@wangshibo.com smtp_server smtp.wangshibo.com smtp_connect_timeout 30 router_id master-node } vrrp_script chk_sftp_port { script "/data/chk_sftp.sh" interval 2 weight -5 fall 2 rise 1 } vrrp_instance VI_1 { state MASTER interface eth0 mcast_src_ip 172.16.51.191 virtual_router_id 51 priority 101 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.16.51.193 } track_script { chk_sftp_port } } sftp-test02服務器作為backup端的Keepalived.conf配置如下: [root@sftp-test02 keepalived-1.3.2]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak [root@sftp-test02 keepalived-1.3.2]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { wangshibo@wangshibo.com } notification_email_from notice@wangshibo.com smtp_server smtp.wangshibo.com smtp_connect_timeout 30 router_id slave-node } vrrp_script chk_sftp_port { script "/data/chk_sftp.sh" interval 2 weight -5 fall 2 rise 1 } vrrp_instance VI_1 { state BACKUP interface eth0 mcast_src_ip 172.16.51.192 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.16.51.193 } track_script { chk_sftp_port } } 編寫sftp監控腳本(兩臺機器都要寫) [root@sftp-test01 keepalived-1.3.2]# vim /data/chk_sftp.sh #!/bin/bash counter=$(/etc/init.d/sshd status|grep running|wc -l) if [ "${counter}" = "0" ]; then /etc/init.d/sshd start sleep 2 counter=$(/etc/init.d/sshd status|grep running|wc -l) if [ "${counter}" = "0" ]; then /etc/init.d/keepalived stop fi fi [root@sftp-test01 keepalived-1.3.2]# chmod 755 /data/chk_sftp.sh 3)兩臺機器都要啟動Keepalived [root@sftp-test01 ~]# /etc/init.d/keepalived start [root@sftp-test02 ~]# /etc/init.d/keepalived start 啟動后發現sftp-test01已經有了vip資源 [root@sftp-test01 ~]# ip addr 1: lo:mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 02cb:8303 brd ffffff:ff inet 172.16.51.191/24 brd 172.16.51.255 scope global eth0 inet 172.16.51.193/32 scope global eth0 inet6 fe80:cbffad03/64 scope link valid_lft forever preferred_lft forever 4)高可用測試: -> 先關閉sftp-test01機器的Keepalived服務,發現vip資源就會自動漂到sftp-test02機器上繼續提供服務。 當sftp-test01機器的Keepalived服務恢復后,vip資源就會自動搶占回來。 -> 關閉sftp-test01機器的ssh服務,通過腳本會自動啟動ssh服務,當啟動失敗后,會強制關閉Keepalived服務,從而實現vip資源的漂移! 注意: 在FileZilla客戶端里使用172.16.51.193的vip進行連接。可以在FileZilla客戶端的"文件"里建立"新站點",協議”SFTP 登陸類型:正常
======================================================
發現上面雙向實時同步的高可用方案實施后,文件上傳到ftp目錄下的狀態有點問題,有的文件上傳后大小變化嚴重以至文件損壞,無法打開!
后面將雙向實時同步策略關閉,往單個機器上傳文件就沒問題,判斷是rsync+inotify雙向實時同步造成的。
調整后的新方案: 編寫一個監控vip資源的腳本,當vip在哪臺機器上時,就做這臺機器到另一臺的rsync單向同步操作,并且后臺一直運行這個腳本(通過循環語句保證腳本一直運行) 放棄原來的rsync+inotify雙向實時同步腳本! 腳本內容如下: 停止之前的rsync+inotify實時監控腳本,然后做兩臺機器的相互信任關系。 1)第一臺機器sftp-test01的操作 [root@sign-test01 ~]# cat /data/script/sftp_vip_monit.sh #!/bin/bash while [ "1" = "1" ] do NUM=`ip addr|grep 172.16.51.193|wc -l` if [ $NUM -eq 0 ];then echo "vip is not at this server" >/dev/null 2>&1 fi if [ $NUM -eq 1 ];then /usr/bin/rsync -e "ssh -p22" -avpgolr --progress --delete-before /data/sftp/mysftp/ root@172.16.51.192:/data/sftp/mysftp/ fi done [root@sign-test01 ~]# chmod 755 /data/script/sftp_vip_monit.sh [root@sign-test01 ~]# nohup sh /data/script/sftp_vip_monit.sh & //按ctrl+c結束 [root@sign-test01 ~]# ps -ef|grep monit root 10581 22167 0 19:42 pts/0 00:00:00 grep monit root 15113 1 8 17:15 ? 00:13:00 sh sftp_vip_monit.sh 2)第二臺機器sftp-test02 [root@sign-test02 ~]# cat /data/script/sftp_vip_monit.sh #!/bin/bash while [ "1" = "1" ] do NUM=`ip addr|grep 172.16.51.193|wc -l` if [ $NUM -eq 0 ];then echo "vip is not at this server" >/dev/null 2>&1 fi if [ $NUM -eq 1 ];then /usr/bin/rsync -e "ssh -p22" -avpgolr --progress --delete-before /data/sftp/mysftp/ root@172.16.51.191:/data/sftp/mysftp/ fi done [root@sign-test02 ~]# chmod 755 /data/script/sftp_vip_monit.sh [root@sign-test02 ~]# nohup sh /data/script/sftp_vip_monit.sh & //按ctrl+c結束 [root@sign-test02 ~]# ps -ef|grep monit root 10581 22167 0 19:42 pts/0 00:00:00 grep monit root 15113 1 8 17:15 ? 00:13:00 sh sftp_vip_monit.sh
=================創建sftp只讀賬號================
原則上來說,sftp賬號登錄后只能限定到其用戶家目錄下,即不能遍歷除其家目錄下之外的任何其他目錄! sftp添加只讀賬號,這里我采用的方法如下(有些復雜,但經測試可用): sftp-test01和sftp-test02兩個節點機操作一樣 [root@sftp-test01 ~]# useradd -g sftp -s /bin/false readftp [root@sftp-test01 ~]# passwd readftp [root@sftp-test01 ~]# mkdir /data/sftp/readftp [root@sftp-test01 ~]# usermod -d /data/sftp/readftp readftp [root@sftp-test01 ~]# chown root:sftp /data/sftp/readftp [root@sftp-test01 ~]# chmod 755 /data/sftp/readftp # 注意將readftp下的upload目錄權限設置成寫賬號mysftp權限,這樣readftp賬號登錄后就只有讀權限。 [root@sftp-test01 ~]# mkdir /data/sftp/readftp/upload [root@sftp-test01 ~]# chown mysftp:sftp /data/sftp/readftp/upload 由于上面已經編寫了兩個節點機關于/data/sftp/mysftp/目錄的實時同步腳本sftp_vip_monit.sh 下面再在兩臺機器上編寫:本機/data/sftp/mysftp/upload/到/data/sftp/readftp/upload/目錄的實時同步腳本readftp_monit.sh 1)sftp-test01節點上 [root@sftp-test01 ~]# cd /data/script/ [root@sftp-test01 script]# vim readftp_monit.sh #!/bin/bash while [ "1" = "1" ] do /usr/bin/rsync -e "ssh -p22" -avpgolr --delete-before /data/sftp/mysftp/upload/ /data/sftp/readftp/upload/ done [root@sftp-test01 script]# nohup sh readftp_monit.sh & #按ctrl+c結束 [root@sftp-test01 script]# ps -ef|grep monit root 5285 17061 0 11:37 pts/0 0007 sh readftp_monit.sh root 22713 17061 0 10:42 pts/0 0029 sh sftp_vip_monit.sh root 28893 17061 0 12:00 pts/0 0000 grep monit 2)sftp-test02節點上 [root@sftp-test02 ~]# cd /data/script/ [root@sftp-test02 script]# vim readftp_monit.sh #!/bin/bash while [ "1" = "1" ] do /usr/bin/rsync -e "ssh -p22" -avpgolr --delete-before /data/sftp/mysftp/upload/ /data/sftp/readftp/upload/ done [root@sftp-test02 script]# nohup sh readftp_monit.sh & #按ctrl+c結束 [root@sftp-test02 script]# ps -ef|grep monit root 5285 17061 0 11:37 pts/0 0007 sh readftp_monit.sh root 22713 17061 0 10:42 pts/0 0029 sh sftp_vip_monit.sh root 28893 17061 0 12:00 pts/0 0000 grep monit 以上操作做,就能保證mysftp為寫賬號,readftp為只讀賬號,思路: a)通過mysftp賬號登錄sftp服務后,可以進行上傳、創建,刪除和下載的讀寫操作,操作的文件存在在/data/sftp/mysftp/upload目錄下,然后實時同步到 /data/sftp/readftp/upload目錄下。 b)通過readftp賬號登錄sfto服務器,只能進行下載的只讀操作。由于/data/sftp/mysftp/upload為mysftp權限,沒有寫權限。而且就算可以進行寫操作,寫 之后的文件放在/data/sftp/mysftp/upload目錄下也會被覆蓋掉(因為跟mysftp/upload單向實時同步的),也即寫操作失敗!
=================只讀賬號目錄權限的坑===================
根據上面的操作完成后, 只讀賬號readftp登錄后, 對于上傳的目錄下的文件看不到了!! 這是問什么呢??
這是因為readftp只讀賬號登錄后的/data/sftp/readftp/upload/下上傳的目錄權限不夠導致的! 應該賦予755權限.
[root@localhost ~]# ll /data/sftp/readftp/upload/
total 72
drwxr--r-- 2 rbn sftp 4096 Feb 28 13:23 20190225
drwxr--r-- 2 rbn sftp 4096 Feb 27 12:05 20190226
-rwxr--r-- 1 rbn sftp 54 Feb 26 16:52 dir.qdp.mvfc_appo_acct_dtl.20190224.000.00.i
-rwxr--r-- 1 rbn sftp 54 Feb 26 13:51 dir.qdp.mvfc_appo_acct_dtl.20190225.000.00.i
通過/data/script/readftp_monit.sh腳本可知, /data/sftp/readftp/upload下的文件目錄權限都是同步/data/sftp/mysftp/upload的.
所以正確做法: 保證/data/sftp/mysftp/upload下的目錄一直是755權限
[root@sftp-test01 script]# vim readftp_monit.sh
#!/bin/bash
while [ "1" = "1" ]
do
/usr/bin/rsync -e "ssh -p22" -avpgolr --delete-before /data/sftp/mysftp/upload/ /data/sftp/readftp/upload/
/bin/chmod -R 755 /data/sftp/mysftp
done
調整后, 只讀賬號readftp登錄后,上傳的目錄權限應該是755就可以了
[root@localhost ~]# ll /data/sftp/readftp/upload/
total 72
drwxr-xr-x2 rbn sftp 4096 Feb 28 13:23 20190225
drwxr-xr-x2 rbn sftp 4096 Feb 27 12:05 20190226
-rwxr-xr-x 1 rbn sftp 54 Feb 26 16:52 dir.qdp.mvfc_appo_acct_dtl.20190224.000.00.i
-rwxr-xr-x 1 rbn sftp 54 Feb 26 13:51 dir.qdp.mvfc_appo_acct_dtl.20190225.000.00.i
================sftp日常運維維護命令===============
cd 路徑 更改到遠程目錄的路徑 lcd 路徑 更改到本地目錄的路徑 chgrp group path 將文件path的組更改為group chmod mode path 將文件path的權限更改為mode chown owner path 將文件path的屬主更改為owner exit 退出 sftp help 顯示這個幫助文本 get 遠程路徑 下載文件 ln existingpath linkpath 符號鏈接遠程文件 ls [選項] [路徑] 顯示遠程目錄列表 lls [選項] [路徑] 顯示本地目錄列表 mkdir 路徑 創建遠程目錄 lmkdir 路徑 創建本地目錄 mv oldpath newpath 移動遠程文件 open [用戶@]主機[:端口] 連接到遠程主機 put 本地路徑 上傳文件 pwd 顯示遠程工作目錄 lpwd 打印本地工作目錄 quit 退出 sftp rmdir 路徑 移除遠程目錄 lrmdir 路徑 移除本地目錄 rm 路徑 刪除遠程文件 lrm 路徑 刪除本地文件 symlink existingpath linkpath 符號鏈接遠程文件 version 顯示協議版本 =============================================================================== sftp mysftp@192.168.10.191 #ssh是默認22端口的連接方法 sftp -o port=6666 mysftp@192.168.10.191 #ssh是6666非默認端口的連接方法
審核編輯:黃飛
-
服務器
+關注
關注
12文章
9335瀏覽量
86133 -
防火墻
+關注
關注
0文章
420瀏覽量
35692 -
伺服器
+關注
關注
1文章
152瀏覽量
19571 -
CentOS
+關注
關注
0文章
79瀏覽量
13832
原文標題:Centos下SFTP雙機高可用環境部署記錄
文章出處:【微信號:magedu-Linux,微信公眾號:馬哥Linux運維】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
Linux開發_CentOS7.4服務器搭建NFS、NGINX服務器,安裝ffmpeg、Qt環境
![Linux開發_<b class='flag-5'>CentOS</b>7.4服務器搭建NFS、NGINX服務器,安裝ffmpeg、Qt<b class='flag-5'>環境</b>](https://file1.elecfans.com/web2/M00/8C/BA/wKgZomSw4NSAck05AAKmOJunv3U401.jpg)
在Linux上配置SFTP服務
Oralce 11.2.0.4 RAC for CENTOS 7.x 最佳安裝部署手冊
![Oralce 11.2.0.4 RAC for <b class='flag-5'>CENTOS</b> 7.x 最佳安裝<b class='flag-5'>部署</b>手冊](https://file.elecfans.com/web2/M00/49/AF/pYYBAGKhvE6AH6jNAAAM844rRiA946.png)
通過安裝該Linux-HA軟件可以實現Linux雙機系統的高可用性解決方案
![通過安裝該Linux-HA軟件可以實現Linux<b class='flag-5'>雙機</b>系統的<b class='flag-5'>高</b><b class='flag-5'>可用</b>性解決方案](https://file1.elecfans.com//web2/M00/A7/19/wKgZomUMQl2AJwgrAAAQMzoOPH8714.png)
CentOS下配置Apache+PHP+MySQL
![<b class='flag-5'>CentOS</b><b class='flag-5'>下</b>配置Apache+PHP+MySQL](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
評論