sudo + ulimit not working ?

When I use sudo with ulimit there is an error but if I use ulimit without sudo there is no error.

In bash:

user1@debian:~$ sudo -u user2 -H ulimit -S -c unlimited
sudo: ulimit: command not found
user1@debian:~$


user1@debian:/home/user1$ ulimit -S -c unlimited
user1@debian:/home/user1$


user2@debian:/home/user2$ ulimit -S -c unlimited
user2@debian:/home/user2$

The entire content of /etc/security/limits.conf is commented.

Content of /etc/sudoers :

# /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the man page for details on how to write a sudoers file.
#

Defaults        env_reset,timestamp_timeout=0

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL) ALL
user1 debian=(user2) NOPASSWD: ALL

# Uncomment to allow members of group sudo to not need a password
# (Note that later entries override this, so you might need to move
# it further down)
# %sudo ALL=NOPASSWD: ALL

How shall sudo know when your command starts?
-c accepts commands, put them into double quotes.
Try:

sudo -u user2 -H -c "ulimit -S -c unlimited"

-c will not work. I have an old sudo version ?

user1@debian:~$ sudo -u user2 -H -c "ulimit -S -c unlimited"
sudo: illegal option `-c'
usage: sudo -h | -K | -k | -L | -l | -V | -v
usage: sudo [-bEHPS] [-p prompt] [-u username|#uid] [VAR=value]
            {-i | -s | <command>}
usage: sudo -e [-S] [-p prompt] [-u username|#uid] file ...
user1@debian:~$

This does not produce any errors...

user1@debian:~$ sudo -u user2 -H bash -c "ulimit -S -c unlimited"
user1@debian:~$

Sorry, forget what I wrote, I was mixing up sudo with su - I come back when I have checked it out :wink:

---------- Post updated at 04:10 PM ---------- Previous update was at 03:46 PM ----------

ulimit is a shell builtin on some OSes and in that case - you can't use shell builtins via sudo as far as I know.
If it is an option for you, you might want to change your limits permanent for some user via editing /etc/security/limits.conf or whatever file it is on your OS. Don't forget to relog to make changes for that user/shell active.