要?jiǎng)?chuàng)建一個(gè)用戶并限制其訪問特定目錄,您需要執(zhí)行以下步驟:
用戶假設(shè)是res_user
目錄假設(shè)是usr/local/code/resource
-
創(chuàng)建新的用戶賬戶:
- 在終端中以root身份登錄。
- 運(yùn)行以下命令來創(chuàng)建新用戶:
sudo adduser username - 將
username替換為您要?jiǎng)?chuàng)建的用戶名,當(dāng)前假設(shè)是res_user。 - 運(yùn)行
passwd res_user,并按照提示輸入密碼和其他相關(guān)信息
-
創(chuàng)建用戶組:
- 使用
groupadd restricted命令創(chuàng)建用戶組 - 并將res_user放到用戶組中
usermod -g restricted res_user
- 使用
-
更改目錄的權(quán)限和所有權(quán):
chown res_user:restricted /usr/local/code/resourcechmod g+rx /usr/local/code/resource
-
用戶限制使用shell模式:
usermod -s /bin/rbash res_user
設(shè)置
/etc/ssh/sshd_config配置文件,在最后加上
Match Group restricted
ForceCommand internal-sftp
ChrootDirectory /usr/local/code
PermitTunnel no
AllowAgentForwarding no
AllowTcpForwarding no
X11Forwarding no
- 重啟
sshd
systemctl restart sshd