創建新用戶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