Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fix Ubuntu and other Linux slow/hanging file copying via USB.

If your running a x64 bit Ubuntu or other Linux and find USB transfers hang at the end apply this fix:

echo $((16*1024*1024)) > /proc/sys/vm/dirty_background_bytes
echo $((48*1024*1024)) > /proc/sys/vm/dirty_bytes

I suggest you edit your /etc/rc.local file to make this change persistant across reboots.

sudo nano /etc/rc.local

Go to the bottom of the file and leave a space then paste in those two lines.

Save the file with ctrl + x then press y.

To revert the changes enter this in console and remove the lines in /etc/rc.local

echo 0 > /proc/sys/vm/dirty_background_bytes
echo 0 > /proc/sys/vm/dirty_bytes

More info and references: https://unix.stackexchange.com/questions/107703/why-is-my-pc-freezing-while-im-copying-a-file-to-a-pendrive/107722#107722

@jorgeribeiro

This comment has been minimized.

Copy link

@jorgeribeiro jorgeribeiro commented Oct 24, 2018

It worked just fine, thank you so much!

After searching a lot in different sources, this was the only answer to actually work.
I use Linux Mint 18.

@Oldbuntu

This comment has been minimized.

Copy link

@Oldbuntu Oldbuntu commented Jan 24, 2019

Thank's this really work for Ubuntu 18.04.Thank one's again

@Rockburner

This comment has been minimized.

Copy link

@Rockburner Rockburner commented Feb 20, 2019

I tried entering these lines into the file, but now a 1Gb file that previously took less than a minute to copy, after which 'files' would hang for over an hour, is taking up to an hour to copy at something like 255kB/sec.
For reference the machine is an I5 with 16GbRAM running ubuntu 18.04. The USB stick is a new 256Gb SanDisk.
Are there some better byte values I can try to get a better balance between the latency and the hanging?

I can't use the lines directly in the terminal either - I just get 'Permission Denied', even using sudo.

@yamal4321

This comment has been minimized.

Copy link

@yamal4321 yamal4321 commented May 2, 2019

@Rockburner type sudo su

@Triztan

This comment has been minimized.

Copy link

@Triztan Triztan commented Jun 3, 2019

Good one! Worked for me in elementary OS 5.0 Juno

@mohamedmaged

This comment has been minimized.

Copy link

@mohamedmaged mohamedmaged commented Jun 5, 2019

I am interested to understand more why these two commands worked ? thanks for your time in advance :)

@texadactyl

This comment has been minimized.

@alainalemany

This comment has been minimized.

Copy link

@alainalemany alainalemany commented Jul 24, 2019

I have no /etc/rc.local file. Should I create it and paste those two lines? Or I have to look for the equivalent rc.local file in my SO (KDE Neon 5.16 btw).

Thanks in advance.

@rajeshisnepali

This comment has been minimized.

Copy link

@rajeshisnepali rajeshisnepali commented Oct 25, 2019

Thanks very much..

@cipricus

This comment has been minimized.

Copy link

@cipricus cipricus commented Oct 27, 2019

@alainalemany - Yes, create the file if absent.

(By the way, the same file can help preventing mouse movement from waking up a suspended computer: https://askubuntu.com/q/252743/925128).

@SaltyPaws

This comment has been minimized.

Copy link

@SaltyPaws SaltyPaws commented Dec 4, 2019

This fix did not fully work for me. After I applied the fix, I would still experience machine freeze but less frequently. What did work was disabling the swap file. I first tried increasing the swap file to 64 GB, this did not work, I then disabled the swap file altogether. I am on a dual CPU server with 32 GB memory. I did run memtest extensively, so I know it is not a memory issue. I did copy from exFAT 2 TB system (via usb) to ZFS Z2 6x10TB array.

@stdonato

This comment has been minimized.

Copy link

@stdonato stdonato commented Dec 12, 2019

SaltyPaws, disable swap works fine. Thanks.

@fharookshaik

This comment has been minimized.

Copy link

@fharookshaik fharookshaik commented Jan 30, 2020

It didn't worked with my system.
my system is of 8gb ram,intel i5-8300H running on ubuntu 18.04.3 and the external storage is 2TB Seagte UltraSlim Hardisk

@roeniss

This comment has been minimized.

Copy link

@roeniss roeniss commented Mar 20, 2020

with 32 RAM, 128 SSD, 16 USB, It worked.

but I really wonder what exactly this two lines do. Anyway Thankyou.

@KarthikKavinA

This comment has been minimized.

Copy link

@KarthikKavinA KarthikKavinA commented Apr 5, 2020

Thank you very much.
It works better for Ubuntu 18.04.3

@axdeveloper

This comment has been minimized.

Copy link

@axdeveloper axdeveloper commented May 20, 2020

for those who dont have /etc/rc.local can use this:
sudo sysctl -w vm.dirty_bytes=50331648
sudo sysctl -w vm.dirty_background_bytes=16777216

@RS0307

This comment has been minimized.

Copy link

@RS0307 RS0307 commented Sep 1, 2020

I AM NEW ON UBANTU CAN ANYONE PLEASE TELL ME FROM ZERO STEP BY STEP TO DO ALL USB HANG ANSWERS
I HAVE ALSO THIS ISSUE BUT I AM NOT GETTING HOW TO FIND /ETC/RC.LOCAL

CAN ANYONE HELP ME

@RS0307

This comment has been minimized.

Copy link

@RS0307 RS0307 commented Sep 1, 2020

I AM NEW ON UBANTU CAN ANYONE PLEASE TELL ME FROM ZERO STEP BY STEP TO DO ALL USB HANG ANSWERS
I HAVE ALSO THIS ISSUE BUT I AM NOT GETTING HOW TO FIND /ETC/RC.LOCAL

CAN ANYONE HELP ME

@RS0307

This comment has been minimized.

Copy link

@RS0307 RS0307 commented Sep 1, 2020

It worked just fine, thank you so much!

After searching a lot in different sources, this was the only answer to actually work.
I use Linux Mint 18.

can you please tell me how to do

@Lumumba

This comment has been minimized.

Copy link

@Lumumba Lumumba commented Oct 3, 2020

for those who dont have /etc/rc.local can use this:
sudo sysctl -w vm.dirty_bytes=50331648
sudo sysctl -w vm.dirty_background_bytes=16777216

is this persistent or do i have to apply this every boot?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.