# Unexpected Error When Creating RAW Socket Without Sudo

**URL:** <https://ziggit.dev/t/unexpected-error-when-creating-raw-socket-without-sudo/9978>\
**Category:** Help\
**Tags:** standard-library\
**Created:** [May 5, 2025, 9:37pm UTC](https://ziggit.dev/t/unexpected-error-when-creating-raw-socket-without-sudo/9978 "2025-05-05T21:37:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jibarra](https://ziggit.dev/letter_avatar_proxy/v4/letter/j/8797f3/32.png) [@jibarra](https://ziggit.dev/u/jibarra)\
**Post date:** [May 5, 2025, 9:37pm UTC](https://ziggit.dev/t/unexpected-error-when-creating-raw-socket-without-sudo/9978/1 "2025-05-05T21:37:04Z")

</div>

Hey guys,

Why is this error.Unexpected rather than error.AccessDenied?

I’m attempting to create a raw socket using std.posix. When running my program without sudo I’m getting an unknown error. Running with sudo returns no error. Im a noob so if someone could enlighten me that would be cool 😎.

Function call:

```zig
const socket: std.posix.socket_t = try std.posix.socket(std.posix.AF.INET, std.posix.SOCK.RAW, std.posix.IPPROTO.ICMP)

```

Im running zig 0.14.0 on popOS.

---

<div class="post-metadata">

**Author:** ![cryptocode](https://ziggit.dev/user_avatar/ziggit.dev/cryptocode/32/817_2.png) [@cryptocode](https://ziggit.dev/u/cryptocode)\
**Post date:** [May 5, 2025, 10:05pm UTC](https://ziggit.dev/t/unexpected-error-when-creating-raw-socket-without-sudo/9978/2 "2025-05-05T22:05:28Z")

</div>

Hey, and welcome to the forum! The error you see is not a Zig issue per se. It’s due to raw sockets requiring special permissions on your OS.

You can use `setcap` to set this capability on the executable as explained [here](https://squidarth.com/networking/systems/rc/2018/05/28/using-raw-sockets.html)

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [May 5, 2025, 10:43pm UTC](https://ziggit.dev/t/unexpected-error-when-creating-raw-socket-without-sudo/9978/3 "2025-05-05T22:43:33Z")

</div>

Welcome to ziggit @jibarra 🙂

Zig handles all the error number cases listed in macos and linux manual page for `man 2 socket`, including `EACCES` defined as errno 13, meaning “Permission denied”.  
But the linux man page also says:

> Other errors may be generated by the underlying protocol modules.

The manual page `man 7 packet` lists some more error numbers that the zig source code of `socket` currently does not handle. For example `EPERM` is defined as errno 1, meaning “Operation not permitted”, with the following description in the manual page:

> User has insufficient privileges to carry out this operation.

* * *

What is the error number displayed after the message `unexpected errno: `?

---

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [May 6, 2025, 2:58am UTC](https://ziggit.dev/t/unexpected-error-when-creating-raw-socket-without-sudo/9978/4 "2025-05-06T02:58:55Z")

</div>

> <https://github.com/ziglang/zig/issues/20524>
>
> \### Zig Version
> 
> 0.14.0-dev.66+1fdf13a14
> 
> \### Steps to Reproduce and Observe…d Output
> 
> this test outputs the following when not run with sudo:
> 
> \`\`\`
> error: 'main.test.socket permissions error' failed: unexpected errno: 1
> \`\`\`
> errno 1 corresponds to \`E.PERM\` (permission denied when creating socket)
> 
> This errno is expected because creating a raw socket generally requires elevated privileges.
> 
> \`\`\`zig
> test "socket permissions error" {
> const ETH\_P\_ETHERCAT: u16 = 0x88a4;
> const socket\_result = std.posix.socket(
> std.posix.AF.PACKET,
> std.posix.SOCK.RAW,
> std.mem.nativeToBig(u32, ETH\_P\_ETHERCAT),
> );
> try std.testing.expect(socket\_result == std.posix.SocketError.PermissionDenied);
> }
> \`\`\`
> 
> 
> 
> 
> this is because std.posix.socket does not expect \`.PERM\`:
> https://github.com/ziglang/zig/blob/master/lib/std/posix.zig#L3519
> 
> \`\`\`zig
> switch (errno(rc)) {
> .SUCCESS =\> {
> const fd: fd\_t = @intCast(rc);
> errdefer close(fd);
> if (!have\_sock\_flags) {
> try setSockFlags(fd, socket\_type);
> }
> return fd;
> },
> // \<------------------ .PERM is missing here!
> .ACCES =\> return error.PermissionDenied,
> .AFNOSUPPORT =\> return error.AddressFamilyNotSupported,
> .INVAL =\> return error.ProtocolFamilyNotAvailable,
> .MFILE =\> return error.ProcessFdQuotaExceeded,
> .NFILE =\> return error.SystemFdQuotaExceeded,
> .NOBUFS =\> return error.SystemResources,
> .NOMEM =\> return error.SystemResources,
> .PROTONOSUPPORT =\> return error.ProtocolNotSupported,
> .PROTOTYPE =\> return error.SocketTypeNotSupported,
> else =\> |err| return unexpectedErrno(err),
> }
> }
> 
> \`\`\`
> 
> when \`.PERM\` is is actually expected.
> 
> So the switch should probably have the following in it:
> 
> \`\`\`zig
> .PERM =\> return error.PermissionDenied,
> \`\`\`
> 
> \### Expected Output
> 
> we expect \`std.posix.socket\` to return \`error.PermissionDenied\` when the syscall returns \`E.PERM\`.

---

<div class="post-metadata">

**Author:** ![jibarra](https://ziggit.dev/letter_avatar_proxy/v4/letter/j/8797f3/32.png) [@jibarra](https://ziggit.dev/u/jibarra)\
**Post date:** [May 6, 2025, 3:45pm UTC](https://ziggit.dev/t/unexpected-error-when-creating-raw-socket-without-sudo/9978/5 "2025-05-06T15:45:11Z")

</div>

Thanks for sharing this!

---

<div class="post-metadata">

**Author:** ![jibarra](https://ziggit.dev/letter_avatar_proxy/v4/letter/j/8797f3/32.png) [@jibarra](https://ziggit.dev/u/jibarra)\
**Post date:** [May 6, 2025, 3:47pm UTC](https://ziggit.dev/t/unexpected-error-when-creating-raw-socket-without-sudo/9978/6 "2025-05-06T15:47:14Z")

</div>

You’re totally right I am getting errno 1. This exactly answered my question, Thanks!
