How to set ulimit in linux?

See this for systemd.

As for limits.conf change, PAM is used (as Paul mentioned) so you need to (re)login.

Lets see the following example (this is Debian, but all the same on most distributions)

I'm logged in (on desktop) and open terminal to see my stack size in KB as soft limit

myuser:~$ ulimit -Sa
real-time non-blocking time  (microseconds, -R) unlimited
core file size              (blocks, -c) 0
data seg size               (kbytes, -d) unlimited
scheduling priority                 (-e) 0
file size                   (blocks, -f) unlimited
pending signals                     (-i) 127873
max locked memory           (kbytes, -l) 4101812
max memory size             (kbytes, -m) unlimited
open files                          (-n) 1024
pipe size                (512 bytes, -p) 8
POSIX message queues         (bytes, -q) 819200
real-time priority                  (-r) 0
stack size                  (kbytes, -s) 8192 # this one.
cpu time                   (seconds, -t) unlimited
max user processes                  (-u) 127873
virtual memory              (kbytes, -v) unlimited
file locks                          (-x) unlimited

Lets change the soft limit by modifying the mentioned limits.conf file - you should use drop in file rather in /etc/security/limits.d, but lets keep it in limits.conf for the sake of argument.

So i have added the wanted new stack size value for my desktop user.

@myuser		soft stack 16384

I just open new terminal window, but ulimit is still same.

Now lets SSH to localhost as myuser and check :

myuser:~$ ssh myuser@localhost "ulimit -Sa"
myuser@localhost's password: 
real-time non-blocking time  (microseconds, -R) unlimited
core file size              (blocks, -c) 0
data seg size               (kbytes, -d) unlimited
scheduling priority                 (-e) 0
file size                   (blocks, -f) unlimited
pending signals                     (-i) 127873
max locked memory           (kbytes, -l) 4101812
max memory size             (kbytes, -m) unlimited
open files                          (-n) 1024
pipe size                (512 bytes, -p) 8
POSIX message queues         (bytes, -q) 819200
real-time priority                  (-r) 0
stack size                  (kbytes, -s) 16384 # applied.
cpu time                   (seconds, -t) unlimited
max user processes                  (-u) 127873
virtual memory              (kbytes, -v) unlimited
file locks                          (-x) unlimited

Of course, one can just logout and login back to see new soft value.

Does this make it clearer for login sessions ?
How did you login back after making the above change ?

Check also output of -Ha -Sa for soft and hard limits.

A soft limit is a 'default' limit, if your program does not change it will be the limit enforced by the system ( if program is started from terminal after login )

A hard limit is maximum value defined to some sane system defaults, often much higher or at least same as soft limit.

Both limits can be manipulated by program, in case of say shell, using ulimit command directly by setting the desired param before executing the program / script which would need the option ( say 10k open files - ulimit -S -n 10000 )

A user soft limit can be set by user as high as hard limit limited in limits.conf (or system default)

The process (program you executed) will inherit the limit and behave accordingly as well as all child processes produced from it (fork)

In most use cases soft limit is to be manipulated in PAM sessions if required and enforced by super user (limits.conf / limits.d/custom.conf)
E.g. you do not want a user to ssh to server to run a zillion bugged scripts bringing multiuser system to its knees.

A hard limit should only be manipulated by superuser via limits.conf / limits.d for login session (PAM) or systemd files (or global systemd options) for services (boot)

Setting those value to extreme numbers as operating system defaults (systemd/boot) is a bad idea, rather configure on per service / per user level

In runtime you can check the limits of the process by executing :
cat /proc/<pid>/limits where you put your app parent PID instead of <pid>

Hopefully i got most right :clown_face:

Hope that helps.
Regards
Peasant.