You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins跨Unix服务器传输.sh文件至sudo用户目录失败排查

Fixing sudo: scp: command not found When Transferring Files via Jenkins

Let's work through this issue step by step—here's what's going on and how to fix it:

Why You're Seeing This Error

When you run commands with sudo, it resets your shell's PATH environment variable to a restricted, secure default. This default path often doesn’t include all the directories your regular user has access to. So even though scp works fine for your normal user, sudo can’t locate the executable because it’s only looking in a limited set of system folders.

Solution 1: Use the Absolute Path to scp (Quick Fix)

The fastest way to resolve this is to tell sudo exactly where the scp command lives on your source server a. First, run this as your regular user to find the absolute path:

which scp

You’ll get an output like /usr/bin/scp or /bin/scp. Update your Jenkins command to use this full path with sudo:

sudo /usr/bin/scp *.sh sell4@u:/usr/app/TomcatDomain/ScoringTools_ACCDomain04/

This bypasses the restricted PATH issue because you’re directly pointing sudo to the scp executable.

Solution 2: Update sudo's Secure Path (For Long-Term Use)

If you’ll need to run scp via sudo regularly, you can adjust the sudoers file to include the directory with scp in its secure path. Never edit this file directly—use visudo instead, which validates your changes to avoid breaking sudo functionality:

sudo visudo

Look for a line starting with Defaults secure_path. Modify it to include the directory where scp is located (from the which scp output). For example:

Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

Save and exit, and now sudo will automatically find scp without needing the absolute path every time.

Bonus: Verify Target Directory Permissions

Once you fix the scp not found error, double-check that the target directory on server u is writable by the sell4 user. On server u, have sell4 run:

ls -ld /usr/app/TomcatDomain/ScoringTools_ACCDomain04/

Ensure the permissions include write access for sell4 (either via user ownership, group membership, or global permissions). If not, you may need to adjust permissions with chmod or add sell4 to the group that owns the directory.

内容的提问来源于stack exchange,提问作者Wolfgang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:16:55