6

I am using multiple UTS namespaces, each of them has some servers constantly running.

I can get the namespace IDs via readlink /proc/<pid>/ns/uts, now I would like to change into a given namespace to do modifications there.

As far as I understand from man 2 setns it should be possible to do this, by using the setns() system call. There is even an example program which could do what I am looking for, but I was hoping that there is already some utility that most distributions include which can do the same job.

The unshare command doesn't seem to work for me because it only creates new namespaces to launch processes in, but it doesn't allow me to specify an existing namespace.

replay
  • 8,553
  • 1
  • 27
  • 31
  • I'd be pretty amazed if there would be such a tool as writing one is trivial as you point out and the application for this is specific (more or less) when running containers. I'd guess everyone has again compiled their own and decided it's not worth the effort to push a 20 line program to any distribution. –  Dec 30 '13 at 11:53
  • @SamiLaine I hoped that since somebody has already done that for unshare and the utility is even in the util-linux package it might as well be that somebody has done it for setns – replay Dec 30 '13 at 15:23

1 Answers1

5

On the 11. of January 2013 the utility nsenter has been added to the util-linux package. This is the commit together with its comment:

commit f8aa8e9495dd89f39b7d0db5aab431cac02ea519
Author: Eric W. Biederman <ebiederm@xmission.com>
Date:   Fri Jan 11 14:46:38 2013 -0800

nsenter: new command (light wrapper around setns)

Inspired by unshare, nsenter is a simple wrapper around setns that
allows running a new process in the context of an existing process.

Full paths may be specified to the namespace arguments so that
namespace file descriptors may be used wherever they reside in the
filesystem.

Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>

The first release of util-linux that includes nsenter is 2.23 released in April 2013.

Unfortunately most distributions are currently still using older versions of util-linux which don't include this utility yet.

replay
  • 8,553
  • 1
  • 27
  • 31