They definitely know. If they’re domestic, they know if they harm their owner good things (food, shelter, positive interaction) will stop happening. My friend’s puppy, while playing, nipped my hand. He didn’t injure me, but it did hurt, and I said, "ouch, stop !". The puppy stopped immediately, looked worried, then licked my hand gently. When I petted him, he wagged his tail again, like he knew he was forgiven. And he seemed careful not to nip me again.