smbmnt
is a simple shell script to mount Samba shares on Linux, making it easy to access shared folders from a remote server using configuration values stored in a YAML file. The script can also unmount shares, create mount points, and change directories to the mount point after a successful mount.
- Installation
- Usage
- Configuration
- Options
- Error Handling
- Accessing Mounted Files
- License
- Contributing
To use the smbmnt
script, you need to install yq
(YAML processor) and ensure you have the required permissions to mount and unmount network shares. The script can be used on any Linux system.
-
yq - Install
yq
for reading and parsing YAML files.- On Debian/Ubuntu-based systems:
sudo apt-get install yq
- On Arch Linux-based systems:
sudo pacman -S yq
- On Debian/Ubuntu-based systems:
-
CIFS Utils - Install
cifs-utils
to enable CIFS mounting.- On Debian/Ubuntu-based systems:
sudo apt-get install cifs-utils
- On Arch Linux-based systems:
sudo pacman -S cifs-utils
- On Debian/Ubuntu-based systems:
-
sudo - Ensure that
sudo
is installed and that you have sudo permissions to execute commands that require elevated privileges (e.g., creating directories, mounting/unmounting).- On Debian/Ubuntu-based systems:
sudo apt-get install sudo
- On Arch Linux-based systems:
sudo pacman -S sudo
- On Debian/Ubuntu-based systems:
-
Permissions - Ensure you have the necessary permissions to mount and unmount network shares.
git clone https://github.com/mohamed1242012/smbmnt.git
cd smbmnt
sudo cp smbmnt /bin
sudo cp config.yaml ~/.config/smbmnt/config.yaml
The smbmnt.sh
script is designed to mount Samba shares by reading configuration details from a YAML file. It supports multiple options and checks for missing or incorrect configurations.
smbmnt [server_name]
Replace [server_name]
with the name of the server as defined in the configuration file.
-
-u, --usage, --help
Displays usage instructions and exits.smbmnt --help
-
-v, --version
Displays the version information of the script.smbmnt --version
The configuration is stored in the config.yaml
file located in ~/.config/smbmnt/config.yaml
. Each server entry should be structured under the servers
key.
servers:
myserver:
ip: '192.168.1.30' # The IP address of the Samba server
share: 'myfiles' # The shared folder name
user: 'superuser' # The username to authenticate with the Samba server
password: '' # The password (optional, can be left empty for security reasons)
version: '2.0' # Samba version (don't edit unless necessary)
mnt: '/mnt/smbmnt' # The local mount point
dmp: 'true' # Delete mount point after unmounting (true/false)
uim: 'true' # Automatically unmount if mounted (true/false)
cd: 'true' # Change to the mount point directory after mounting (true/false)
permissions: # File and directory permissions (advanced)
file_mode: '0777'
dir_mode: '0777'
uid: '1000'
gid: '1000'
- ip: The IP address of the Samba server.
- share: The name of the shared folder on the server.
- user: The username for authentication with the Samba server.
- password: The password for authentication (optional, can be left empty for security reasons).
- version: The version of Samba to use (typically
2.0
or3.0
). - mnt: The local mount point on the system where the share will be mounted.
- dmp: If set to
true
, the mount point will be deleted after unmounting. - uim: If set to
true
, the mount point will automatically be unmounted if it is already mounted. - cd: If set to
true
, the script will change to the mount point directory after successfully mounting.
You can define multiple servers in the config.yaml
file under the servers
key. Each server should have its own unique identifier (e.g., myserver
, another_server
), and the script will use the name provided in the command to look up the corresponding configuration.
servers:
myserver:
ip: '192.168.1.30'
share: 'myfiles'
user: 'superuser'
password: ''
version: '2.0'
mnt: '/mnt/smbmnt'
dmp: 'true'
uim: 'true'
cd: 'true'
permissions:
file_mode: '0777'
dir_mode: '0777'
uid: '1000'
gid: '1000'
another_server:
ip: '192.168.1.31'
share: 'documents'
user: 'admin'
password: 'password123'
version: '3.0'
mnt: '/mnt/another_smbmnt'
dmp: 'false'
uim: 'false'
cd: 'false'
permissions:
file_mode: '0775'
dir_mode: '0775'
uid: '1001'
gid: '1001'
To mount a share from myserver, run:
smbmnt myserver
To mount a share from another_server, run:
smbmnt another_server
The script will look up the server name in the config.yaml
file, read the associated configuration, and proceed with mounting the share.
The script includes various error handling mechanisms:
- Missing Configuration: If essential fields such as
share
,ip
,user
,version
, ormnt
are missing from the configuration, the script will display an error and exit. - Mount Point Already Mounted: If the mount point is already mounted, the script will check the
uim
option to see if it should unmount the share first. - Failed Mounting: If the mount operation fails, the script will display an error message and attempt to clean up by removing the mount point.
Once the Samba share is successfully mounted, you can access the shared files through the specified mount point directory.
After running the script and mounting the Samba share, you can access the mounted files from the local mount point directory defined in the configuration file (e.g., /mnt/smbmnt
).
cd /mnt/smbmnt
You can then list the files and directories:
ls -l
If the configuration option cd
is set to true
, the script will automatically change to the mount point directory after a successful mount.
smbmnt myserver
After the mount operation completes, if cd
is set to true
, you will be automatically switched to the /mnt/smbmnt
directory. You can then use the mounted files as if they were local files.
This project is licensed under the MIT License. See the LICENSE file for more information.
Feel free to fork this repository, open issues, and submit pull requests. Contributions are welcome!