How to Set Root Directory in Apache Mina Sshd Server in Java

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");
        }
    }
}

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";
                }
            };
        };
    });

By default, it takes the root path from the System property called user.dir. To change this, you can override getVirtualUserDir() in NativeFileSystemView and  Both solutions are deprecated in 1.0 in- How to Set Root Directory in Apache Mina Sshd Server in Java How to override getVirtualUserDir() in Apache Mina sshd-core version 0.14.0 In 0.14.0 the following worked fine: sshd.setFileSystemFactory(new NativeFileSystemFactory() {@Override. public FileSystemView createFileSystem(final Session session)


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.

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. Apache Mina SSHD :: Core 1.1.0 API. JavaScript is disabled on your browser. Frame Alert. This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client. Link to Non-frame version.


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

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. Embedding SSHD in 5 minutes. SSHD is designed to be easily embedded in your application as an SSH server. SSH Server needs to be configured before it can be started. Essentially, there are three steps for creating the Server. Create an instance of SshServer class; Configure the Server; Start the Server; Lets look at all these steps.


Apache Mina SSHD 1.0.0 set user directory & mapping protected Path computeRootDir(Session session) throws IOException { String username = session. Apache Mina SSHD 0.6.0 Sources (.zip) PGP SHA; Binary distributions: Apache Mina SSHD 0.6.0 Binary (.tar.gz) PGP SHA; Apache Mina SSHD 0.6.0 Binary (.zip) PGP SHA; Release Notes. Apache Mina SSHD 0.6.0 contains a few enhancements and bug-fixes. Please report any feedback to users@mina.apache.org. Bug SSHD-19 - NullPointerException during SSHD


How to Set Root Directory in Apache Mina Sshd Server in Java. sshd. getUsername(); // create home if does not exist if (isCreateHome()) { String homeRoot  The Apache MINA uses a system that allows for the developer of the MINA-base application to use their own logging system. SLF4J. MINA employs the Simple Logging Facade for Java (SLF4J). You can find information on SLF4J here. This logging utility allows for the implementation of any number of logging systems.


AbstractLoggingBean. org.apache.sshd.server.subsystem.sftp. Fields inherited from class org.apache.sshd.common.util.logging. Called prior to creating a directory void, linked(ServerSession session, Path source, Path target, boolean symLink, Throwable thrown) Methods inherited from class java.lang.Object. The thing is that I want everyone to log into the server and to be put in his home directory and that directory to be the root directory for that user. I want to do this with ftpd, sshd and apache. All services for that user must let him in his home directory and he shouldn't be able to go anywhere else (excepting his subdirectories).