10

I am using a script to automatically set up a computer. I need to assign a password to the vnc server for the user, which is normally done using the vncserver command. However, it prompts for the user to enter and re-enter their password, neither of which the script is capable of doing. So, how can I set up the VNC password without an interactive prompt?

Thanks.

5 Answers 5

12

I found another way of doing this in a script (as root):

#!/bin/sh    

myuser="asimov"
mypasswd="mysecret"

mkdir /home/$myuser/.vnc
echo $mypasswd | vncpasswd -f > /home/$myuser/.vnc/passwd
chown -R $myuser:$myuser /home/$myuser/.vnc
chmod 0600 /home/$myuser/.vnc/passwd

Cheers!

3
  • 1
    Did you launch vncserver any differently after doing this? When I set the passwd file manually, I'm still prompted to specify a password when I run vncserver. Jan 30, 2017 at 13:21
  • 1
    best answer IMOH Apr 10, 2020 at 1:45
  • To clarify for others what this is doing, -f prints to STDOUT and then redirects the output to the passwd file with the >.
    – Rex Linder
    Jun 22, 2021 at 22:53
12

Was able to do it this way today (from a dockerfile at least):

RUN printf "password\npassword\n\n" | vncpasswd
2
  • 2
    I think this is the simplest cleanest way. Works in a script and in my case I'm using su and it works there as well, i.e. su -l -c 'printf "password\npassword\n\n" | vncpasswd' username
    – Raj
    Jan 11, 2019 at 6:01
  • 1
    Using the above comment by @raj worked well for me (I'm on VNC4): sudo printf "password\npassword\n\n" | vnc4passwd
    – Lance Kind
    Oct 22, 2019 at 18:56
8

As far as I know, the vncserver command only prompts for a password if the password file (by default, $HOME/.vnc/passwd) is absent - typically the first time it's run for a particular user. You could either script that initial vncserver interaction using 'expect', or pre-create the user's password file by calling the vncpasswd utility via expect before you run vncserver for the first time; e.g. [CAUTION: this is absolutely minimal, you should add some sanity checks if this is used in a serious environment]

#!/bin/sh

prog=/usr/bin/vncpasswd
mypass="newpass"

/usr/bin/expect <<EOF
spawn "$prog"
expect "Password:"
send "$mypass\r"
expect "Verify:"
send "$mypass\r"
expect eof
exit
EOF

If you don't want to (or can't) use 'expect', then there are various hacks available on the web - VNC passwords apparently use a form of DES encryption so searching with the terms 'VNC' 'DES' 'password' should get you what you need (I'm not going to link here since I cannot vouch for any particular one).

For completeness, note that the default Ubuntu 'Desktop Sharing' uses vino, and the password for that appears to be simply base64 encoded, so it is possible to set it directly e.g.

gsettings set org.gnome.Vino vnc-password "$(echo -n "newpass" | base64)"

Hope this helps

0
2

By slightly modifying steeldriver's version I was able to automate my usage of vncpasswd. Allows passing both the password and filename for vncpasswd to create.

Usage: $ ./scriptname <filename> <password>

#!/bin/sh    

myuser="$1"
mypass="$2"

/usr/bin/expect <<EOF
spawn /usr/bin/vncpasswd $myuser
expect "Password:"
send "$mypass\r"
expect "Verify:"
send "$mypass\r"
expect eof
exit
EOF
1
  • 2
    It's generally discouraged to pass secrets as command-line arguments because they can be viewed by all users. Nov 13, 2016 at 9:28
0

In Ansible (requires pexpect to be installed on target):

    - name: Set vnc password for tigervnc
      expect:
        command: vncpasswd /home/{{ UserName }}/.vnc/passwd
        responses:
          (?i)password: "max8char"
          (?i)verify: "max8char"
          (?i)\(y/n\): "n"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .