virli/tutorial/4/setns-samples.md

914 B

Exemple C

Voici un exemple de code C utilisant setns(2) :

```c #define _GNU_SOURCE #include #include #include

// ./a.out /proc/PID/ns/FILE cmd args...

int main(int argc, char *argv[]) { int fd = open(argv[1], O_RDONLY); if (fd == -1) { perror("open"); return EXIT_FAILURE; }

if (setns(fd, 0) == -1)
{
  perror("setns");
  return EXIT_FAILURE;
}

execvp(argv[2], &argv[2]);

perror("execve");
return EXIT_FAILURE;

}

</div>


#### Exemple shell

Dans un shell, on utilisera la commande `nsenter(1)` :

<div lang="en-US">
```bash
42sh# unshare --uts /bin/bash
  inutsns# echo $$
  42
  inutsns# hostname jument
  # Keep this sheel active to perform nexts steps, in another shell.

42sh# hostname
chaton
42sh# nsenter --uts=/proc/42/ns/uts /bin/bash
  inutsns# hostname
  jument