2

I wanna create a user had random password for FTP in Docker. The section of Dockerfile like below:

RUN useradd web
RUN export ftppass=`cat /proc/sys/kernel/random/uuid`
RUN echo -e "$ftppass\n$ftppass" | passwd web

But I got an error in last line:

New password: Retype new password: Sorry, passwords do not match.
passwd: Authentication token manipulation error
passwd: password unchanged

Why the passwords do not match even I using a same variable?


update:

I found the output of echo -e "$ftppass\n$ftppass" is:

Step 9/15 : RUN echo -e "$ftppass\n$ftppass"
 ---> Running in ddd97df41d85
-e

Removing intermediate container ddd97df41d85
 ---> a64b606ea898
Step 10/15 : ...

Why it's not works for echo -e and where are my $ftppass?


Resolved, the new section of Dockerfile is:

RUN useradd web
RUN cat /proc/sys/kernel/random/uuid > /etc/vsftp-password
RUN echo "web:"`cat /etc/vsftp-password` | chpasswd
RUN echo "ftp info web:"`cat /etc/vsftp-password`

Thanks anyone and happy new year~

pea3nut
  • 1,634
  • 3
  • 11
  • 15
  • Did you run the echo command without the pipe to test what it echoes? Underscores and a backslash may make it hard for the shell to identify where the variable name ends without curly braces delimiting. I am only guessing no shell at hand and happy New Year in mind. – Dilettant Jan 01 '20 at 08:20
  • 1
    You should use `pnintf` with a format string instead, like `printf '%s\n' "$ftppass" "$ftppass"`. But I second the answer by Roland; don't use `passwd` in scripts. – tripleee Jan 01 '20 at 08:45
  • Unix/Linux questions should be programming related. I suggest to visit https://unix.stackexchange.com . – peterh Jan 01 '20 at 18:05

1 Answers1

5

Instead of using passwd in a creative way, you should rather look at its manual page, which mentions chpasswd in the See Also section.

The passwd command is meant for interactive use, and it doesn't read its input like most other programs, since the input is not echoed to the screen.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121