Dùng JSch để thực thi lệnh shell trên server Unix qua SSH
2 phút đọc22/05/2025
Reading Time: 2 minutesHôm nay chúng ta sẽ cùng tìm hiểu một ví dụ về JSch. Chúng ta có thể sử dụng thư viện này để tạo kết nối SSH trong Java. Cụ thể, dưới đây là về một chương trình dùng JSch để kết nối tới máy chủ có hỗ trợ SSH và thực thi các lệnh shell.

Ví dụ dùng JSch
Bạn có thể tải về file JAR của JSch từ trang web chính thức của nó. Bạn cũng có thể lấy chúng bằng cách khai báo dependency Maven như dưới đây.
com.jcraft
jsch
0.1.53
Dưới đây là một ví dụ JSch đơn giản để chạy lệnh “ls -ltr” trên máy chủ.
import java.io.InputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class JSchExampleSSHConnection {
/**
* JSch Example Tutorial
* Java SSH Connection Program
*/
public static void main(String[] args) {
String host="ssh.journaldev.com";
String user="sshuser";
String password="sshpwd";
String command1="ls -ltr";
try{
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session=jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command1);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect();
byte[] tmp=new byte[1024];
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
System.out.print(new String(tmp, 0, i));
}
if(channel.isClosed()){
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
channel.disconnect();
session.disconnect();
System.out.println("DONE");
}catch(Exception e){
e.printStackTrace();
}
}
}
Đây là một ví dụ khá đơn giản về cách dùng JSch để tạo kết nối SSH trong Java. Hãy để lại bình luận nếu gặp bất kỳ vấn đề nào chạy thử ví dụ ở trên.
Bạn có thể tải file JSch jar từ trang web chính thức của nó. Nếu gặp bất cứ vấn đề gì khi làm theo ví dụ trên, đừng ngần ngại mà để lại comment bên dưới nhé.
Về tác giả

Bao TranWeb Developer
I’m passionate about web development and sharing my insights through articles, with over 8 years of experience. I hope these sharings inspire you and help build a strong web development community.
@#@
Tôi đam mê phát triển web và chia sẻ những hiểu biết của mình thông qua các bài viết, với hơn 8 năm kinh nghiệm. Tôi hy vọng những chia sẻ này sẽ truyền cảm hứng cho các bạn và giúp xây dựng một cộng đồng phát triển web mạnh mẽ.
Cập nhật thông tin mới nhấtNhận các thông tin mới nhất về mối đe dọa, báo cáo an ninh mạng từ CyStack về hòm thư điện tử của bạn
{"success":true,"head":"<title>Dùng JSch để thực thi lệnh shell trên server Unix qua SSH - CyStack Tutorial</title>\n<meta name=\"description\" content=\"Đọc bài viết này để tìm hiểu cách sử dụng thư viện JSch trong Java để thiết lập kết nối SSH an toàn đến server Unix và thực thi các lệnh shell từ xa.\"/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-video-preview:-1, max-image-preview:large\"/>\n<link rel=\"canonical\" href=\"https://blog.cystack.org/tutorial/2025/05/22/dung-jsch-de-chay-lenh-shell/\" />\n<meta property=\"og:locale\" content=\"en_US\" />\n<meta property=\"og:type\" content=\"article\" />\n<meta property=\"og:title\" content=\"Dùng JSch để thực thi lệnh shell trên server Unix qua SSH - CyStack Tutorial\" />\n<meta property=\"og:description\" content=\"Đọc bài viết này để tìm hiểu cách sử dụng thư viện JSch trong Java để thiết lập kết nối SSH an toàn đến server Unix và thực thi các lệnh shell từ xa.\" />\n<meta property=\"og:url\" content=\"https://blog.cystack.org/tutorial/2025/05/22/dung-jsch-de-chay-lenh-shell/\" />\n<meta property=\"og:site_name\" content=\"CyStack Tutorial\" />\n<meta property=\"article:tag\" content=\"vi\" />\n<meta property=\"article:section\" content=\"Java\" />\n<meta property=\"og:updated_time\" content=\"2025-06-05T09:36:10+07:00\" />\n<meta property=\"og:image\" content=\"https://s2.cystack.net/tutorial/22120215/Dung-JSch-de-thuc-thi-lenh-shell-tren-server-Unix-qua-SSH-1024x538.png\" />\n<meta property=\"og:image:secure_url\" content=\"https://s2.cystack.net/tutorial/22120215/Dung-JSch-de-thuc-thi-lenh-shell-tren-server-Unix-qua-SSH-1024x538.png\" />\n<meta property=\"og:image:width\" content=\"1024\" />\n<meta property=\"og:image:height\" content=\"538\" />\n<meta property=\"og:image:alt\" content=\"Thực thi lệnh shell trên server Unix qua SSH\" />\n<meta property=\"og:image:type\" content=\"image/png\" />\n<meta property=\"article:published_time\" content=\"2025-05-22T12:04:03+07:00\" />\n<meta property=\"article:modified_time\" content=\"2025-06-05T09:36:10+07:00\" />\n<meta name=\"twitter:card\" content=\"summary_large_image\" />\n<meta name=\"twitter:title\" content=\"Dùng JSch để thực thi lệnh shell trên server Unix qua SSH - CyStack Tutorial\" />\n<meta name=\"twitter:description\" content=\"Đọc bài viết này để tìm hiểu cách sử dụng thư viện JSch trong Java để thiết lập kết nối SSH an toàn đến server Unix và thực thi các lệnh shell từ xa.\" />\n<meta name=\"twitter:image\" content=\"https://s2.cystack.net/tutorial/22120215/Dung-JSch-de-thuc-thi-lenh-shell-tren-server-Unix-qua-SSH-1024x538.png\" />\n<meta name=\"twitter:label1\" content=\"Written by\" />\n<meta name=\"twitter:data1\" content=\"Bao Tran\" />\n<meta name=\"twitter:label2\" content=\"Time to read\" />\n<meta name=\"twitter:data2\" content=\"1 minute\" />\n<script type=\"application/ld+json\" class=\"rank-math-schema\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https://blog.cystack.org/tutorial/#person\",\"name\":\"CyStack Tutorial\"},{\"@type\":\"WebSite\",\"@id\":\"https://blog.cystack.org/tutorial/#website\",\"url\":\"https://blog.cystack.org/tutorial\",\"name\":\"CyStack Tutorial\",\"publisher\":{\"@id\":\"https://blog.cystack.org/tutorial/#person\"},\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https://s2.cystack.net/tutorial/22120215/Dung-JSch-de-thuc-thi-lenh-shell-tren-server-Unix-qua-SSH.png\",\"url\":\"https://s2.cystack.net/tutorial/22120215/Dung-JSch-de-thuc-thi-lenh-shell-tren-server-Unix-qua-SSH.png\",\"width\":\"2400\",\"height\":\"1260\",\"caption\":\"Th\\u1ef1c thi l\\u1ec7nh shell tr\\u00ean server Unix qua SSH\",\"inLanguage\":\"en-US\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://blog.cystack.org/tutorial/2025/05/22/dung-jsch-de-chay-lenh-shell/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":\"1\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":\"2\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial/category/java/\",\"name\":\"Java\"}},{\"@type\":\"ListItem\",\"position\":\"3\",\"item\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/05/22/dung-jsch-de-chay-lenh-shell/\",\"name\":\"D\\u00f9ng JSch \\u0111\\u1ec3 th\\u1ef1c thi l\\u1ec7nh shell tr\\u00ean server Unix qua SSH\"}}]},{\"@type\":\"WebPage\",\"@id\":\"https://blog.cystack.org/tutorial/2025/05/22/dung-jsch-de-chay-lenh-shell/#webpage\",\"url\":\"https://blog.cystack.org/tutorial/2025/05/22/dung-jsch-de-chay-lenh-shell/\",\"name\":\"D\\u00f9ng JSch \\u0111\\u1ec3 th\\u1ef1c thi l\\u1ec7nh shell tr\\u00ean server Unix qua SSH - CyStack Tutorial\",\"datePublished\":\"2025-05-22T12:04:03+07:00\",\"dateModified\":\"2025-06-05T09:36:10+07:00\",\"isPartOf\":{\"@id\":\"https://blog.cystack.org/tutorial/#website\"},\"primaryImageOfPage\":{\"@id\":\"https://s2.cystack.net/tutorial/22120215/Dung-JSch-de-thuc-thi-lenh-shell-tren-server-Unix-qua-SSH.png\"},\"inLanguage\":\"en-US\",\"breadcrumb\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/05/22/dung-jsch-de-chay-lenh-shell/#breadcrumb\"}},{\"@type\":\"Person\",\"@id\":\"https://blog.cystack.org/tutorial/author/baotran/\",\"name\":\"Bao Tran\",\"url\":\"https://blog.cystack.org/tutorial/author/baotran/\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https://secure.gravatar.com/avatar/0cdd33c02ec4a531fcf557b9c1ccc276df0c69031b2382c959bcf335248e840c?s=96&d=mm&r=g\",\"url\":\"https://secure.gravatar.com/avatar/0cdd33c02ec4a531fcf557b9c1ccc276df0c69031b2382c959bcf335248e840c?s=96&d=mm&r=g\",\"caption\":\"Bao Tran\",\"inLanguage\":\"en-US\"}},{\"@type\":\"BlogPosting\",\"headline\":\"D\\u00f9ng JSch \\u0111\\u1ec3 th\\u1ef1c thi l\\u1ec7nh shell tr\\u00ean server Unix qua SSH - CyStack Tutorial\",\"keywords\":\"JSch\",\"datePublished\":\"2025-05-22T12:04:03+07:00\",\"dateModified\":\"2025-06-05T09:36:10+07:00\",\"articleSection\":\"Java\",\"author\":{\"@id\":\"https://blog.cystack.org/tutorial/author/baotran/\",\"name\":\"Bao Tran\"},\"publisher\":{\"@id\":\"https://blog.cystack.org/tutorial/#person\"},\"description\":\"\\u0110\\u1ecdc b\\u00e0i vi\\u1ebft n\\u00e0y \\u0111\\u1ec3 t\\u00ecm hi\\u1ec3u c\\u00e1ch s\\u1eed d\\u1ee5ng th\\u01b0 vi\\u1ec7n JSch trong Java \\u0111\\u1ec3 thi\\u1ebft l\\u1eadp k\\u1ebft n\\u1ed1i SSH an to\\u00e0n \\u0111\\u1ebfn server Unix v\\u00e0 th\\u1ef1c thi c\\u00e1c l\\u1ec7nh shell t\\u1eeb xa.\",\"name\":\"D\\u00f9ng JSch \\u0111\\u1ec3 th\\u1ef1c thi l\\u1ec7nh shell tr\\u00ean server Unix qua SSH - CyStack Tutorial\",\"@id\":\"https://blog.cystack.org/tutorial/2025/05/22/dung-jsch-de-chay-lenh-shell/#richSnippet\",\"isPartOf\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/05/22/dung-jsch-de-chay-lenh-shell/#webpage\"},\"image\":{\"@id\":\"https://s2.cystack.net/tutorial/22120215/Dung-JSch-de-thuc-thi-lenh-shell-tren-server-Unix-qua-SSH.png\"},\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https://blog.cystack.org/tutorial/2025/05/22/dung-jsch-de-chay-lenh-shell/#webpage\"}}]}</script>\n"}