pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS


nfskrb5test1.ksh - KDC test script for Windows NFS krb5 setup test #001
Posted by Anonymous on Wed 26th Aug 2026 18:15
raw | new post
modification of post by Anonymous (view diff)

  1. #!/bin/ksh
  2.  
  3. #
  4. # nfskrb5test1.ksh - KDC test script for Windows NFS krb5 setup test #001
  5. #
  6. # Written by Roland Mainz <roland.mainz@nrubsig.org>
  7. #
  8.  
  9. #
  10. # This is a very simple *TEST* script which sets up a Kerberos5 KDC and
  11. # NFSv4.1 server with GSSAPI on the same machine
  12. #
  13. # This script requires the following Debian 11/13 packages:
  14. # $ apt-get install krb5-user krb5-kdc libkrb5-dev krb5-admin-server keyutils ksh nfs-kernel-server nfs-common nfs4-acl-tools
  15. #
  16. # setup Windows machine
  17. # On Windows machine use (via Cygwin bash/ksh93 shell):
  18. # --- snip ---
  19. # # KDC and NFS server must have entries in "/cygdrive/c/Windows/System32/drivers/etc/hosts"
  20. # printf '10.49.202.233\t\tdebnfskrb5001.nfschicken.test\n' >>/cygdrive/c/Windows/System32/drivers/etc/hosts
  21. # printf '10.49.202.233\t\tdebnfskrb5001\n' >>/cygdrive/c/Windows/System32/drivers/etc/hosts
  22. # ksetup /setrealm NFSCHICKEN.TEST
  23. # ksetup /addkdc NFSCHICKEN.TEST debnfskrb5001.nfschicken.test
  24. # ksetup /setrealmflags NFSCHICKEN.TEST tcpsupported
  25. # ksetup /mapuser rmainz@NFSCHICKEN.TEST roland_mainz
  26. # ksetup /setcomputerpassword myhorriblepassword12
  27. # # user has to do the Windows logon as user "rmainz@NFSCHICKEN.TEST", NOT as "roland_mainz"
  28. # # mount with
  29. # nfs_mount -o sec=krb5,rw 'K' nfs://10.49.202.233//nfsdata
  30. # --- snip ---
  31. #
  32. #
  33.  
  34. export PATH='/usr/bin:/bin:/sbin'
  35.  
  36. builtin mkdir
  37. #builtin rm
  38.  
  39. set -o xtrace
  40. set -o nounset
  41. set -o errexit
  42.  
  43. kdctestdir='/tmp/kdctest1'
  44. compound config=(
  45.         hostname="$(hostname --fqdn)"
  46.         kdcport=88
  47.         kadmindport=749
  48.         # nfsdomain must be LOWERCASE!
  49.         nfsdomain='nfschicken.test'
  50.         # realmname must be UPPERCASE!
  51.         realmname='NFSCHICKEN.TEST'
  52. )
  53.  
  54.  
  55. rm -Rfv -- "$kdctestdir"
  56. rm -Rfv "/tmp/krb5cc_dir_$(id -u)"
  57. mkdir -p -- "$kdctestdir"
  58.  
  59.  
  60. export KRB5_KDC_PROFILE="${kdctestdir}/kdc.conf"
  61. export KRB5_CONFIG="${kdctestdir}/krb5.conf"
  62.  
  63. if [[ ! -d '/nfsdata' ]] ; then
  64.         mkdir /nfsdata
  65.         chmod a+rwxt /nfsdata
  66.  
  67.         printf '/nfsdata\tgss/krb5(rw,no_subtree_check) gss/krb5i(rw,no_subtree_check) gss/krb5p(rw,no_subtree_check)\n' >>'/etc/exports'
  68. fi
  69.  
  70.  
  71. printf 'NEED_SVCGSSD="yes"\n' >>/etc/default/nfs-kernel-server
  72.  
  73. cat >'/etc/idmapd.conf' <<EOF
  74. # Verbosity = 8 logs all idmapper lookups
  75. Verbosity = 8
  76. Pipefs-Directory = /run/rpc_pipefs
  77. # set your own domain here, if it differs from FQDN minus hostname
  78. # Domain = localdomain
  79. # Value for "Domain" must be lowercase, value for "Local-Realms" uppercase
  80. Domain = ${config.nfsdomain}
  81. Local-Realms = ${config.realmname}
  82.  
  83. [Mapping]
  84. Nobody-User = nobody
  85. Nobody-Group = nogroup
  86. EOF
  87.  
  88. cat >"${KRB5_CONFIG}" <<EOF
  89. [libdefaults]
  90.     default_realm = ${config.realmname}
  91.     default_ccache_name = DIR:/tmp/krb5cc_dir_%{uid}
  92.  
  93. [realms]
  94.     ${config.realmname} = {
  95.         kdc = ${config.hostname}:${config.kdcport}
  96.         admin_server = ${config.hostname}:${config.kadmindport}
  97.     }
  98. EOF
  99.  
  100.  
  101. cat >"${KRB5_KDC_PROFILE}" <<EOF
  102. [kdcdefaults]
  103.     kdc_ports = ${config.kdcport}
  104.     kdc_tcp_ports = ${config.kdcport}
  105.  
  106. [realms]
  107.     ${config.realmname} = {
  108.         kadmind_port = ${config.kadmindport}
  109.         acl_file = ${kdctestdir}/kadm5.acl
  110.         admin_keytab = ${kdctestdir}/kadm5.keytab
  111.         database_name = ${kdctestdir}/principal
  112.         key_stash_file = ${kdctestdir}/.k5.${config.realmname}
  113.  
  114.         max_life = 12h 0m 0s
  115.         max_renewable_life = 7d 0h 0m 0s
  116.         master_key_type = aes256-cts-hmac-sha1-96
  117.         supported_enctypes = aes256-cts-hmac-sha1-96:normal aes128-cts-hmac-sha1-96:normal arcfour-hmac:normal
  118.     }
  119.  
  120. [logging]
  121.     kdc = FILE:${kdctestdir}/kdc.log
  122.     admin_server = FILE:${kdctestdir}/kadmin.log
  123.     default = FILE:${kdctestdir}/default.log
  124.  
  125. EOF
  126.  
  127. {
  128.         printf '%s\n' 'myhorriblepassword12'
  129.         printf '%s\n' 'myhorriblepassword12'
  130. } | kdb5_util create -r "${config.realmname}" -s
  131.  
  132.  
  133.  
  134. #
  135. # add principals
  136. #
  137. compound -A principal_list=(
  138.         # add admin user
  139.         ['admin']=(
  140.                 principal="admin/admin@${config.realmname}"
  141.                 password='myhorriblepassword12'
  142.         )
  143.  
  144.         # add plain (Windows) user
  145.         ['user1']=(
  146.                 principal="rmainz@${config.realmname}"
  147.                 password='myhorriblepassword12'
  148.         )
  149.  
  150.         # add Linux host
  151.         ['linuxhost1']=(
  152.                 principal="host/debnfskrb5001@${config.realmname}"
  153.                 password='myhorriblepassword12'
  154.         )
  155.         ['linuxhost1_fqdn']=(
  156.                 principal="host/debnfskrb5001.nfschicken.test@${config.realmname}"
  157.                 password='myhorriblepassword12'
  158.         )
  159.  
  160.         # add NFS service on Linux host
  161.         ['linuxhost1_nfs_service']=(
  162.                 principal="nfs/debnfskrb5001@${config.realmname}"
  163.                 password='myhorriblepassword12'
  164.         )
  165.         ['linuxhost1_nfs_service_fqdn']=(
  166.                 principal="nfs/debnfskrb5001.nfschicken.test@${config.realmname}"
  167.                 password='myhorriblepassword12'
  168.         )
  169.  
  170.         # add Windows machine
  171.         ['win1']=(
  172.                 principal="host/wingrendel02@${config.realmname}"
  173.                 password='myhorriblepassword12'
  174.         )
  175.         ['win1_fqdn']=(
  176.                 principal="host/wingrendel02.nfschicken.test@${config.realmname}"
  177.                 password='myhorriblepassword12'
  178.         )
  179. )
  180.  
  181. for pi in "${!principal_list[@]}" ; do
  182.         nameref pie="principal_list[$pi]"
  183.  
  184.         {
  185.                 printf "addprinc %s\n" "${pie.principal}"
  186.                 printf '%s\n' "${pie.password}"
  187.                 printf '%s\n' "${pie.password}"
  188.         } | kadmin.local
  189. done
  190.  
  191. #
  192. # create /etc/krb5.keytab for NFS server
  193. #
  194. rm -f /tmp/nfs01.keytab
  195. {
  196.         printf 'ktadd -k /tmp/nfs01.keytab nfs/debnfskrb5001.nfschicken.test@NFSCHICKEN.TEST\n'
  197. } | kadmin.local
  198. mv -f /tmp/nfs01.keytab /etc/krb5.keytab
  199.  
  200. #
  201. # start KDC
  202. #
  203. krb5kdc -n &
  204. (( kdc_pid=$! ))
  205.  
  206. # wait for KDC to start
  207. sleep 10
  208.  
  209. #
  210. # simple function test
  211. #
  212. {
  213.         printf '%s\n' 'myhorriblepassword12'
  214. } | kinit "admin/admin@${config.realmname}"
  215. {
  216.         printf '%s\n' 'myhorriblepassword12'
  217. } | kinit "rmainz@${config.realmname}"
  218.  
  219. klist -A
  220.  
  221. #
  222. # restart NFS server to pickup changes in /etc/exports and KRB5 changes
  223. #
  224. systemctl restart nfs-idmapd.service
  225. sleep 5
  226. systemctl restart nfs-kernel-server
  227.  
  228. #
  229. # sleep for two weeks
  230. #
  231. sleep $((60*60*24*7*2))
  232.  
  233. kill $kdc_pid
  234. wait $kdc_pid
  235.  
  236. # EOF.

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at