国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > mcookie 以获得更好的体验。

# Bash 的 mcookie 命令

mcookie [OPTION]...

!subtitle:功能

生成用作 xauth cookie 的 128 位十六进制随机数。

!subtitle:类型

可执行文件(/usr/bin/mcookie),属于 util-linux

!subtitle:参数

  • OPTION 选项:

    • -f, --file file - 指定随机数设备文件,默认位 /dev/urandom;如果此参数设为 - 则读取标准输入

    • -m, --max-size number - 仅从随机数设备文件中读取 number 字节

    • -v, --verbose - 显示详细信息

    • -h, --help - 显示帮助

    • -V, --version - 显示版本

# 示例

$ mcookie                           # 生成随机数
ad149d9f7f46d254bc93b9fc66dc70a4
$ xauth add :0 . $(mcookie)         # 生成 cookie 并添加到 DISPLAY :0 中
$ xauth list                        # 查看 cookie
dev/unix:0  MIT-MAGIC-COOKIE-1  6b662e86481f97c1bf0e6cb3be355ee7

# 推荐阅读

# 手册

MCOOKIE(1)                       User Commands                      MCOOKIE(1)

NAME
       mcookie - generate magic cookies for xauth

SYNOPSIS
       mcookie [options]

DESCRIPTION
       mcookie generates a 128-bit random hexadecimal number for use with the
       X authority system. Typical usage:

          xauth add :0 . mcookie

       The "random" number generated is actually the MD5 message digest of
       random information coming from one of the sources getrandom(2) system
       call, /dev/urandom, /dev/random, or the libc pseudo-random functions,
       in this preference order. See also the option --file.

OPTIONS
       -f, --file file
           Use this file as an additional source of randomness (for example
           /dev/urandom). When file is '-', characters are read from standard
           input.

       -m, --max-size number
           Read from file only this number of bytes. This option is meant to
           be used when reading additional randomness from a file or device.

           The number argument may be followed by the multiplicative suffixes
           KiB=1024, MiB=1024*1024, and so on for GiB, TiB, PiB, EiB, ZiB and
           YiB (the "iB" is optional, e.g., "K" has the same meaning as "KiB")
           or the suffixes KB=1000, MB=1000*1000, and so on for GB, TB, PB,
           EB, ZB and YB.

       -v, --verbose
           Inform where randomness originated, with amount of entropy read
           from each source.

       -h, --help
           Display help text and exit.

       -V, --version
           Print version and exit.

FILES
       /dev/urandom

       /dev/random

BUGS
       It is assumed that none of the randomness sources will block.

SEE ALSO
       md5sum(1), X(7), xauth(1), rand(3)

REPORTING BUGS
       For bug reports, use the issue tracker at
       https://github.com/util-linux/util-linux/issues.

AVAILABILITY
       The mcookie command is part of the util-linux package which can be
       downloaded from Linux Kernel Archive
       <https://www.kernel.org/pub/linux/utils/util-linux/>.

util-linux 2.39.3                 2023-10-23                        MCOOKIE(1)
本文 更新于: 2026-03-06 09:52:32 创建于: 2026-03-06 09:52:32