The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

    • RT @nadineswagerman: Iemand mailde me dat een 13-jarig meisje mijn novelle had uitgekozen om te lezen/bespreken op school. Het boek bevat 9… 3 hours ago
    • RT @jilles_com: Afgelopen 10 dagen hebben we door NL gereisd, elke dag een nieuw hotel en op heel veel locaties gegeten. In 60% van de geva… 3 hours ago
    • RT @FTM_nl: Sywert van Lienden plaatste bewust een serie tweets om het ministerie onder druk te zetten om de omstreden mondkapjesdeal met h… 3 hours ago
    • RT @ArmsControlWonk: China just used a rocket to put a space plane in orbit and the space plane glided back to earth. Orbital bombardment i… 3 hours ago
    • RT @shossontwits: O jee. Nu dit weer. 3 hours ago
  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,482 other followers

bash: generating random passwords on Mac OS X / MacOS or what Apple calls their operating system by now

Posted by jpluimers on 2018/08/14

After some fiddling, I put this function in ~/bash_profile on my MacBook:

# pass # digits as first argument; defaults to 20
generate_password() {
  local l=$1
  [ "$l" == "" ] && l=20
  env LC_CTYPE=C LC_ALL=C tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs

The fiddling was working around this error:

tr: Illegal byte sequence

The cause is that Mac OS is not Linux, where tr happily accepts any byte sequence from /dev/urandom: it requires the C locale.

Originally, I started with the below function from [WayBackTop 20 OpenSSH Server Best Security Practices – nixCraft (thanks +Joe C Hecht) which I undid from mixed tab/spaces:

genpasswd() {
  local l=$1
  [ "$l" == "" ] && l=20
  tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs

Via: [WayBackJoe C. Hecht – Google+ who mentions:

I might add that if you do move the SSH port, try to keep it in within the first 1024 ports for a tad bit more protection on some systems.

Background reading on Apple hating binary input:

Background reading on the language setting:


2 Responses to “bash: generating random passwords on Mac OS X / MacOS or what Apple calls their operating system by now”

  1. ruurd said

    The operating system that runs om Apple Mac computers is nowadays called macOS with a lowercase ‘m’ and uppercase ‘OS’.

    • jpluimers said

      Right. That’s exactly the thing I meant. With so many renames, and addressing older versions too, I am not sure how to call their OS any more.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: