Skip to content



dup2 - Duplicate an open file descriptor.


The dup2() system call performs the equivalent of dup(), but instead of using the lowest-numbered unused file descriptor, it uses the file descriptor number specified in newfd. If the file descriptor newfd was previously open, it is silently closed before being reused.

The dup2() system call can be used, for example, to convert a disk file descriptor fd into a network socket descriptor (which is really just a file descriptor number). If newfd is already open, it is first closed. Then its value is applied as the new duplicate descriptor so that dup2(fd, newfd) is equivalent to close(newfd); dup2(fd, newfd).

The dup2() system call does not cause the file offset (See lseek(2)) and file status flags to be changed.


  • oldfd:int[K] - Descriptor of the file that is going to be duplicated.
  • newfd:int[K] - Descriptor that is going to contain the duplicate.

Available Tags

  • K - Originated from kernel-space.






To trace the duplication of an open file descriptor.

Example Use Case

A use case for the dup2 system call is to duplicate a file descriptor that has been opened in order to write to an alternate location, while still leaving the file descriptor that was opened pointing to the original location. This might be useful if you want to preserve the original file descriptor, but need to make modifications to a different location.


The dup2() system call does not cause the file offset and file status flags to be changed.

The relation between dup2() and dup() and fork().

This document was automatically generated by OpenAI and needs review. It might not be accurate and might contain errors. The authors of Tracee recommend that the user reads the "events.go" source file to understand the events and their arguments better.