I always take a few locks with me and you should too.
Hostels sometimes provide locks for sale but it’s cheaper to get them at the store.
What kind of locks do you buy? The problem with thick or short locks is that every locker is different. Therefore your lock might not fit and it’d be convenient if the lock also worked for your backpack as well as the locker. The solution is to buy long cable locks. The cable is narrow enough to swop into zippers for your backpack and long enough to use on lockers.
Purchase TSA approved locks if you’re traveling within the US at any point. TSA has a master key to open those specific locks instead of having to break it. The locks have a red and white symbol on it.
Having keys is a big responsibly while you’re traveling, purchase a cable lock that has a combination you can set and don’t forget the combination!
Most hostels don’t have a locker bigger enough for an entire backpack. I usually lock up my valuable in the locker while keeping all my clothes still in my backpack. I try to avoid hostels that don’t have lockers because I have too many electronics but it does happen. If the hostel doesn’t have any lockers, I always lock up all my electronics into the bag and stuff it with clothes. I hide the lock so it’s not obvious that I’m locking up anything important in the bag. I’ve seen some people lock up their bags to their bed poles but I always thought it was too obvious so I never did that.
If all else fails and you don’t have a lock, sleep with your day bag with your valuables. It’s super uncomfortable but it has helped on bus trips and train rides.
You can lock-up your zippers on your backpack for when it’s under the bus but many of them bring knives to cut bags anyway if they plan to steal. Don’t put anything valuable under the bus. Don’t put your electronics bag under your seat either. Just keep it on your lap the whole time. Again, it’s uncomfortable but better than losing it all.