OpenBSD Mount And Use a USB Flash Drive

How do I mount and use a USB pen or hard disk under OpenBSD operating systems using ksh/bash/sh shell prompt?

OpenBSD does supports USB storage devices such as pen and hard disk via emulated SCSI drives. You need to login as root user to mount USB pen/disk.
Tutorial details
Difficulty Easy (rss)
Root privileges Yes
Requirements OpenBSD
Time 2m

ADVERTISEMENTS

Finding out your USB device name on an OpenBSD based system

First, insert your your USB flash drive in USB socket. Type the following command to list device names:

# sysctl hw.disknames
Sample outputs:

hw.disknames=wd0:3af96a8b7d621ab2,cd0:,sd0:

To get more info about sd0 (my usb pen), type:
# dmesg | grep sd0
Sample outputs:

sd0 at scsibus1 targ 1 lun 0:  SCSI2 0/direct removable
sd0: 1956MB, 512 bytes/sector, 4005888 sectors
sd0 detached
sd0 at scsibus1 targ 1 lun 0:  SCSI2 0/direct removable
sd0: 1956MB, 512 bytes/sector, 4005888 sectors

The following command shows more information about sd0
# disklabel sd0
Sample outputs:

# /dev/rsd0c:
type: vnd
disk: vnd device
label: fictitious
duid: 97d41b01a9d85387
flags:
bytes/sector: 512
sectors/track: 100
tracks/cylinder: 1
sectors/cylinder: 100
cylinders: 5120
total sectors: 4005888
boundstart: 0
boundend: 512000
drivedata: 0 

16 partitions:
#                size           offset  fstype [fsize bsize  cpg]
  a:           512000                0  4.2BSD   2048 16384    1 
  c:          4005888                0  unused                   
# 

Mounting USB Pen/Hard Disk on an OpenBSD

Type the following command to create a directory (mount point), enter:
# mkdir /mnt/pen

Mount the flash drive using mount command at /mnt/pen, enter:
# mount /dev/sd0i /mnt/pen
You may need to replace /dev/sd0i with actual device name. Now, you can access the files at /mnt/pen:
# cd /mnt/pen
# ls -l
# cp filename /path/to/dir

Finally, unmount the flash filesystem using the following command:
# umount /mnt/pen

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
9 comments… add one
  • Allan Jun 30, 2009 @ 16:51

    “You may need to replace /dev/sd0i with actual device name.”

    For people new to OpenBSD, how do you find the actual device name?

  • 🐧 nixCraft Jun 30, 2009 @ 19:34

    Use dmesg. The device shows up in dmesg output after plugging it in. You can also use usbdevs.

  • 100 Jun 7, 2010 @ 0:59

    How did you know the ‘i’ in sd0i? Dmesg only shows ‘sd0’ with no ending.

    • 100 Aug 7, 2010 @ 12:44

      Answering my own question:

      $ disklabel sd0

      At the end, you will see the partitions. The i-label denotes a partition.

  • dean Oct 3, 2010 @ 15:37

    another way
    # sysctl hw.disknames

    will show (example)
    $ sysctl hw.disknames
    hw.disknames=wd0,wd1,cd0

  • mitch Sep 12, 2017 @ 4:08

    How do I write to the drive as a non-root user?

    Thanks

  • Roman Sep 20, 2017 @ 11:41

    mitch, you can use doas. Right?

    $ doas cp /mnt/pen

    https://man.openbsd.org/doas.conf

    Add to /etc/doas.conf:

    permit nopass

    • mitch Dec 8, 2017 @ 22:26

      duh, yes I can!

      • jones Dec 12, 2017 @ 4:06

        What if you want a non-root program to be able to write (save) dirctly to the directory?
        i can’t seem to figure this out.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.