How to specify a password when mounting a smb share with gio?

8,262

Solution 1

Create a file in your home directory. For example at /home/morbius/.servercreds

Into that file enter your credentials. You must specify the user name, workgroup, and password - one per line

<username>
<workgroup>
<password>

Then your gio command would look like this:

gio mount smb://<server>/<share> < /home/morbius/.servercreds

Solution 2

To avoid creating a new file, you can pipe the input into gio.

echo -e "USERNAME\nWORKGROUP\nPASSWORD\n" | gio mount smb://<server>/<share>

echo -e tells echo to allow special characters, and \n after each field is like pressing the enter key.

Share:
8,262

Related videos on Youtube

Tyras
Author by

Tyras

Updated on September 18, 2022

Comments

  • Tyras
    Tyras over 1 year

    I'm working on a software that needs to mount a smb/cifs share automatically.

    The catch is that the share must be mounted using the user's login and password, and, AFAIK and for reasons completly unknown to me, looks like gio doesn't have an option to specify the password in the command line, only the user. If the user requires a password, it asks for it interactively. While this works for someone calling gio from the terminal, for a development... it's a pain.

    I've already tried to call

    gio mount smb://<user>:<password>@<server>/<share>/
    

    but it just ignores the password and asks for it in the terminal. The (poor) documentation does not show any way to specify the password. Is waiting the password prompt and "emulating" an input the only way to set it?

    Using mount -t cifs is not an option, since it requires root/sudo, and the software I'm working on isn't supposed to require elevated privileges.

    • Jos
      Jos about 6 years
      This is a long shot, but I had more or less the same trouble to automatically mount a Webdav share. The mount program kept asking me for the password. It turned out that the password contained a "%" and that caused the mount to fail, unless I typed in the terminal.
    • Tyras
      Tyras about 6 years
      I indeed use '%' in the passwords, but I just tried with an user with a password containing only numbers and letters and it still asked the password. But thanks anyway!
  • Erich Kuester
    Erich Kuester almost 4 years
    Even useful on Fedora ... Instead of '/home/morbius/' you can use '$HOME'
  • joonas.fi
    joonas.fi over 3 years
    Just to clarify: this is pretty hacky, as it just simulates user entering these details and pressing enter between them. If the command asks new questions and/or in different order, this breaks down.
  • Admin
    Admin almost 2 years
    Please note that giving a password on the command line is security concern because command line parameters of running commands can be seen with different tools. Better store it in a temp file only readable by yourself and pipe it into the gio command.