Linux / UNIX: Generate Passwords

How do I generate random passwords under Linux / UNIX / Mac OS X and *BSD operating systems?

You can use the following command to generate the random password:

ADVERTISEMENTS

tr -dc A-Za-z0-9_ < /dev/urandom | head -c 16 | xargs

Sample outputs:

1z2G4SVmZOdd4uK4

You can create a bash shell function as follows (add to your ~/.bashrc):

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

Now use genpasswd as follows to create 8 character long password:
genpasswd 8
Sample outputs:

Es5Yzf8H

See also:

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
1 comment… add one
  • turtle Apr 16, 2012 @ 7:26

    more elegant :

    genpasswd() {
            tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${1:-8} | xargs
    }
    

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.