Linux fuser Command Tutorial With Examples – POFTUT

Linux fuser Command Tutorial With Examples


Linux have a lot of tools to manage processes and network. But fuser is one of the most popular tool which combines process and network management in a single command. fuser can display process according to their directory, file and network handles. Also it can kill these process according to various parameter.

Syntax

fuser [options] [file|socket]

Help

$ fuser -h
Help
Help

Display Processes Using Directory

In daily usage of a server there will be a lot of users and processes. Finding related process about opened directory can be hard. fuser command provides feature simply find related processes according to directory usage. In the example we will provide the directory path we want to inspect which is /home/ismail . Best way to use fuser is with root privileges

$ fuser /home/ismail
Display Processes Using Directory
Display Processes Using Directory

From output we can see that some process ID are printed. There is also some characters after PIDs. These are used to specify access type and information about the process. Here is what they mean

  • c – current directory
  • e – executeable is running
  • f – open file
  • F – open file for writing
  • r – root directory
  • m – mmaped file or shared library

Display More Information Like User And Command

In the previous example we have only listed PID’s. But this information can not be enough some time. We may need more information like the user name and related command. To get user name and related command more verbose output is needed. Verbose output can be displayed with -v option. In the example we will list information about directory access of processes with user and command information in a column based output.

$ fuser -v /home/ismail
Display More Information Like User And Command
Display More Information Like User And Command

In the example out we can see that our looking path is /home/ismail and process id is 2548 which is the PID of bash cmomand with user ismail

Display Processes Using File System

Process access information about the file system can be displayed with -m option. This will create enormous amount of output.

$ fuser -v -m /home/ismail/.bashrc
Display Processes Using File System
Display Processes Using File System

Display Processes About TCP and UDP Sockets

Another useful feature of fuser command is listing process information of TCP and UDP sockets. This can be very useful if our Apache web server do not starts because of the TCP 80 is used by another process we do not know. We can find this process easily. Furthermore this process can be killed with a single command which we will look below examples. In the example we want to list processes information listening TCP 22. We need root privileges to get information. We will provide -n option with tcp 22 parameter

$ fuser -v -n tcp 22
Display Processes About TCP and UDP Sockets
Display Processes About TCP and UDP Sockets

In order  to use with UDP ports look following command.

$ fuser -v -n udp 53

Kill Process Using Specified TCP Socket

As previously stated we can kill process by specifying TCP or UDP ports or sockets in a single command. We will provide -k option to kill related process. In the example we will kill all ssh or TCP port 22 process.

$ sudo fuser -k -n tcp 22
Kill Process Using Specified TCP Socket
Kill Process Using Specified TCP Socket

We can see that we have killed our current ssh connection to our server.

LEARN MORE  How To Configure RIP Routing For Cisco IOS?

List Signals

In previous example we have killed the ssh process. Killing a process is done by sending signal to the process. Signals are used to communicate with process. To kill a process kill related signals are sent. There is also different type of signals those may send to the related process. We can list signals  list-signals option like below.

$ fuser -list-signals
List Signals
List Signals

Kill Process With Specific Signal

As we stated we can send specific signal to the process we have specified. In the example we will send -HUP signal to the TCP 22 or ssh port with the following command.

$ sudo fuser -k -HUP -n tcp 22
Kill Process With Specific Signal
Kill Process With Specific Signal

Kill Process Interactively

While killing process in the critical servers  we have no room for mistakes. But issuing fuser command will execute command in real time without any caution. We can prevent mistakes and kill related processes in a more controlled way with interactive option. Interactive option is provided with -i

$ sudo fuser -i -k -n tcp 22
Kill Process Interactively
Kill Process Interactively

1 thought on “Linux fuser Command Tutorial With Examples”

Leave a Comment