June 26, 2015

Cài đặt Chroot SFTP Linux trên Debian/Ubuntu

SFTP server là một loại file server, dùng giao thức bảo mật SSH, mỗi người dùng được tạo một tài khoản lưu trữ trên server. Người dùng có thể truy cập vào server bằng tài khoản của mình bằng FTP hoặc SSH, làm việc trong folder riêng của mình và không gây ảnh hưởng đến các user khác.

Khi chưa cấu hình Chroot: (user có thể truy cập vào /etc )


$sftpjohn@192.168.1.200
john@192.168.1.200'spassword:
Connectedto192.168.1.200.
sftp>pwd
Remoteworkingdirectory:/home/john
sftp>cd/etc/


1. Cài đặt OpenSSH Server:
Trước tiên ta kiểm tra OpenSSH server đã được cài đặt hay chưa:

#dpkg--list|grepopenssh-server
iiopenssh-server 1:6.0p1-4+deb7u1amd64secureshell(SSH)server,forsecureaccessfromremotemachines
Trường hợp này thì OpenSSH đã được cài đặt, nếu OpenSSH chưa được cài đặt thì sẽ không xuất hiện dòngii openssh-server, để cài đặt ta dùng lệnh:


#apt-getinstallopenssh-server


2. Tạo Group chứa user truy cập SFTP:
Tạo 1 nhóm mới tên sftpusers, chỉ có các user trong nhóm này mới được phép truy cập SFTP:

#groupaddsftpusers
Tạo mới 1 user john và add user bày vào group sftpuser:

#useradd -gsftpusers -mjohn
Nếu user đã tồn tại, ta chỉ việc add nó vào group:

#usermod -gsftpusersjohn


3. Tạo thư mục làm việc cho mỗi user:
Trong thư mục home của mỗi user mới tạo (/home/username), ta sẽ tạo ra các folder mới để user sẽ làm việc (lưu trữ dữ liệu) trực tiếp trên folder này:
Tạo 2 folder (public_html và logs) cho user john:

#mkdir/home/john/{public_html,logs} -p


4. Chmod folder home của user:
Ủy quyền truy cập folder /home/username cho root và chmod 755 cho folder này, các folder bên trong folder này sẽ được ủy quyền cho chinh user truy cập (john):


#chownjohn:sftpusers/home/john/ -R
#chownroot:sftpusers/home/john/
#ls -l/home/
drwxr-xr-x4root sftpusers4096Jun2703:01john
#ls -l/home/john/
total8
drwxr-xr-x2johnsftpusers4096Jun2703:01logs
drwxr-xr-x2johnsftpusers4096Jun2703:01public_html


5. Cấu hình SFTP Server:
Mở file /etc/ssh/sshd_config, tìm đến dòng bên dưới và comment dòng đó lại:

#Subsystemsftp/usr/lib/openssh/sftp-server
Chèn Subsystem sftp internal-sftp vào bên dưới, ta sẽ đc:

#Subsystemsftp/usr/lib/openssh/sftp-server
Subsystemsftpinternal-sftp
Kế tiếp, ta chèn thêm đoạn sau đây vào cuối file:
Trong đó: sftpusers là tên Group vừa tạo:


Matchgroupsftpusers
ChrootDirectory/home/%u
X11Forwardingno
AllowTcpForwardingno
Lưu file sshd_config, thoát và khởi động lại ssh:


#servicesshrestart
[ok]RestartingOpenBSDSecureShellserver:sshd.


6. Kiểm tra:
Ta có thể thử kết nối SSH hoặc FTP đến Server từ máy Client:
Kết nối SFTP Server bằng Filezilla CLient:


Kiểm tra kết nối SFTP từ terminal:

$sftpjohn@192.168.1.200
john@192.168.1.200'spassword:
Connectedto192.168.1.200.
sftp>pwd
Remoteworkingdirectory:/
sftp>ls
logs public_html
sftp>
Kết nối đến Server bằng SSH:
Hãy chắc chắn rằng trong file file /etc/ssh/sshd_config đã unconment dòng:

UseLoginyes


$sshjohn@192.168.1.200
john@192.168.1.200'spassword:
Linuxdebian3.2.0-4-amd64#1SMPDebian3.2.57-3+deb7u2x86_64
TheprogramsincludedwiththeDebianGNU/Linuxsystemarefreesoftware;
theexactdistributiontermsforeachprogramaredescribedinthe
individualfilesin/usr/share/doc/*/copyright.
DebianGNU/LinuxcomeswithABSOLUTELYNOWARRANTY,totheextent
permittedbyapplicablelaw.
$ls
logspublic_html
$pwd
/home/john


Chúc thành công!

0 comments:

Post a Comment