Increase ulimits on Proxmox
Tested working on proxmox 8.x and 9.x. This will increase the ulimits so you don't hit open file limits especially on containers. Run as root and reboot afterwards.
#!/bin/bash
# Script to increase ulimits on a Proxmox box
# Check if script is run as root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root. Exiting."
exit 1
fi
# Define new ulimit values
NOFILE_SOFT=1048576
NOFILE_HARD=1048576
NPROC_SOFT=65536
NPROC_HARD=65536
echo "Increasing ulimits for open files and processes..."
# 1. Modify /etc/security/limits.conf
echo "Configuring /etc/security/limits.conf..."
cat << EOF >> /etc/security/limits.conf
# Added by increase_ulimits.sh
* soft nofile $NOFILE_SOFT
* hard nofile $NOFILE_HARD
* soft nproc $NPROC_SOFT
* hard nproc $NPROC_HARD
root soft nofile $NOFILE_SOFT
root hard nofile $NOFILE_HARD
root soft nproc $NPROC_SOFT
root hard nproc $NPROC_HARD
EOF
# 2. Modify /etc/systemd/system.conf
echo "Configuring /etc/systemd/system.conf..."
sed -i '/^#DefaultLimitNOFILE=/c\DefaultLimitNOFILE='$NOFILE_SOFT':'$NOFILE_HARD /etc/systemd/system.conf
sed -i '/^#DefaultLimitNPROC=/c\DefaultLimitNPROC='$NPROC_SOFT':'$NPROC_HARD /etc/systemd/system.conf
# 3. Modify /etc/systemd/user.conf
echo "Configuring /etc/systemd/user.conf..."
sed -i '/^#DefaultLimitNOFILE=/c\DefaultLimitNOFILE='$NOFILE_SOFT':'$NOFILE_HARD /etc/systemd/user.conf
sed -i '/^#DefaultLimitNPROC=/c\DefaultLimitNPROC='$NPROC_SOFT':'$NPROC_HARD /etc/systemd/user.conf
# 4. Update PAM configuration for login sessions
echo "Configuring PAM settings..."
cat << EOF >> /etc/pam.d/common-session
# Added by increase_ulimits.sh
session required pam_limits.so
EOF
cat << EOF >> /etc/pam.d/common-session-noninteractive
# Added by increase_ulimits.sh
session required pam_limits.so
EOF
# 5. Reload systemd daemon to apply changes
echo "Reloading systemd daemon..."
systemctl daemon-reload
# 6. Verify current ulimits for the root user
echo "Verifying ulimit settings for current session..."
su - root -c "ulimit -Sn && ulimit -Hn && ulimit -u"
echo "Note: Changes to session limits may require a reboot or relogin to take full effect."
# 7. Provide guidance for verification
echo "Ulimits configuration updated successfully."
echo "To verify after reboot, run: ulimit -n (for open files) and ulimit -u (for processes)."
echo "A system reboot is recommended to ensure all changes are applied."
exit 0