Skip to content

Admin Scripts

These Bash scripts collect information from a Posit server: configuration, versions, certificates, ports, DNS, disk space, and groups. They were written for RHEL 8 and RHEL 9 hosts running Workbench, Connect, and Package Manager. Posit lists RHEL 8, 9, and 10 as supported for all three products.123 The scripts help most during audits, handoffs, and migrations. Use them to capture a server's state quickly and compare it with another.

Each page in this section includes the full source of its script in a Source block, so what you read is what you install. Open the block and use its copy button to paste the script onto your server.

Running without root

Many organizations separate the people who support the Posit products from the people who hold sudo on the servers. These scripts assume you are in the first group. They run as an ordinary user, read whatever that user can read, and never call sudo.

Some checks need root: reading a private key, database.conf, or another user's files, or seeing which process owns a port. The script does not attempt them. The report marks the item "needs root" and, just before its footer, lists the commands for the person who holds sudo. Send that section to them, and keep their output with the report. Every deferred command only reads; none of them change the server.

== Commands for your sudo administrator ==
  These checks need root. Ask your sudo administrator to run them and send back the output.

# database.conf
sudo grep -Ev '^[[:space:]]*([#;]|$)' /etc/rstudio/database.conf | sed -E 's/^([^=]*([Pp]assword|[Ss]ecret|[Tt]oken)[^=]*=).*/\1 ********/'

# Active sessions
sudo rstudio-server active-sessions

Set POSIT_ROOT_ADMIN to the name of that person or account, and the report uses it in place of "your sudo administrator":

export POSIT_ROOT_ADMIN=platform-admin

Adapt before you run

Adapt these scripts before you run them

These scripts are examples written for one environment. Read each one, run it first on a test server, and change anything that does not match your hosts.

The scripts assume a RHEL layout and the default Posit install and configuration locations: /etc/rstudio, /etc/rstudio-connect/rstudio-connect.gcfg, /etc/rstudio-pm/rstudio-pm.gcfg, /opt/rstudio-connect, /opt/rstudio-pm, /opt/R, /opt/python, and /var/log/rstudio. Values to adapt are the environment variables in the table below and the example hostnames on these pages (posit.example.com, ppm-old.example.com, postgres.example.com).

Configuration files are printed with comments removed, and values whose setting name contains password, secret, or token are replaced with ********, as is a password inside a URL. Other values, such as hostnames, group names, and paths, are printed as they are. Review a report before you share it outside your team.

Install

You do not need root to install the scripts. Copy them to a directory on your PATH, such as ~/.local/bin. Bash Practices explains the naming and install conventions.

Start with posit-lib.sh. Every posit-* script loads its helper functions from this file, and exits if it is not in the same directory. Copy the source from the block below, then paste it into a new file:

mkdir -p ~/.local/bin
cat > ~/.local/bin/posit-lib.sh

cat waits for input. Paste the source, press Enter, then press Ctrl+D to save the file. The file is sourced, not run, so it does not need to be executable:

chmod 0644 ~/.local/bin/posit-lib.sh

Install each script the same way, from the Source block on its page, and make it executable:

cat > ~/.local/bin/posit-host
chmod 0755 ~/.local/bin/posit-host

Keep cat and chmod in separate pastes. Otherwise cat reads the chmod line as part of the file.

An editor such as nano or vi works too. Save the file under the script's name, with no extension. test-postgres-connectivity does not use posit-lib.sh, so it can go anywhere.

Saved reports

Save each report under a name that starts with a UTC timestamp, so ls sorts runs in order and you can compare them. The pattern matches the one in Logging Analysis Scripts:

posit-workbench > ~/$(date -u +%Y%m%dT%H%M%SZ)_posit-workbench_$(id -un)_$(hostname -s).txt

Every posit-* report opens with a header and closes with a footer, so a saved report shows which script ran, where, and whether it finished. The header records the script's name and the first 12 characters of its SHA-256 checksum, the host, the user, the process ID, and a UTC start time:

Posit Workbench report
Script: posit-workbench   sha256: 1c2d144f192a
Host: posit.example.com   User: jsmith   PID: 71908
Start: 2026-11-04T20:20:55Z

A trap writes the footer however the script exits:

End: 2026-11-04T20:21:03Z   exit status: 0

A report without an End: line was cut short. The checksum shows whether two reports came from the same copy of the script. These follow the rules in Logging from Bash scripts.

Source: scripts/posit-lib.sh
# shellcheck shell=bash
# posit-lib.sh: helpers shared by the posit-* scripts.
#
# Source this file; do not run it. The scripts run as an ordinary user.
# Any check that needs root is not attempted: it is recorded with defer()
# and printed at the end of the report as a command for the person who
# holds sudo on the server.

readonly ROOT_ADMIN="${POSIT_ROOT_ADMIN:-your sudo administrator}"
DEFERRED=()

#######################################
# Print a section heading.
# Arguments:
#   Heading text.
#######################################
header() {
  printf '\n== %s ==\n' "$1"
}

#######################################
# Print one labeled value.
# Arguments:
#   Label, value.
#######################################
item() {
  printf '  %-30s %s\n' "$1:" "$2"
}

#######################################
# Print a warning line.
# Arguments:
#   Message; several arguments are joined with spaces.
#######################################
warn() {
  printf '  WARNING: %s\n' "$*"
}

#######################################
# Print a note line.
# Arguments:
#   Message; several arguments are joined with spaces.
#######################################
note() {
  printf '  NOTE: %s\n' "$*"
}

#######################################
# Record a root only check for the closing section.
# Globals:
#   DEFERRED
# Arguments:
#   Label, command for the sudo administrator.
#######################################
defer() {
  DEFERRED+=("# $1"$'\n'"$2")
  item "$1" "needs root (see the end of this report)"
}

#######################################
# Print the deferred commands, if any.
# Globals:
#   DEFERRED
#   ROOT_ADMIN
#######################################
print_deferred() {
  (( ${#DEFERRED[@]} == 0 )) && return 0
  header "Commands for ${ROOT_ADMIN}"
  echo "  These checks need root. Ask ${ROOT_ADMIN} to run them and send" \
    "back the output."
  echo
  printf '%s\n\n' "${DEFERRED[@]}"
}

#######################################
# Print the current time in ISO 8601 UTC.
#######################################
utc_now() {
  date -u +%Y-%m-%dT%H:%M:%SZ
}

#######################################
# Print the report header and register the footer.
# Arguments:
#   Report title.
#######################################
report_start() {
  local self
  local sum
  local host
  self="$(readlink -f "$0")"
  sum="$(sha256sum "${self}" 2>/dev/null | cut -c1-12)"
  host="$(hostname -f 2>/dev/null || hostname)"
  echo "$1"
  echo "Script: $(basename "${self}")   sha256: ${sum}"
  echo "Host: ${host}   User: $(id -un)   PID: $$"
  echo "Start: $(utc_now)"
  if (( $(id -u) == 0 )); then
    note "running as root; the deferred commands are not needed"
  fi
  # The footer is written on every exit, including early ones
  trap 'report_end $?' EXIT
}

#######################################
# Print the report footer.
# Arguments:
#   Exit status.
#######################################
report_end() {
  printf '\nEnd: %s   exit status: %s\n' "$(utc_now)" "$1"
}

#######################################
# Test whether a systemd service unit exists.
# Arguments:
#   Unit name without .service.
# Returns:
#   0 if the unit is loaded, 1 otherwise.
#######################################
unit_installed() {
  local state
  state="$(systemctl show -p LoadState --value "$1.service" 2>/dev/null)"
  [[ "${state}" == "loaded" ]]
}

#######################################
# Print whether a unit is active and enabled at boot.
# Arguments:
#   Unit name.
#######################################
unit_status() {
  local name="$1"
  local active
  local enabled
  if ! unit_installed "${name}"; then
    item "${name}" "not installed"
    return
  fi
  active="$(systemctl is-active "${name}" 2>/dev/null)"
  enabled="$(systemctl is-enabled "${name}" 2>/dev/null)"
  item "${name}" "${active}, ${enabled} at boot"
}

#######################################
# Print the last value of KEY=VALUE in an rserver.conf style file.
# Arguments:
#   File, key.
# Outputs:
#   The value, or an empty line.
#######################################
ini_value() {
  awk -v k="$2" '
    /^[[:space:]]*#/ { next }
    index($0, "=") {
      key = substr($0, 1, index($0, "=") - 1)
      gsub(/[[:space:]]/, "", key)
      if (key == k) {
        v = substr($0, index($0, "=") + 1)
        gsub(/^[[:space:]]+|[[:space:]]+$/, "", v)
      }
    }
    END { print v }
  ' "$1" 2>/dev/null
}

#######################################
# Print every value of a key in one section of a gcfg file.
# Section and key names are matched without regard to case.
# Arguments:
#   File, section, key.
# Outputs:
#   One value per line.
#######################################
gcfg_values() {
  awk -v s="$2" -v k="$3" '
    BEGIN { s = tolower(s); k = tolower(k) }
    /^[[:space:]]*[;#]/ { next }
    /^[[:space:]]*\[/ {
      sec = tolower($0)
      gsub(/^[[:space:]]*\[|\][[:space:]]*$/, "", sec)
      in_s = (sec == s)
      next
    }
    in_s && index($0, "=") {
      key = substr($0, 1, index($0, "=") - 1)
      gsub(/[[:space:]]/, "", key)
      if (tolower(key) == k) {
        v = substr($0, index($0, "=") + 1)
        gsub(/^[[:space:]]+|[[:space:]]+$/, "", v)
        gsub(/^"|"$/, "", v)
        print v
      }
    }
  ' "$1" 2>/dev/null
}

#######################################
# Print the last value of a key in one section of a gcfg file.
# Arguments:
#   File, section, key.
#######################################
gcfg_value() {
  gcfg_values "$@" | tail -n 1
}

#######################################
# Print settings without comments, with secrets masked.
# Arguments:
#   Config file.
#######################################
show_config() {
  sed -E \
    -e '/^[[:space:]]*([#;]|$)/d' \
    -e 's/^([^=]*([Pp]assword|[Ss]ecret|[Tt]oken)[^=]*=).*/\1 ********/' \
    -e 's#(://[^:/@[:space:]]+:)[^@[:space:]]+@#\1********@#' \
    -e 's/^/    /' "$1"
}

#######################################
# Show a config file if readable, defer it if not.
# Arguments:
#   Label, file.
#######################################
config_file() {
  local label="$1"
  local file="$2"
  local cmd
  if [[ ! -e "${file}" ]]; then
    item "${label}" "not present"
  elif [[ -r "${file}" ]]; then
    item "${label}" "${file}"
    show_config "${file}"
  else
    cmd="sudo grep -Ev '^[[:space:]]*([#;]|\$)' ${file} | sed -E"
    cmd+=" 's/^([^=]*([Pp]assword|[Ss]ecret|[Tt]oken)[^=]*=).*/\\1 ********/'"
    defer "${label}" "${cmd}"
  fi
}

#######################################
# Print the owner, group, and mode of a file.
# Arguments:
#   File.
# Outputs:
#   user:group mode, or "cannot stat" if the parent directory hides it.
#######################################
file_mode() {
  stat -c '%U:%G %a' -- "$1" 2>/dev/null || echo "cannot stat"
}

#######################################
# Print the local addresses listening on a TCP port.
# Arguments:
#   Port.
# Outputs:
#   Space separated addresses, or an empty line.
#######################################
listening() {
  ss -Hltn 2>/dev/null \
    | awk -v p="$1" '{ n = split($4, a, ":"); if (a[n] == p) print $4 }' \
    | sort -u \
    | paste -sd' ' -
}

#######################################
# Report whether anything listens on a configured port.
# Arguments:
#   Label, configured address, port.
#######################################
check_listen() {
  local label="$1"
  local addr="$2"
  local port="$3"
  local found
  found="$(listening "${port}")"
  if [[ -n "${found}" ]]; then
    item "${label}" "${addr}:${port} configured, listening on ${found}"
  else
    item "${label}" \
      "${addr}:${port} configured, nothing listening on port ${port}"
  fi
}

#######################################
# Report the HTTP status for a URL, or the curl error.
# Arguments:
#   Label, URL.
#######################################
http_check() {
  local label="$1"
  local url="$2"
  local out
  if ! command -v curl >/dev/null 2>&1; then
    item "${label}" "curl not installed"
    return
  fi
  out="$(curl -sS -o /dev/null -w '%{http_code}' --max-time 10 -- "${url}" \
    2>&1)"
  if [[ "${out: -3}" != "000" ]]; then
    item "${label}" "${url} (HTTP ${out: -3})"
  else
    out="${out%000}"
    out="${out%$'\n'}"
    item "${label}" "${url} (failed: ${out##*curl: })"
  fi
}

#######################################
# Report whether this host can open a TCP connection.
# Arguments:
#   Label, host, port.
#######################################
tcp_check() {
  # The inner shell expands $1 and $2, so single quotes are intended
  # shellcheck disable=SC2016
  if timeout 5 bash -c 'exec 3<>"/dev/tcp/$1/$2"' _ "$2" "$3" 2>/dev/null
  then
    item "$1" "$2:$3 reachable"
  else
    item "$1" "$2:$3 not reachable"
  fi
}

#######################################
# Report a certificate's subject, issuer, expiry, and key match.
# Arguments:
#   Label, certificate file, key file (may be empty).
#######################################
cert_check() {
  local label="$1"
  local cert="$2"
  local key="$3"
  local end
  local end_s
  local days
  local cmd
  if [[ -z "${cert}" ]]; then
    item "${label}" "not set"
    return
  fi
  if [[ ! -e "${cert}" && -x "$(dirname -- "${cert}")" ]]; then
    item "${label}" "${cert} not present"
    return
  elif [[ ! -r "${cert}" ]]; then
    defer "${label}" \
      "sudo openssl x509 -in ${cert} -noout -subject -issuer -enddate"
  else
    item "${label}" "${cert}"
    openssl x509 -in "${cert}" -noout -subject -issuer -enddate 2>/dev/null \
      | sed 's/^/    /'
    end="$(openssl x509 -in "${cert}" -noout -enddate 2>/dev/null \
      | cut -d= -f2)"
    if [[ -n "${end}" ]] && end_s="$(date -d "${end}" +%s 2>/dev/null)"; then
      days=$(( (end_s - $(date +%s)) / 86400 ))
      if (( days < 0 )); then
        warn "certificate expired $(( -days )) days ago"
      elif (( days < 30 )); then
        warn "certificate expires in ${days} days"
      fi
    fi
  fi
  [[ -z "${key}" ]] && return
  if [[ -r "${key}" && -r "${cert}" ]]; then
    if [[ "$(openssl x509 -in "${cert}" -noout -pubkey 2>/dev/null)" \
      == "$(openssl pkey -in "${key}" -pubout 2>/dev/null)" ]]; then
      item "${label} key" "matches the certificate"
    else
      warn "${key} does not match ${cert}"
    fi
  else
    item "${label} key mode" "$(file_mode "${key}")"
    cmd="sudo bash -c 'diff <(openssl x509 -in ${cert} -noout -pubkey)"
    cmd+=" <(openssl pkey -in ${key} -pubout) && echo key matches'"
    defer "${label} key match" "${cmd}"
  fi
}

#######################################
# List the newest logs in a directory if readable, defer if not.
# Arguments:
#   Label, directory.
#######################################
log_dir() {
  local label="$1"
  local dir="$2"
  if [[ ! -d "${dir}" ]]; then
    item "${label}" "${dir} not present"
  elif [[ -r "${dir}" && -x "${dir}" ]]; then
    item "${label}" "${dir}"
    # ls -lt gives the newest first with sizes, which find does not
    # shellcheck disable=SC2012
    ls -lt -- "${dir}" 2>/dev/null | sed -n '2,6p' | sed 's/^/    /'
  else
    defer "${label}" "sudo ls -lt ${dir} | head -n 6"
  fi
}

#######################################
# Report free space on a directory's filesystem and defer its size.
# Its size needs read access to every file, so it always needs root.
# Arguments:
#   Label, directory.
#######################################
dir_size() {
  local label="$1"
  local dir="$2"
  local free
  if [[ ! -e "${dir}" ]]; then
    item "${label}" "${dir} not present"
    return
  fi
  free="$(df -hP -- "${dir}" 2>/dev/null \
    | awk 'NR == 2 { print $4 " free of " $2 " (" $5 " used) on " $6 }')"
  item "${label} free" "${free}"
  defer "${label} size" "sudo du -sh ${dir}"
}

#######################################
# Print the first line of a program's --version output.
# Arguments:
#   Program.
#######################################
version_of() {
  "$1" --version 2>&1 | head -n 1
}

Environment variables

Variable Used by Default
POSIT_ROOT_ADMIN All posit-* scripts "your sudo administrator"
POSIT_WB_CONF_DIR posit-workbench, posit-groups /etc/rstudio
POSIT_CONNECT_CONF posit-connect, posit-groups, posit-usage /etc/rstudio-connect/rstudio-connect.gcfg
POSIT_PM_CONF posit-pm, posit-usage /etc/rstudio-pm/rstudio-pm.gcfg
POSIT_GROUPS posit-groups Empty. Extra group names, separated by spaces.
DNS_TEST_HOST posit-host This host's FQDN
DB_HOST, DB_PORT, DB_USER, DB_NAME test-postgres-connectivity postgres.example.com, 5432, posit_admin, rstudio_pm

The configuration path variables let you run a script against a copy of the files, for example a copy your sudo administrator sent you.

Catalog

Script Purpose Page
posit-host OS, CPU, memory, filesystems, network, DNS, listening ports, services, SELinux, firewall, R, Python, and Quarto Host
posit-usage CPU, memory, processes, and disk used by each Posit service Resource Usage
posit-workbench Workbench and Launcher configuration, ports, launcher TLS, repositories, logs Workbench
posit-connect Connect configuration, addresses, authentication, R, Python, and Quarto, email, data Connect
posit-pm Package Manager configuration, addresses, sync schedules, repositories, comparison with another server Package Manager
posit-groups Groups Posit uses, export, and a plan to match another server Groups
test-postgres-connectivity DNS, TCP, TLS, and login test against PostgreSQL PostgreSQL

What the scripts write

The scripts only read, with one exception: posit-groups --export FILE writes a new file (mode 0600) and refuses to overwrite one that exists. posit-groups --plan prints commands but does not run them.