Permission denied using JSch

穿精又带淫゛_ 提交于 2019-12-07 18:06:49

问题


I'm trying to retrieve some files from sftp server using JSch but I'm getting the following error.

3: Permission denied
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846)
at com.jcraft.jsch.ChannelSftp._realpath(ChannelSftp.java:2340)
at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:342)
at com.company.common.sftp.impl.managedFile.moveFiles(managedFile.java:712)

Here is the code:

private List<String> moveFiles(String prefixFileName, String path) {
    Session session = getSession();
    Channel channel = connect(session);
    ChannelSftp channelSftp = null;
    try {
        channelSftp = (ChannelSftp)channel;
        channelSftp.cd(_workingDir);
    ...
    }
    ...
    finally {
        channel.disconnect();
        session.disconnect();
    }
}

public Session getSession() {              
    Session session = null;
    JSch jsch = new JSch();
    session = jsch.getSession(_user,_server,_port);
    session.setPassword(_password);
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", _strictHostKeyChecking);
    session.setConfig(config);
    session.connect();
    return session;
 }

public static Channel connect(Session session) {
  Channel channel = null;
  channel = session.openChannel("sftp");
  channel.connect();
  return channel;
}

_workingDir is a property with the following value: /user_files. Both folders (source and destination) are on a Windows server and all the privileges was granted to any user. But for some reason it doesn't let me change the current directory in the source (remote) server.

Any idea?

UPDATE: The Sftp server is freeFTPd and using a sftp client (like Filezilla) I can move files without problems


回答1:


Probably /user_files is an absolute path.

Try ./user_files for a relative path to the user's home directory.

In Filezilla, it is C:\user_files on the remote side ?




回答2:


The same issue was encountered by me and verifying the followings fixed my issue:

  1. Path of the file we are trying to retrieve doesn't exist. So, please make sure that file actually exists under the directory that your application is accessing to retrieve the file.
  2. Make sure SFTP user that you are using in the application to make the connection is configured and active in SFTP.
  3. If above both points are valid in your scenario then try after rebooting the SFTP service.



回答3:


I have also meet the problem when using jsch in docker container, and fix the issue after chowning with the folder and starting sftp with root :

  1. Start up the application with root ,not the other user, because it will work with the dir "/var/run" owned by root;
  2. Because of (1), you should have a path owned by root to be the data path, you'd better give full privileges to the path:chmod 777 /yourpath;
  3. Mention: you should mkdir when logining the sftp server with "sftp -P 22 admin@127.0.0.1", not to do it in data path, because the path will owned by root or other system user, but not the user in your sftp system, so that you can not upload files(because permission denied);
  4. After changing all of above, I can finally upload and download files on the server.


来源:https://stackoverflow.com/questions/33243278/permission-denied-using-jsch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!