108

I have:

  • ISO image of Windows 7 install media
  • 4 GB USB flash drive
  • no DVD drive
  • Linux installed
Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24
Ian Kelling
  • 2,661
  • 6
  • 23
  • 21
  • If you could manage to do all [these tasks](http://serverfault.com/questions/2952/boot-and-install-windows-from-a-usb-thumb-drive) from within your linux desktop, you'll be rocking the house. I think the toughest part would be fiddling with all the operations that Windows' Diskpart does. Making the partition marked as 'active' and 'boot', 'primary', etc. – p.campbell May 10 '09 at 00:17
  • 3
    I think GParted (http://gparted.sourceforge.net/) can handle all the partition flags just fine. See e.g. http://gparted.sourceforge.net/screenshots.php . And I haven't tested but it looks like rsync can substitute for robocopy in that checklist. – Matthew Flaschen May 10 '09 at 02:47
  • 1
    There are many detailed answers to the same question on http://askubuntu.com/q/289559/135671 – erik Nov 23 '15 at 10:37
  • Comment about WoeUSB is much better than Accepted answer! Also AA is inaccurate - DOS or GPT label? Wasted 3 hours with "ms-sys + ntfs/fat32 + gpt/msdos, lurking for efi in bios, changing flash drives" but finally, WoeUSB done the job in several minutes! Probably grub is more robust than original MBR. – Oleg Gritsak May 11 '21 at 19:36

4 Answers4

185

OK, after unsuccessfully trying all methods mentioned here, I finally got it working. Basically, the missing step was to write a proper boot sector to the USB stick, which can be done from Linux with ms-sys or lilo -M. This works with the Windows 7 retail version.

Here is the complete rundown again:

Install ms-sys - if it is not in your repositories, get it here. Or alternatively, make sure lilo is installed (but do not run the liloconfig step on your local box if e.g. Grub is installed there!)

Check what device your USB media is assigned - here we will assume it is /dev/sdb. Delete all partitions, create a new one taking up all the space, set type to NTFS (7), and remember to set it bootable:

# cfdisk /dev/sdb   or   fdisk /dev/sdb (partition type 7, and bootable flag)

Create an NTFS filesystem:

# mkfs.ntfs -f /dev/sdb1

Write Windows 7 MBR on the USB stick (also works for windows 8), multiple options here:

  1. # ms-sys -7 /dev/sdb
  2. or (e.g. on newer Ubuntu installs) sudo lilo -M /dev/sdb mbr (info)
  3. or (if syslinux is installed), you can run sudo dd if=/usr/lib/syslinux/mbr/mbr.bin of=/dev/sdb

Mount ISO and USB media:

# mount -o loop win7.iso /mnt/iso
# mount /dev/sdb1 /mnt/usb

Copy over all files:

# cp -r /mnt/iso/* /mnt/usb/   ...or use the standard GUI file-browser of your system

Call sync to make sure all files are written.

Open gparted, select the USB drive, right-click on the file system, then click on "Manage Flags". Check the boot checkbox, then close.

...and you're done.

After all that, you probably want to back up your USB media for further installations and get rid of the ISO file... Just use dd: # dd if=/dev/sdb of=win7.img

Note, this copies the whole device! — which is usually (much) bigger than the files copied to it. So instead I propose

# dd count=[(size of the ISO file in MB plus some extra MB for boot block) divided by default dd blocksize] if=/dev/sdb of=win7.img

Thus for example with 8 M extra bytes:

# dd count=$(((`stat -c '%s' win7.iso` + 8*1024*1024) / 512)) if=/dev/sdb of=win7.img status=progress

As always, double check the device names very carefully when working with dd.

The method creating a bootable USB presented above works also with Win10 installer iso. I tried it running Ubuntu 16.04 copying Win10_1703_SingleLang_English_x64.iso (size 4,241,291,264 bytes) onto an 8 GB USB-stick — in non-UEFI [non-secure] boot only. After execution dd reports: 8300156+0 records in 8300156+0 records out 4249679872 bytes (4.2 GB, 4.0 GiB) copied, 412.807 s, 10.3 MB/s

Reverse if/of next time you want to put the Windows 7 installer onto USB.

jthurner
  • 1,966
  • 2
  • 12
  • 5
  • 1
    Would be nice to mention the numerical partition type to use in cfdisk as well (7, 86, 87?) – Johan Dahlin Sep 05 '10 at 21:03
  • 5
    It worked with type 7 – Ropez Sep 15 '10 at 20:56
  • 3
    Just a note: If your ntfs filesystem gives weird permission errors when you write to it even as root, make sure you've installed ntfs-3g. – Jeremy Salwen Jun 24 '11 at 06:32
  • 1
    I tried this directly onto a hard drive, and I got the error "windows cannot access the installation sources" once I booted up from it. – Jeremy Salwen Jun 24 '11 at 07:21
  • Worked except I didn't need to run `ms-sys` - Just set the boot flag in GParted. – l0b0 Nov 17 '11 at 22:09
  • 1
    Ubuntu PPA for ms-sys (12.04): ppa:berkon/ppa – Dmitry Verkhoturov Jun 05 '12 at 18:15
  • Note: this method works only with NTFS, not FAT32 unfortunately. – Lekensteyn Jul 10 '13 at 17:59
  • 4
    call `sync` after the copy to make sure all files are written – Nicolas Dusart Nov 14 '14 at 10:43
  • You first need to mkdir /mnt/iso and /mnt/usb, otherwise the mount fails. – Amit Moscovich Apr 11 '15 at 15:28
  • 1
    Does this work on a windows 10 ISO? – enigmaticPhysicist Nov 20 '15 at 07:09
  • @l0b0 would be nice to know if it worked because your motherboard's firmware supports USB-CD (not just USB-HDD or whatever) or if it really works by just using the 'boot flag'. Seems strange to me because just the flag should not be the sole reason for it to work. And.. EnigmaticPhysicist should work. – José May 14 '16 at 01:35
  • How do I call `sync` command? Just like that? Where do I need to be located? – Erik Escobedo Jun 30 '16 at 02:44
  • @ErikEscobedo Just execute it on the command line. It will cause buffers to be flushed to disk, any disk. – David Tonhofer Aug 15 '16 at 15:58
  • Just a friendly advice: write the commands in a script, or in several scripts. If you have more than one disk, the USB drive is unlikely to be `sdb`, and executing commands one by one from the site to the terminal, changing the `b` to something else each time, can be a risky business! – Déjà vu Oct 27 '17 at 06:10
  • Also, using `fdisk`, if you need to create a partition table, type `o` (MBR). – Déjà vu Oct 27 '17 at 06:44
  • Finally, why using `gparted` to set the boot flag, as it was done during the `fdisk` step? (btw, better unmount `/mnt/usb` before using gparted!) – Déjà vu Oct 27 '17 at 07:03
  • I cannot answer here sue to rep but https://askubuntu.com/a/928874/220798 has a much easier answer with WoeUSB – eddygeek May 30 '18 at 07:32
  • Do I have to dedicate the whole device to the installer? Can I use just a primary partition in it instead? I have an 1TB external HDD and it would be nice if I can have an installer on just a partition. – Calmarius Sep 08 '18 at 11:29
  • 3
    Did not work for me until I called `ms-sys -n /dev/sdb1` before `ms-sys -7 /dev/sdb` (all that after copying the files). See https://wycd.net/posts/2015-03-10-creating-a-bootable-windows-10-usb-drive-on-linux.html – gsc Oct 10 '20 at 07:51
  • 1
    When you `cp` files of the ISO, do it as root. – Big Shield Oct 17 '20 at 10:48
  • 1
    I got the BIOS error "this is not a bootable disk" until I ran `ms-sys -n /dev/sdb1` on the partition like the commenter above. The ms-sys manual says that this flag "is probably of no practical use" but it seems that it is in this case. So to clarify, you need to run `ms-sys -7` on the BLOCK DEVICE (i.e. /dev/sdb) and `ms-sys -n` on the PARTITION (i.e. /dev/sdb1). – JShorthouse Nov 29 '21 at 21:30
13

You can accomplish this with dd, if your PC supports UEFI, and, by extension, GPT disks.

Open up a terminal, your going to need to find what device is your pendrive. If you have the drive mounted you can find the name of the device by typing "mount" and looking at it's entry. Something like the following:

/dev/sdb1 on /media/USBDISK type vfat (rw,nosuid,nodev,uhelper=hal,uid=1000,utf8,shortname=mixed)

In this case the first partion of /dev/sdb is mounted at /media/USBDISK. Open a root shell and unmount the drive.

umount /dev/sdb1

Go to the directory where your ISO is stored in a root shell and type in the following: (Replace windows7.iso with whatever the iso is called, and /dev/sdb with the device id of your usb stick).

dd if=windows7.iso of=/dev/sdb

If your motherboard supports booting off of a pendrive it should be able to boot off it. This will get the installer on the pendrive not the OS itself.

TrueDuality
  • 1,844
  • 5
  • 27
  • 37
  • 22
    Doesn't work for me. –  Jun 02 '10 at 16:41
  • 1
    Does your motherboard support booting from USB? That'll be the ultimate deciding factor of whether this approach will work or not. – TrueDuality Jun 04 '10 at 14:46
  • 11
    Doesn't work for me either and my machine does support USB booting just fine. Maybe this approach can work on machines that can boot DVD-style USB-storage but most BIOSes assume HDD-style layout on USB (meaning 512 byte MBR with boot code in it). On thing worth a mention: Fedora CD/DVD images, unlike W7, do in fact have such MBR with boot code and partition table on them, making them suitable for HDD/USB boot too. – Tronic Aug 28 '10 at 17:35
  • 9
    This does require that your motherboard is able to boot USB-CDROM not just USB-HDD – TrueDuality Oct 05 '10 at 12:43
  • 12
    This solution is missing the MBR-installation step, and will typically not work unless it magically is there already. The solution by @Gunthers is complete. – stolsvik Dec 25 '11 at 01:30
5

PCambell's suggestion is good but you will also want to clear the MBR, the linux equivalent is below

I tried this and it worked (I'm not sure why the dd method failed but seems the partition had to be ntfs?):

  • work out which /dev/device is your usb flash drive and unmount it
  • clear the MBR: dd if=/dev/zero of=/dev/device bs=446 count=1
  • run fdisk /dev/device
  • remove all partitions and create 1 primary partition, make it bootable then save the changes
  • run mkfs.ntfs /dev/device1 (partition 1)
  • copy the entire contents of the windows install iso on to the partition you created
-4

Instead of

dd if=windows7.iso of=/dev/sdb

you write as root,

dd if=windows7.iso of=/dev/sdb1 

(ADD 1 at the end or whatever your USB drive is placed at.)

Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24
gonzo
  • 1