{"id":13307,"date":"2019-04-01T11:54:19","date_gmt":"2019-04-01T11:54:19","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13307"},"modified":"2019-04-01T11:54:19","modified_gmt":"2019-04-01T11:54:19","slug":"restrict-ssh-user-access-to-certain-directory-using-chrooted-jail","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/restrict-ssh-user-access-to-certain-directory-using-chrooted-jail\/","title":{"rendered":"Restrict SSH User Access to Certain Directory Using Chrooted Jail"},"content":{"rendered":"<p>There are several reasons to\u00a0<a href=\"https:\/\/www.tecmint.com\/restrict-sftp-user-home-directories-using-chroot\/\" target=\"_blank\" rel=\"noopener\">restrict a SSH user session<\/a>\u00a0to a particular directory, especially on web servers, but the obvious one is a system security. In order to lock SSH users in a certain directory, we can use\u00a0<strong>chroot<\/strong>mechanism.<\/p>\n<p>change root (<strong>chroot<\/strong>) in Unix-like systems such as Linux, is a means of separating specific user operations from the rest of the Linux system; changes the apparent root directory for the current running user process and its child process with new root directory called a\u00a0<strong>chrooted jail<\/strong>.<\/p>\n<p>In this tutorial, we\u2019ll show you how to restrict a SSH user access to a given directory in Linux. Note that we\u2019ll run the all the commands as root, use the\u00a0<a href=\"https:\/\/www.tecmint.com\/sudoers-configurations-for-setting-sudo-in-linux\/\" target=\"_blank\" rel=\"noopener\">sudo command<\/a>\u00a0if you are logged into server as a normal user.<\/p>\n<h3>Step 1: Create SSH Chroot Jail<\/h3>\n<p><strong>1.<\/strong>\u00a0Start by creating the chroot jail using the mkdir command below:<\/p>\n<pre># mkdir -p \/home\/test\r\n<\/pre>\n<p><strong>2.<\/strong>\u00a0Next, identify required files, according to the\u00a0<strong>sshd_config<\/strong>\u00a0man page, the\u00a0<code>ChrootDirectory<\/code>\u00a0option specifies the pathname of the directory to chroot to after authentication. The directory must contain the necessary files and directories to support a user\u2019s session.<\/p>\n<p>For an interactive session, this requires at least a shell, commonly\u00a0<code>sh<\/code>, and basic\u00a0<code>\/dev<\/code>\u00a0nodes such as null, zero, stdin, stdout, stderr, and tty devices:<\/p>\n<pre># ls -l \/dev\/{null,zero,stdin,stdout,stderr,random,tty}\r\n<\/pre>\n<div id=\"attachment_24927\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Listing-Required-Files.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24927\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Listing-Required-Files.png\" alt=\"Listing Required Files\" width=\"669\" height=\"197\" aria-describedby=\"caption-attachment-24927\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24927\" class=\"wp-caption-text\">Listing Required Files<\/p>\n<\/div>\n<p><strong>3.<\/strong>\u00a0Now, create the\u00a0<code>\/dev<\/code>\u00a0files as follows using the\u00a0<strong>mknod command<\/strong>. In the command below, the\u00a0<code>-m<\/code>\u00a0flag is used to specify the file permissions bits,\u00a0<code>c<\/code>\u00a0means character file and the two numbers are major and minor numbers that the files point to.<\/p>\n<pre># mkdir -p \/home\/test\/dev\/\t\t\r\n# cd \/home\/test\/dev\/\r\n# mknod -m 666 null c 1 3\r\n# mknod -m 666 tty c 5 0\r\n# mknod -m 666 zero c 1 5\r\n# mknod -m 666 random c 1 8\r\n<\/pre>\n<div id=\"attachment_24928\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Create-Required-Files.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24928\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Create-Required-Files.png\" alt=\"Create \/dev and Required Files\" width=\"444\" height=\"183\" aria-describedby=\"caption-attachment-24928\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24928\" class=\"wp-caption-text\">Create \/dev and Required Files<\/p>\n<\/div>\n<p><strong>4.<\/strong>\u00a0Afterwards, set the appropriate permission on the chroot jail. Note that the chroot jail and its subdirectories and subfiles must be owned by\u00a0<strong>root<\/strong>\u00a0user, and not writable by any normal user or group:<\/p>\n<pre># chown root:root \/home\/test\r\n# chmod 0755 \/home\/test\r\n# ls -ld \/home\/test\r\n<\/pre>\n<div id=\"attachment_24929\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Set-Permission-on-Directory.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24929\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Set-Permission-on-Directory.png\" alt=\"Set Permissions on Directory\" width=\"490\" height=\"115\" aria-describedby=\"caption-attachment-24929\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24929\" class=\"wp-caption-text\">Set Permissions on Directory<\/p>\n<\/div>\n<h3>Step 2: Setup Interactive Shell for SSH Chroot Jail<\/h3>\n<p><strong>5.<\/strong>\u00a0First, create the\u00a0<code>bin<\/code>\u00a0directory and then copy the\u00a0<code>\/bin\/bash<\/code>\u00a0files into the\u00a0<code>bin<\/code>\u00a0directory as follows:<\/p>\n<pre># mkdir -p \/home\/test\/bin\r\n# cp -v \/bin\/bash \/home\/test\/bin\/\r\n<\/pre>\n<div id=\"attachment_24930\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Copy-Bin-Files.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24930\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Copy-Bin-Files.png\" alt=\"Copy Files to bin Directory\" width=\"469\" height=\"93\" aria-describedby=\"caption-attachment-24930\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24930\" class=\"wp-caption-text\">Copy Files to bin Directory<\/p>\n<\/div>\n<p><strong>6.<\/strong>\u00a0Now, identify bash required shared\u00a0<code>libs<\/code>, as below and copy them into the\u00a0<code>lib<\/code>\u00a0directory:<\/p>\n<pre># ldd \/bin\/bash\r\n# mkdir -p \/home\/test\/lib64\r\n# cp -v \/lib64\/{libtinfo.so.5,libdl.so.2,libc.so.6,ld-linux-x86-64.so.2} \/home\/test\/lib64\/\r\n<\/pre>\n<div id=\"attachment_24931\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Copy-Shared-Library-Files.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24931\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Copy-Shared-Library-Files.png\" sizes=\"auto, (max-width: 987px) 100vw, 987px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Copy-Shared-Library-Files.png 987w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Copy-Shared-Library-Files-768x242.png 768w\" alt=\"Copy Shared Library Files\" width=\"987\" height=\"311\" aria-describedby=\"caption-attachment-24931\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24931\" class=\"wp-caption-text\">Copy Shared Library Files<\/p>\n<\/div>\n<h3>Step 3: Create and Configure SSH User<\/h3>\n<p><strong>7.<\/strong>\u00a0Now, create the SSH user with the\u00a0<a href=\"https:\/\/www.tecmint.com\/add-users-in-linux\/\" target=\"_blank\" rel=\"noopener\">useradd command<\/a>\u00a0and set a secure password for the user:<\/p>\n<pre># useradd tecmint\r\n# passwd tecmint\r\n<\/pre>\n<p><strong>8.<\/strong>\u00a0Create the chroot jail general configurations directory,\u00a0<code>\/home\/test\/etc<\/code>\u00a0and copy the updated account files (<strong>\/etc\/passwd<\/strong>\u00a0and\u00a0<strong>\/etc\/group<\/strong>) into this directory as follows:<\/p>\n<pre># mkdir \/home\/test\/etc\r\n# cp -vf \/etc\/{passwd,group} \/home\/test\/etc\/\r\n<\/pre>\n<div id=\"attachment_24932\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Copy-Password-Files.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24932\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Copy-Password-Files.png\" alt=\"Copy Password Files\" width=\"584\" height=\"112\" aria-describedby=\"caption-attachment-24932\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24932\" class=\"wp-caption-text\">Copy Password Files<\/p>\n<\/div>\n<p><strong>Note<\/strong>: Each time you add more SSH users to the system, you will need to copy the updated account files into the\u00a0<code>\/home\/test\/etc<\/code>\u00a0directory.<\/p>\n<h3>Step 4: Configure SSH to Use Chroot Jail<\/h3>\n<p><strong>9.<\/strong>\u00a0Now, open the\u00a0<code>sshd_config<\/code>\u00a0file.<\/p>\n<pre># vi \/etc\/ssh\/sshd_config\r\n<\/pre>\n<p>and add\/modify the lines below in the file.<\/p>\n<pre>#define username to apply chroot jail to\r\n<strong>Match User tecmint<\/strong>\r\n#specify chroot jail\r\n<strong>ChrootDirectory \/home\/test<\/strong>\r\n<\/pre>\n<div id=\"attachment_24933\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Configure-SSH-Chroot-Jail.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24933\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Configure-SSH-Chroot-Jail.png\" alt=\"Configure SSH Chroot Jail\" width=\"538\" height=\"346\" aria-describedby=\"caption-attachment-24933\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24933\" class=\"wp-caption-text\">Configure SSH Chroot Jail<\/p>\n<\/div>\n<p>Save the file and exit, and restart the SSHD services:<\/p>\n<pre># systemctl restart sshd\r\nOR\r\n# service sshd restart\r\n<\/pre>\n<h3>Step 5: Testing SSH with Chroot Jail<\/h3>\n<p><strong>10.<\/strong>\u00a0At this point, test if the chroot jail setup is working as expected:<\/p>\n<pre><strong># ssh tecmint@192.168.0.10<\/strong>\r\n-bash-4.1$ ls\r\n-bash-4.1$ date\r\n-bash-4.1$ uname\r\n<\/pre>\n<div id=\"attachment_24934\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Testing-SSH-User-Chroot-Jail.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24934\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Testing-SSH-User-Chroot-Jail.png\" alt=\"Testing SSH User Chroot Jail\" width=\"468\" height=\"189\" aria-describedby=\"caption-attachment-24934\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24934\" class=\"wp-caption-text\">Testing SSH User Chroot Jail<\/p>\n<\/div>\n<p>From the screenshot above, we can see that the SSH user is locked in the chrooted jail, and can\u2019t run any external commands (ls, date, uname etc).<\/p>\n<p>The user can only execute bash and its builtin commands such as(pwd, history, echo etc) as seen below:<\/p>\n<pre><strong># ssh tecmint@192.168.0.10<\/strong>\r\n-bash-4.1$ pwd\r\n-bash-4.1$ echo \"Tecmint - Fastest Growing Linux Site\"\r\n-bash-4.1$ history\r\n<\/pre>\n<div id=\"attachment_24935\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/SSH-Builtin-Commands.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24935\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/SSH-Builtin-Commands.png\" alt=\"SSH Built-in Commands\" width=\"536\" height=\"255\" aria-describedby=\"caption-attachment-24935\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24935\" class=\"wp-caption-text\">SSH Built-in Commands<\/p>\n<\/div>\n<h3>Step 6. Create SSH User\u2019s Home Directory and Add Linux Commands<\/h3>\n<p><strong>11.<\/strong>\u00a0From the previous step, we can notice that the user is locked in the root directory, we can create a home directory for the the SSH user like so (do this for all future users):<\/p>\n<pre># mkdir -p \/home\/test\/home\/tecmint\r\n# chown -R tecmint:tecmint \/home\/test\/home\/tecmint\r\n# chmod -R 0700 \/home\/test\/home\/tecmint\r\n<\/pre>\n<div id=\"attachment_24936\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Create-SSH-User-Home-Directory.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24936\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Create-SSH-User-Home-Directory.png\" alt=\"Create SSH User Home Directory\" width=\"644\" height=\"121\" aria-describedby=\"caption-attachment-24936\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24936\" class=\"wp-caption-text\">Create SSH User Home Directory<\/p>\n<\/div>\n<p><strong>12.<\/strong>\u00a0Next, install a few user commands such as ls, date, mkdir in the\u00a0<code>bin<\/code>\u00a0directory:<\/p>\n<pre># cp -v \/bin\/ls \/home\/test\/bin\/\r\n# cp -v \/bin\/date \/home\/test\/bin\/\r\n# cp -v \/bin\/mkdir \/home\/test\/bin\/\r\n<\/pre>\n<div id=\"attachment_24937\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Add-Commands-to-SSH-User.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24937\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Add-Commands-to-SSH-User.png\" alt=\"Add Commands to SSH User\" width=\"479\" height=\"153\" aria-describedby=\"caption-attachment-24937\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24937\" class=\"wp-caption-text\">Add Commands to SSH User<\/p>\n<\/div>\n<p><strong>13.<\/strong>\u00a0Next, check the shared libraries for the commands above and move them into the chrooted jail libraries directory:<\/p>\n<pre># ldd \/bin\/ls\r\n# cp -v \/lib64\/{libselinux.so.1,libcap.so.2,libacl.so.1,libc.so.6,libpcre.so.1,libdl.so.2,ld-linux-x86-64.so.2,libattr.so.1,libpthread.so.0} \/home\/test\/lib64\/\r\n<\/pre>\n<div id=\"attachment_24938\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Copy-Shared-Libraries.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24938\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Copy-Shared-Libraries.png\" alt=\"Copy Shared Libraries\" width=\"737\" height=\"526\" aria-describedby=\"caption-attachment-24938\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24938\" class=\"wp-caption-text\">Copy Shared Libraries<\/p>\n<\/div>\n<h3>Step 7. Testing SFTP with Chroot Jail<\/h3>\n<p><strong>14.<\/strong>\u00a0Do a final test using sftp; check if the commands you have just installed are working.<\/p>\n<p>Add the line below in the\u00a0<code>\/etc\/ssh\/sshd_config<\/code>\u00a0file:<\/p>\n<pre>#Enable sftp to chrooted jail \r\nForceCommand internal-sftp\r\n<\/pre>\n<p>Save the file and exit. Then restart the SSHD services:<\/p>\n<pre># systemctl restart sshd\r\nOR\r\n# service sshd restart\r\n<\/pre>\n<p><strong>15.<\/strong>\u00a0Now, test using SSH, you\u2019ll get the following error:<\/p>\n<pre># ssh tecmint@192.168.0.10\r\n<\/pre>\n<div id=\"attachment_24939\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Test-SSH-Chroot-Jail.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24939\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Test-SSH-Chroot-Jail.png\" alt=\"Test SSH Chroot Jail\" width=\"434\" height=\"119\" aria-describedby=\"caption-attachment-24939\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24939\" class=\"wp-caption-text\">Test SSH Chroot Jail<\/p>\n<\/div>\n<p>Try using SFTP as follows:<\/p>\n<pre># sftp tecmint@192.168.0.10\r\n<\/pre>\n<div id=\"attachment_24942\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Testing-sFTP-SSH-User.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24942\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/03\/Testing-sFTP-SSH-User.png\" alt=\"Testing sFTP SSH User\" width=\"610\" height=\"249\" aria-describedby=\"caption-attachment-24942\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24942\" class=\"wp-caption-text\">Testing sFTP SSH User<\/p>\n<\/div>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/restrict-sftp-user-home-directories-using-chroot\/\" target=\"_blank\" rel=\"noopener\">Restrict SFTP Users to Home Directories Using chroot Jail<\/a><\/p>\n<p>That\u2019s it for now!. In this article, we showed you how to restrict a SSH user in a given directory (chrooted jail) in Linux. Use the comment section below to offer us your thoughts about this guide.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/restrict-ssh-user-to-directory-using-chrooted-jail\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are several reasons to\u00a0restrict a SSH user session\u00a0to a particular directory, especially on web servers, but the obvious one is a system security. In order to lock SSH users in a certain directory, we can use\u00a0chrootmechanism. change root (chroot) in Unix-like systems such as Linux, is a means of separating specific user operations from &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/restrict-ssh-user-access-to-certain-directory-using-chrooted-jail\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Restrict SSH User Access to Certain Directory Using Chrooted Jail&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13307","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13307"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13307\/revisions"}],"predecessor-version":[{"id":13308,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13307\/revisions\/13308"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}