Your own personal IT department

Unlock network files locked by Samba Linux / Unix Server

Samba is a free Linux/Unix software which provides file and print access for Microsoft Windows clients. If you have a Linux file server or Linux based NAS device on a network with Windows clients you are probably using Samba.

Samba deals with file locking, preventing file corruption and data loss when two or more clients try to simultaneously edit the same file. There may be situations where you need to force file unlock, for example when file was closed but lock was not properly released by Samba.

Unfortunately Samba doesn’t allow unlocking individual files. There are couple of workarounds though.

Restart Samba

Quickest and most dirty way is to restart samba. Problem is that all users will be temporarily disconnected from the server, potentially resulting in data loss. To restart samba, run:
/etc/init.d/samba restart
Command may be slightly different depending on your Linux distribution.

Kill user’s PID

Better option is to kill Samba process which is locking the file. Samba assign a separate process to every connected user. This way only files locked by that particular user will be unlocked. It won’t drop the connection in the same way as restarting samba does.

First run:
smbstatus
and find the locked file. Note PID number (first column)
smbstatus
If list is very long and you are struggling to find the file run:
smbstatus | grep -i filename.ext
where filename.ext is name of the locked file.
This will filter the smbstatus results and return only lines with your locked file.

Now run smbstatus again (or return to the first query) and in the top table find the user with the PID number you noted. This will be the user the who is locking the file.

smbstatus
You can also check what other files this user has opened by inspecting smbstatus results or running:

smbstatus | grep 14899
where 14899 user’s PID number

If possible, ask user to close all files opened from this particular server.
Finally, to unlock all files this user has opened, run

kill 14899
where 14899 user’s PID number