LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2012, 11:11 AM   #1
Nachete
LQ Newbie
 
Registered: Oct 2012
Posts: 3

Rep: Reputation: Disabled
Find the sockets being used by a process (as a non-root user)


I have to find the sockets being used by any running process on the system. I can't install external libraries or apps in order to get that information.

I tried with netstat -a -p, fuser lsof -i and ss -p, and all of them work fine... but only if I run them as root, and I should be logged in as a non-root user. In that case, I get the sockets, but not them owner processes. Also, if I use netstat to get the local port of every socket, and then I do fuser -n tcp [lcl_port], I don't get any information (of course, unless I am root).

I tried running those commands as a non-root user in privileged mode: bash -c - p netstat -a -p, bash -c -p fuser -n tcp [lcl_port], bash -c -p lsof -i and bash -c -p ss -p. But I get similar results.

I also tried accessing the /proc/net directory, doing something like cat /proc/[PID]/net/tcp, but it seems to return exactly the same info for any PID, so it isn't really useful for me.

By the way, if I am not logged in as root, I can't redirect the output of lsof -i to the input of grep command. By example, if I type something like lsof -i | grep firefox, I get this message:

Quote:
can't stat() fuse.gvfs-fuse-daemon file system /root/.gvfs
Output information may be incomplete.
How can I find the sockets being currently used by running processes as a non-root user?

Thanks in advance.
 
Old 10-08-2012, 02:21 PM   #2
nini09
Senior Member
 
Registered: Apr 2009
Posts: 1,854

Rep: Reputation: 161Reputation: 161
If not run as root, you will only see the names of PID you own. The root user is super user. It can see other user information. You can use "sudo" prefix to upgrade current user to root.
 
1 members found this post helpful.
Old 10-08-2012, 04:59 PM   #3
propofol
Member
 
Registered: Nov 2007
Location: Seattle
Distribution: Debian Wheezy & Jessie; Ubuntu
Posts: 334

Rep: Reputation: 60
Another option similar to 'sudo' is to use 'super'. Add the commands to /etc/super.tab:

Code:
:define SuperUsers user

checkport  'netstat -a -p'              uid=root $SuperUsers
Run by entering:
super checkport

Regards,
Stefan

Last edited by propofol; 10-08-2012 at 05:00 PM.
 
Old 10-08-2012, 10:18 PM   #4
techguru666
LQ Newbie
 
Registered: Jul 2012
Posts: 24

Rep: Reputation: Disabled
Check various usages of netstat command:
http://www.expertslogin.com/tip-for-...riant-outputs/
 
Old 10-10-2012, 04:11 PM   #5
Nachete
LQ Newbie
 
Registered: Oct 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks to everyone, but I still can't solve the problem.
I have to get the information through a Bash script that should be executed by a non-root user. The script verifies if there are any open TCP/UDP sockets owned by current user's processes; if this is true, it kills those processes.
So, I only need to find the current user's sockets and their associated processes. It's supposed that I could easily get that information just by doing lsof -i logged in as a "normal" user. So, in order to try the command, I open a socket through establishing a connection from another PC, doing this:

Quote:
ssh nachete@192.168.x.x 'while true; do sleep 2; echo $RANDOM; done'
Then, from my main PC, logged in as root, I check the current sockets for nachete:

Quote:
root@utnso-vm:~# lsof -i | awk '$3 == "nachete" {print $0}'
sshd 5834 nachete 3u IPv4 23609 0t0 TCP utnso-vm.local:ssh->192.168.1.37:35753 (ESTABLISHED)
It finds the socket! However, when I do the same logged in as nachete:

Quote:
nachete@utnso-vm:~$ lsof -i | awk '$3 == "nachete" {print $0}'
nachete@utnso-vm:~$
... I got nothing.

If I try using netstat -a -e -p, it recognizes the nachete sockets as sockets owned by root:

Quote:
root@utnso-vm:~# netstat -a -e -p | grep 23609
tcp 0 0 utnso-vm.local:ssh 192.168.1.37:35753 ESTABLECIDO root 23609 5711/sshd: utnso
Because of that, if I look for nachete sockets using this command, I got nothing:

Quote:
root@utnso-vm:~# netstat -a -e -p | awk '$7 == "nachete" {print $0}'
root@utnso-vm:~#
(in both computers, the hostname is utnso-vm; the username on the second PC is utnso)

It's not allowed to me to use the super command, so I need a way to obtain the information I want without doing that. Anyone knows?
 
Old 10-10-2012, 07:38 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Unfortunately in your case repeating the question does not change the answer.
 
Old 10-14-2012, 09:34 AM   #7
Nachete
LQ Newbie
 
Registered: Oct 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
I think I solved my problem. The error was in the way I was testing my script. Apparently, when you establish a connection through SSH, the client host creates a socket as owned by the current user, but the server creates a socket as owned by root. I was running the ssh command from a host and trying to find the sockets from the other one (using netstat, lsof -i, etc.). That's why I couldn't find any socket unless I was root.
Finally, I ran lsof -i on the same host I had established the connection from, logged in as a non-root user, and it found a socket associated to the ssh process, owned by that user.
Thanks again and sorry for my english.
 
Old 10-14-2012, 10:00 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Maybe your wording threw us off (after all you went from "find the sockets being used by any running process (..) logged in as a non-root user" to "sockets owned by current user's processes") but it's good to see you found what you need.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change a process running in root-user to non-root user ???????????????????? narendra1310 Linux - Software 4 10-29-2009 02:11 AM
How to run daemon process being a normal user [not root user]. narendra1310 Linux - Software 1 10-26-2009 09:48 AM
IPC fails between user process and root process zaryab Linux - Newbie 1 09-22-2008 01:25 AM
Start a program for a user as root, with process belonging to user gnashley Programming 4 03-19-2007 01:58 PM
Raw sockets root user work around P_Shep Programming 2 11-18-2003 11:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 12:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration