15

I use Apache Mina Sshd API to start up a local SFTP server in java.In SFTP client i use Jcraft jsch API to create my SFTP client.I successfully start up a server.The problem is that i want to write some unit test cases to check whether client can put some files into server's root directory. Currently my SFTP server doesn't have any root directory.So i would like to know that is there is any approach to set server's root directory.

Eg: C:\sftp How can i set this path as my server root directory.so then client can read and write files to it every time connect with the server.Thank you.

public class SftpServerStarter {

    private SshServer sshd;
    private final static Logger logger = 
        LoggerFactory.getLogger(SftpServerStarter.class);

    public void start(){
        sshd = SshServer.setUpDefaultServer();
        sshd.setPort(22);
        sshd.setHost("localhost");
        sshd.setPasswordAuthenticator(new MyPasswordAuthenticator());
        sshd.setPublickeyAuthenticator(new MyPublickeyAuthenticator());
        sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());
        sshd.setSubsystemFactories(
            Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
        sshd.setCommandFactory(new ScpCommandFactory());

        try {
            logger.info("Starting ...");
            sshd.start();
            logger.info("Started");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            logger.info("Can not Start Server");
        }
    }
}
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
gihan-maduranga
  • 4,381
  • 5
  • 41
  • 74
  • possible duplicate of [How to start sftp server programmatically in java](http://stackoverflow.com/questions/29531342/how-to-start-sftp-server-programmatically-in-java) – Daniel Newtown Apr 09 '15 at 06:34
  • @DanielNewtown Hi mate, I know this is a long while back.. could you please re-post that article in the link? it returns a 404 now as we speak. Thanks in advance – James Jiang Jun 02 '16 at 12:24

3 Answers3

10

In more recent sshd versions you can use org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory and supply it to the SshServer instance via method setFileSystemFactory.

Snippet:

VirtualFileSystemFactory fileSystemFactory = new VirtualFileSystemFactory();
fileSystemFactory.setDefaultHomeDir("home.directory");
sshd.setFileSystemFactory(fileSystemFactory)
dbaltor
  • 2,737
  • 3
  • 24
  • 36
Stefan Winter
  • 101
  • 1
  • 3
  • This now requires a Path object, e.g.: fileSystemFactory.setDefaultHomeDir(Files.createTempDirectory("sshd_")) – rees Jun 25 '20 at 02:40
6

In Default it takes the root path from System property called user.dir

Inorder to change this, you can override getVirtualUserDir() in NativeFileSystemView and return your path.

    sshd.setFileSystemFactory(new NativeFileSystemFactory() {
        @Override
        public FileSystemView createFileSystemView(final Session session) {
            return new NativeFileSystemView(session.getUsername(), false) {
                @Override
                public String getVirtualUserDir() {
                    return  "C:\\MyRoot";
                }
            };
        };
    });
Tharaka Deshan
  • 1,349
  • 3
  • 15
  • 30
  • Hi Tharaka, I am working on the same type of use case now, but i want to create a totally virtual file system for the user when he logs in using winscp or any other sftp client. I am trying to create or set the root directory for the user such as "/xyz" and the sub directories such as "/abc" instead of any system file directory (eg: "C:\"). Any idea how to achieve that? – SSharma2203 Jan 22 '20 at 06:00
3

You can also follow following link to know about how to set root directory in Apache Mina sshd SFTP server with different sshd-core version.

<dependency>
        <groupId>org.apache.sshd</groupId>
        <artifactId>sshd-core</artifactId>
        <version>0.10.0</version>
    </dependency>

into

<dependency>
        <groupId>org.apache.sshd</groupId>
        <artifactId>sshd-core</artifactId>
        <version>0.14.0</version>
    </dependency>

How to override getVirtualUserDir() in Apache Mina sshd-core version 0.14.0

Community
  • 1
  • 1
gihan-maduranga
  • 4,381
  • 5
  • 41
  • 74
  • Thank you very much! That's the only solution available for 0.14.0 version of sshd-core. – Brain Sep 16 '15 at 06:52