Search code examples
javafile-permissionsjsch

Change file permissions with SFTP or SCP uing JSch


I have a user account, but on trying using this am getting exception for rssh. Is there any possibility to give permission of directory using other way other than ChannelExec. Based on the exception I come to know this account can't use ChannelExec to give permission of a directory or file. Thus is there any other way this account could give file permission without getting access to this user account for rssh. Please give you thoughts.

Code :

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

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

Output :

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.

Solution

  • There's no need to use the "exec" channel for this.

    Use the ChannelSftp.chmod:

    public void chmod(int permissions, String path)
    

    Note that the method takes the permissions as an integer. So you cannot use 777, as that's an octal representation of the permissions.

    An equivalent decimal representation is 511 (= 7*8^2 + 7*8^1 + 7*8^0).

    See also Decimal to Octal Conversion.


    Though the ChannelSftp.chmod cannot set permissions recursively. The SFTP protocol does not support recursive operations of any kind.