Ask Your Question

Generate function on centos 7.x calling scripts. Commands having 'cat /dev/uradom' are not terminated. Why?

asked 2018-11-14 11:41:55 -0600

pier4r gravatar image

This is a problem in which I incurred some days ago.

I used a generate function to call a shell script, and one instruction in the shell script was:
cat /dev/urandom | tr -dc 'a-zA-Z0-9-_' | fold -w 16 | head -1 .
That didn't terminate. After an hour of debug and trial and error I pinpointed the problem to cat /dev/urandom . Changing the one liner with:
dd if=/dev/urandom bs=150 count=1 2> /dev/null | tr -dc 'a-zA-Z0-9-_' | fold -w 16 | head -1
solved the problem.

The peculiar part is that the problem appeared only on systems with centos 7.x . On ubuntu 14 cat /dev/urandom had no problem. Does someone have an idea why it happened?

Thanks for the help.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2018-11-15 09:41:13 -0600

A possible cause can be considered from urandom(4):

Since Linux 3.16, a read(2) from /dev/urandom will return at most 32 MB.

Note, there are alternatives in creating a random string consisting of printable characters only, e.g. something like

generate("/usr/bin/pwgen -N1 -H/dev/null#${fqdn} 16")

especially, since you don't realize it's theoretically possible of having more than 134 non-printable characters.

edit flag offensive delete link more


"since you don't realize ..." I do realize it, but it should not happen that frequently from my use case. Plus I feel you could have worded the sentence to be slightly less negative. The cause you mention, the 32 MB from `dev/urandom`, shouldn't be a problem most of the time.

pier4r gravatar imagepier4r ( 2018-11-16 11:01:48 -0600 )edit

No, it's why `cat(1)` terminated. The 32MB limit. `cat(1)` stops reading a file, when it reaches EOF. Since `/dev/urandom` is “virtually” a 32MB large file, as of Linux 3.16 and later, `cat(1)` can't just come up with more characters to print.

Kai Burghardt gravatar imageKai Burghardt ( 2018-11-16 14:16:37 -0600 )edit

However, I suspect your Linux instance that is been installed via the CentOS 7.x distribution, uses an earlier Linux kernel, where this limit did not exist, hence `/dev/urandom` never stops producing data, as there is no EOF, which in turn would terminate `cat(1)`. You see?

Kai Burghardt gravatar imageKai Burghardt ( 2018-11-16 14:18:42 -0600 )edit

I am still not convinced. Executing the command on the shell produces no problem. Only when it is executed via puppet it produces a problem. If the missing EOF would be the cause, then also the shell execution would not terminate, but it does.

pier4r gravatar imagepier4r ( 2018-12-06 05:47:15 -0600 )edit

Your random string function containing `cat /dev/urandom` _does_ terminate on your CentOS system? When it's manually invoked in a shell? What does `uname(1)` tell there? `uname -r` in particular. You call `generate('/path/to/my/')`, right? On both targets.

Kai Burghardt gravatar imageKai Burghardt ( 2018-12-08 17:25:39 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-11-14 11:41:55 -0600

Seen: 92 times

Last updated: Nov 15