centos 6.5 配置sftp步骤

创建新用户testsftp,禁止ssh登录

useradd -s /sbin/nologin -M testsftp

修改密码:

passwd testsftp

建立目录,此处注意sftp要求目录必须是750或者是755,不能设置成777,否则会出现登录不上的情况

mkdir /opt/testsftp

chown root:root /opt/testsftp

chmod 755 /opt/testsftp

修改ssh配置文件,在最后一行添加如下

vim /etc/ssh/sshd_config

# Subsystem     sftp    /usr/libexec/openssh/sftp-server
Subsystem sftp internal-sftp
Match User testsftp
ChrootDirectory /opt/testsftp
ForceCommand internal-sftp
X11Forwarding no
AllowTcpForwarding no

重启服务

service sshd restart

测试工具:
winscp即可

测试代码转载:https://blog.csdn.net/u014204541/article/details/80007316
 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.SocketTimeoutException;
import java.util.Properties;
import java.util.Vector;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class JSchUtils {
    private static JSch jsch;
    private static Session session = null;
    private static Channel channel = null;
    /**
     * 连接到指定的IP
     * 
     * @throws JSchException
     */
    public static ChannelSftp connect(String ftpUserName, String ftpPassword, String ftpHost, int ftpPort) throws Exception {
        jsch = new JSch();// 创建JSch对象
        session = jsch.getSession(ftpUserName, ftpHost, ftpPort);// 根据用户名、主机ip、端口号获取一个Session对象
        session.setPassword(ftpPassword);// 设置密码

        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);// 为Session对象设置properties
        session.setTimeout(1000*30);// 设置超时
        session.connect();// 通过Session建立连接
        System.out.println("Session connected.");
        channel = session.openChannel("sftp"); // 打开SFTP通道
        channel.connect(); // 建立SFTP通道的连接
        System.out.println("Connected successfully to ftpHost = " + ftpHost + ",as ftpUserName = " + ftpUserName);
        return (ChannelSftp) channel;
    }

    /**
     * 关闭连接
     */
    public static void close() {
        if (channel != null) {
            channel.disconnect();
            System.out.println("关闭channel成功");
        }
        if (session != null) {
            session.disconnect();
            System.out.println("关闭session成功");
        }
    }

    /**
     * 执行相关的命令,
     * 但是部分情况不可用
     * 
     * @throws JSchException
     */
    public static void execCmd(String command) throws JSchException {
        BufferedReader reader = null;

        try {
            if (command != null) {
                channel = session.openChannel("exec");
                ((ChannelExec) channel).setCommand(command);
                // ((ChannelExec) channel).setErrStream(System.err);
                channel.connect();

                InputStream in = channel.getInputStream();
                reader = new BufferedReader(new InputStreamReader(in));
                String buf = null;
                while ((buf = reader.readLine()) != null) {
                    System.out.println(buf);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSchException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            channel.disconnect();
        }
    }

    /**
     * 上传文件
     *
     * @param directory
     *            上传的目录
     * @param uploadFile
     *            要上传的文件
     * @param sftp
     * @throws JSchException
     * @throws SftpException
     * @throws FileNotFoundException
     */
    public static void upload(String directory, String uploadFile) throws Exception {
        ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
        channelSftp.cd(directory);
        File file = new File(uploadFile);
        channelSftp.put(new FileInputStream(file), file.getName());       
        System.out.println("上传: " +uploadFile + "成功!");
    }

    /**
     * 下载文件
     * 
     * @param src
     * @param dst
     * @throws JSchException
     * @throws SftpException
     */
    public static void download(String src, String dst) throws Exception {
        // src linux服务器文件地址,dst 本地存放地址
        ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
        channelSftp.connect();
        channelSftp.get(src, dst);
        System.out.println("下载文件:"+src+"成功");
        channelSftp.quit();
    }

    /**
     * 删除文件
     *
     * @param directory
     *            要删除文件所在目录
     * @param deleteFile
     *            要删除的文件
     * @param sftp
     * @throws SftpException
     * @throws JSchException
     */
    public void delete(String directory, String deleteFile) throws Exception {
        ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
        channelSftp.cd(directory);
        channelSftp.rm(deleteFile);
        System.out.println("删除成功");
    }

    /**
     * 列出目录下的文件
     *
     * @param directory
     *            要列出的目录
     * @param sftp
     * @return
     * @throws SftpException
     * @throws JSchException
     */
    @SuppressWarnings("rawtypes")
    public Vector listFiles(String directory) throws Exception {
        ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
        return channelSftp.ls(directory);
    }

    public static void main(String[] args) {
        try {
            // 1.连接到指定的服务器
            connect("cgbchina", "cgbchina0409", "card.expopeking.com", 8821);

           /* // 2.执行相关的命令
            execCmd("grep '160622150549943666' /data/apps/2017-07-07.log >> /data/20170707.txt");

            // 3.下载文件
            download("/data/nginx_log.20170707.txt", "D:\\temp");*/

            // 4.关闭连接
            close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

需要jar包:

 

jsch-0.1.54.jar

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值