No particular rules apply to signing a book, except a caution to remember that whatever you say will last longer than you think, often surfacing many years later.
Your name, where you are, and the date are often enough. Book collectors often want only your signature and nothing else. Establish this before you sign.
DVICE. As long as you are willing, always ask what the person would like--that is, do they want it addressed to themselves, is it a birthday present, or whatever. People are often shy, so you should try to be sure you are doing what they actually want.
If you are going to address this to someone in particular, be absolutely certain you know the spelling of the person's name. Don't make any assumptions--people spell their names in all sorts of ways. Always ask first, even if you don't think you need to.
Cleverness has an expiration date. After awhile, what you wrote won't seem so clever, and in fact may even smell like curdled milk. Try to remember this every time you are tempted. Yes, curdled milk.
If you find yourself in a situation with many people, particularly young people--an elementary or high school, for example, a few students may have actual books for you to sign. But when the signing starts, kids will want you to sign all sorts of things. And if you do it once, you will have to do it for everyone, the way you have to share candy. Only you are the candy. When you sense that this is going to happen, cook up a quick strategy. For example, start using only initials for your first name. It will go much faster.
If you want to say something private, this is not the place.
Some authors put a line through their name as it is printed in the book, replacing it with a signature below. This is personal style. Look at the layout of your book and decide on a signing protocol you simply like regarding placement and general approach. Some people use particular pens or ink color, for example. There are all kinds of eccentricities that can be fun. But remember the first caution--you will likely see this book again somewhere in your future.