简体   繁体   中英

how to chown using JSch sftp?

I'm using JSch to sftp files. After upload, I change permissions on the file. But how to change the owner? There are no good examples out there that I could find. I want to

chown Administrator:Administrators filename.exe

like you would do in linux but the JSch chown command takes an integer, rather than a string for the owner:group. What kind of nonsense is that?

Here is some of my code

    jSch = new JSch();
    if (useKey) jSch.addIdentity(privateKey);
    session = jSch.getSession( user, host, port );
    if (!useKey) {
        session.setPassword(pass);
        session.setConfig( "PreferredAuthentications", "password" );
    }
    session.setConfig("StrictHostKeyChecking", "no");
    session.connect(FTP_TIMEOUT);
    channel = session.openChannel("sftp");
    sftp = (ChannelSftp) channel;
    sftp.connect(FTP_TIMEOUT);

    sftp.put(fis,file.getName());
    String permissions = "744";     
    int octal = Integer.parseInt(permissions,8);    //jsh uses octal, not decimal
    if (file.getName().endsWith(".exe")) { //make exe files executable
        sftp.chmod(octal,file.getName());
        sftp.chown(this-is-an-integer-not-a-string, file.getName());
    }

JSCH implements SFTP version 3 . SFTP version 3 uses numeric values for a file's owner and group. A client that wants to change the owner or group for a file on the server has to know the correct numeric values to request.

Note that the OpenSSH SFTP server also implements SFTP version 3. This is the most widely used SFTP server, and it's probably the one that you're using. As far as I can tell, it doesn't support a chown operation that takes the owner name as a string.

It looks like SFTP version 4 and later support passing owner and group as strings. You might be able to find a third-party (ie, commercial) SFTP client and server that supports this feature.

The method chown is using with UID, please refer to javadocs . You are trying to compile a permission into a UID, that's wrong. Please find the UID of the user your want to change and then pass to the chown function. If your server is linux you can refer here

Below is the complete code which changed the file's permission to 777 or -rwxrwxrwx

JSch jsch = new JSch();
Session session = jsch.getSession("root", "192.168.1.5", 22);
session.setPassword("your_password");
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp cFTP = (ChannelSftp) channel;
jsch.setConfig("StrictHostKeyChecking", "no");
String targetFile = "/var/lib/asterisk/sounds/en/"+ "foo.mp3";

cFTP.chmod(0777, targetFile); //This will change the permissions to 777

cFTP.disconnect();
session.disconnect();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM