大网数据广告活动
服务器运维问答

centos 7 execute remote script from local

· 52CE 我爱测

I'm working on a script to sync a folder from Windows to a Linux server with Rsync and I manage to get it work with git bash and the rsync executable. In Centso I added the syncinguser to the sudoer -> syncinguser ALL=NOPASSWD:/u…

问题原文

原帖链接 · DevWizard · CC BY-SA 4.0

I'm working on a script to sync a folder from Windows to a Linux server with Rsync and I manage to get it work with git bash and the rsync executable.

In Centso I added the syncinguser to the sudoer -> syncinguser ALL=NOPASSWD:/usr/bin/rsync to allow the execution of Rsync, how can I allow the same user to run a .sh file to complete the sync process or a php command?

I want to sync a Laravel application so I need to run the command php artisan cache:clear and php artisan config:cache after the sync has finished.

I would also like to change owner:group like chown -R user:psacln * but I'm quite happy with the fact that doesn't change the rights on the folders E.g: 0777 permission on the public folder.

This is my script in windows so far:

rsync -rt --chown=user:group (being ignored I think because user's permissions) 
--exclude ".env" --exclude "storage" --exclude "public/storage" --progress 
--rsync-path="sudo rsync" -e "ssh -p <port>" 
'/c/path/to/local/folder/' syncinguser@<ip address>:/remote/path/

how can I add something like this after the sync:

ssh yncinguser@<ip address> php artisan cache:clear
ssh syncinguser@<ip address> php artisan config:cache
// OR
ssh syncinguser@<ip address>:/remote/path/to/completesync.sh

where completesync.sh will run the 2 php artisan command and apply recursively the owner:group of all files/folders as when they come from windows they are all root:root

I cannot connect as a root because we blocked it, you need to connect as a normal user and then switch to root.

 ssh -p <ssh port> syncinguser@<IP ADDRESSED> '/path/to/script.sh'

permissions denied, same if I connect as a normal user

已采纳解答

原帖链接 · wazoox · CC BY-SA 4.0

OK, so you need two scripts: one on the source machine, and another one on the destination machine. The script on the source machine should be something like:

rsync ... ... ... syncuser@destination:/dest/path
ssh syncuser@destination "sudo /some/path/to/completesync.sh"

And on the destination machine, the script /some/path/to/completesync.sh which contains something like this:

#!/bin/sh

php artisan cache:clear
php artisan config:cache

# whatever else you need to run as root

Be careful to have restricted rights on this script:

chown root:root /path/to/completesync.sh && chmod 700 /path/to/completesync.sh

Last, modify /etc/sudoers on the destination machine so that "syncuser" can run both rsync and your script as root:

syncuser ALL=NOPASSWD: /usr/bin/rsync, /path/to/completesync.sh

Now running the script on the source machine should complete the whole process in one operation.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-06-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。