create-user.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/bin/sh
  2. # -*- coding: utf-8 -*-
  3. info()
  4. {
  5. echo "--- $*"
  6. }
  7. error()
  8. {
  9. echo "=== ERROR: $*" >&2
  10. }
  11. warning()
  12. {
  13. echo "=== WARNING: $*" >&2
  14. }
  15. die()
  16. {
  17. error "$*"
  18. exit 1
  19. }
  20. entry_checks()
  21. {
  22. [ "$(id -u)" = "0" ] || die "Must be root to create users."
  23. }
  24. sys_groupadd()
  25. {
  26. local args="--system"
  27. info "groupadd $args $*"
  28. groupadd $args "$@" || die "Failed groupadd"
  29. }
  30. sys_useradd()
  31. {
  32. local args="--system -s /usr/sbin/nologin -d /nonexistent -M -N"
  33. info "useradd $args $*"
  34. useradd $args "$@" || die "Failed useradd"
  35. }
  36. do_usermod()
  37. {
  38. info "usermod $*"
  39. usermod "$@" || die "Failed usermod"
  40. }
  41. stop_daemons()
  42. {
  43. systemctl stop letmeind.socket >/dev/null 2>&1
  44. systemctl stop letmeind.service >/dev/null 2>&1
  45. systemctl stop letmeinfwd.socket >/dev/null 2>&1
  46. systemctl stop letmeinfwd.service >/dev/null 2>&1
  47. }
  48. remove_users()
  49. {
  50. # Delete all existing users and groups, if any.
  51. userdel letmeind >/dev/null 2>&1
  52. groupdel letmeind >/dev/null 2>&1
  53. }
  54. add_users()
  55. {
  56. sys_groupadd letmeind
  57. sys_useradd -g letmeind letmeind
  58. }
  59. entry_checks
  60. stop_daemons
  61. remove_users
  62. add_users
  63. # vim: ts=4 sw=4 expandtab