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

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)

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


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