{"id":65124,"date":"2024-08-03T12:59:23","date_gmt":"2024-08-03T11:59:23","guid":{"rendered":"https:\/\/ekiwi-blog.de\/65124\/linux-mount-windows-share-with-script\/"},"modified":"2024-08-03T14:08:04","modified_gmt":"2024-08-03T13:08:04","slug":"linux-mount-windows-share-with-script","status":"publish","type":"post","link":"https:\/\/ekiwi-blog.de\/en\/65124\/linux-mount-windows-share-with-script\/","title":{"rendered":"Linux: Mount Windows share with script"},"content":{"rendered":"<p>Integrate Samba share with shell script in Linux. This is how it works!<\/p>\n<p><!--more--><\/p>\n<p>I have installed Linux on my laptop on the road. I wanted to mount my NAS shares here, if required. So I wanted to have a small shell script that integrates and mounts the share.<\/p>\n<p>Let&#8217;s get started!<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of content<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ekiwi-blog.de\/en\/65124\/linux-mount-windows-share-with-script\/#Video\" >Video<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ekiwi-blog.de\/en\/65124\/linux-mount-windows-share-with-script\/#Installation_cifs-utils\" >Installation cifs-utils<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ekiwi-blog.de\/en\/65124\/linux-mount-windows-share-with-script\/#mount_script\" >mount script<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ekiwi-blog.de\/en\/65124\/linux-mount-windows-share-with-script\/#Unmount_Script\" >Unmount Script<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Video\"><\/span>Video<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/OpuLmLZKpRQ?si=YKXIWAGzPU0lrZ7u\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Installation_cifs-utils\"><\/span>Installation cifs-utils<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If not yet installed:<\/p>\n<pre>\r\nsudo apt-get install cifs-utils\r\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"mount_script\"><\/span>mount script<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Now we come to the mount script. Simply copy this and adapt it in a text editor of your choice. In the first part we find the configuration, here we enter the name of the share, the mount directory, user name and password.<\/p>\n<pre>\r\n#!\/bin\/bash\r\n\r\n# Configuration variables\r\nSHARE=\"\/\/server\/share\"\r\nMOUNT_POINT=\"\/mnt\/samba\"\r\nUSERNAME=\"your_username\"\r\nPASSWORD=\"your_password\"\r\n\r\n# Check if the mount point directory exists\r\nif [ ! -d \"$MOUNT_POINT\" ]; then\r\n  echo \"Creating mount point directory: $MOUNT_POINT\"\r\n  mkdir -p \"$MOUNT_POINT\"\r\nfi\r\n\r\n# Mount the Samba share with write access\r\necho \"Mounting the Samba share...\"\r\nsudo mount -t cifs \"$SHARE\" \"$MOUNT_POINT\" -o username=\"$USERNAME\",password=\"$PASSWORD\",rw,uid=$(id -u),gid=$(id -g),file_mode=0777,dir_mode=0777\r\n\r\n# Check if the mount was successful\r\nif [ $? -eq 0 ]; then\r\n  echo \"Samba share mounted successfully at $MOUNT_POINT\"\r\nelse\r\n  echo \"Failed to mount Samba share\"\r\nfi\r\n<\/pre>\n<p>The script also creates the folder, if it does not exist, in which the share is mounted.<\/p>\n<p>After saving the file, we make it executable.<\/p>\n<pre>\r\nchmod +x mount_samba.sh\r\n<\/pre>\n<p>We can then execute the script and mount the share.<\/p>\n<pre>\r\n.\/mount_samba.sh\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/08\/share_1.png\" alt=\"\" width=\"476\" height=\"204\" class=\"aligncenter size-full wp-image-65120\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/08\/share_1.png 476w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2024\/08\/share_1-300x129.png 300w\" sizes=\"auto, (max-width: 476px) 100vw, 476px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Unmount_Script\"><\/span>Unmount Script<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We can also create a script to unmount the share. Same procedure.<\/p>\n<pre>\r\n#!\/bin\/bash\r\n\r\n# Configuration variable\r\nMOUNT_POINT=\"\/mnt\/samba\"\r\n\r\n# Unmount the Samba share\r\necho \"Unmounting the Samba share...\"\r\nsudo umount \"$MOUNT_POINT\"\r\n\r\n# Check if the unmount was successful\r\nif [ $? -eq 0 ]; then\r\n  echo \"Samba share unmounted successfully from $MOUNT_POINT\"\r\nelse\r\n  echo \"Failed to unmount Samba share\"\r\nfi\r\n<\/pre>\n<p>Here, too, we make the script executable again.<\/p>\n<pre>\r\nchmod +x unmount_samba.sh\r\n<\/pre>\n<p>We can then execute the script.<\/p>\n<pre>\r\n.\/unmount_samba.sh\r\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>Integrate Samba share with shell script in Linux. This is how it works!<\/p>\n","protected":false},"author":1,"featured_media":18426,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1555],"tags":[1682,1598,1558],"class_list":["post-65124","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-en","tag-linux-en","tag-network","tag-windows-en"],"_links":{"self":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/65124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/comments?post=65124"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/65124\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media\/18426"}],"wp:attachment":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media?parent=65124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/categories?post=65124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/tags?post=65124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}