Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
        }
    }
}
like image 901
gihan-maduranga Avatar asked Apr 01 '15 11:04

gihan-maduranga


People also ask

What is the Apache MINA SSHD library?

The Apache MINA SSHDlibrary is a great way to provide SSHD client or server functionality in your application. Unfortunately, it is not especially well documented and recent refactoring in the project has left most of the few usage examples you can find obsolete. This repoprovides a very simple server example to help you get started.

How do I connect to a remote SSH server with Java?

In this tutorial, we'll show how to establish a connection to a remote SSH server with Java using the JSch and Apache MINA SSHD libraries. In our examples, we'll first open the SSH connection, then execute one command, read the output and write it to the console, and, finally, close the SSH connection.

How to change the document root for my Apache web server?

To change the document root for your Apache web server simply open the corresponding file with your favourite text editor and search for DocumentRoot. # # DocumentRoot: The directory out of which you will serve your # documents.

What Maven dependencies does Apache MINA SSHD have?

Apache Mina SSHD has a lot of available Maven dependencies (for example FTP, SCP, etc.), but since we're using only core functionalities, we'll only add core Maven dependency to our project:


1 Answers

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)
like image 134
Stefan Winter Avatar answered Oct 07 '22 15:10

Stefan Winter