I'm trying to get a stream off port 9999 and write to /dev/xvdf. I'm using Amazon EC2 with the Ubuntu 11.10 server image. By default I log in as 'ubuntu' which has sudo privileges. However, when I run the following netcat command, I get this error

ubuntu@ip-10-252-35-122:~$ ls -al /dev/xv*
brw-rw---- 1 root disk 202,  1 2011-11-30 22:22 /dev/xvda1
brw-rw---- 1 root disk 202, 80 2011-11-30 22:27 /dev/xvdf
ubuntu@ip-10-252-35-122:~$ sudo netcat -p 9999 -l > /dev/xvdf
bash: /dev/xvdf: Permission denied

Any idea why I get the permission denied error and how I can work around it?

Update: Something mysterious is running in the background that resets the permissions? Check the snippet below and the permission flags seem to automatically reset when I try using /dev/xvdf !?!

ubuntu@ip-10-252-35-122:~$ sudo chmod 777 /dev/xvdf
ubuntu@ip-10-252-35-122:~$ ls -al /dev/xvdf
brwxrwxrwx 1 root disk 202, 80 2011-11-30 22:43 /dev/xvdf
ubuntu@ip-10-252-35-122:~$ sudo nc -p 9999 -l > /dev/xvdf
This is nc from the netcat-openbsd package. An alternative nc is available
in the netcat-traditional package.
usage: nc [-46DdhklnrStUuvzC] [-i interval] [-P proxy_username] [-p source_port]
          [-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_protocol]
          [-x proxy_address[:port]] [hostname] [port[s]]
ubuntu@ip-10-252-35-122:~$ ls -al /dev/xvdf
brw-rw---- 1 root disk 202, 80 2011-11-30 22:43 /dev/xvdf

I'm using stock Ubuntu Amazon EC2 images from http://alestic.com/ (links to images at the top)


When you run sudo netcat -p 9999 -l > /dev/xvdf, you are redirecting the standard output of sudo netcat -p 9999 -l to /dev/xvdf. At this point, you do not have superuser privileges, resulting in the permission denied error when the shell tries to open the device with write access.

What you really want to do is perform the output redirection after acquiring superuser privileges. This could be done by performing the redirection in a shell instance invoked by sudo. For example:

sudo sh -c 'netcat -p 9999 -l > /dev/xvdf'

Or alternatively, run netcat without elevated privileges but pass its output to a privileged process:

netcat -p 9999 -l | sudo tee /dev/xvdf >/dev/null
  • Thanks, there was also an error where netcat shouldn't be launched with -p and -l at the same time. My fault. However, sudo sh -c 'netcat -l > /dev/xvdf' worked for me ! – DeepSpace101 Dec 1 '11 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.