使用 JSch 使用 SFTP 或 SCP 更改文件权限

Change file permissions with SFTP or SCP uing JSch

我有一个用户帐户,但在尝试使用它时出现 rssh 异常。是否有可能使用 ChannelExec 以外的其他方式授予目录权限。基于异常我开始知道这个帐户不能使用 ChannelExec 来授予目录或文件的权限。因此,此帐户是否可以通过任何其他方式授予文件权限,而无需访问 rssh 的此用户帐户。请大家出出主意。

代码:

channelSftp = (ChannelSftp) channel;
ChannelExec channelexe = (ChannelExec) session.openChannel("exec");
channelexe.setCommand("chmod 777 -R " + depDir);                
channelexe.connect();

System.out.println("channelexe.getExitStatus:"+channelexe.getExitStatus());

输出:

channelexe.getExitStatus:-1:
This account is restricted by rssh.
Allowed commands: scp sftp

If you believe this is in error, please contact your system administrator.

不需要为此使用“exec”通道。

使用ChannelSftp.chmod:

public void chmod(int permissions, String path)

请注意,该方法将权限作为整数。所以你不能使用 777,因为那是权限的八进制表示。

等效的十进制表示是 511 (= 7*8^2 + 7*8^1 + 7*8^0).

另见 Decimal to Octal Conversion


虽然ChannelSftp.chmod不能递归设置权限。 SFTP 协议不支持任何类型的递归操作。