The easy way is to log out after you're done. Your personal changes only last until you log out. That being said, intervening on someone's account is a common use case.
Type/paste this into your terminal (running bash or zsh):
xkb=$(xkbcomp :0 -); trap 'xkbcomp - :0 <<<"$xkb"' 0 1 2 15; setxkbmap dvorak
If you either exit the shell normally (
exit, Ctrl+D) or close the terminal normally (which will send a SIGHUP), i.e. under normal ways to exit the shell session, the trap will restore the original keymap.
Don't do this in multiple terminals at the same time: the first one to exit would restore the original configuration, and the last one to exit would restore whichever configuration was in place when you ran this command.
If you know what keymap to restore and only want to handle normal terminal closure, you can simplify this to e.g.
trap 'setxkbmap us' 0 1; setxkbmap dvorak
Beware that this doesn't restore customizations that people may have made through a typical keyboard settings GUI configurator! (Which is essentially options present in standard XKB maps.) If you want to restore the user's customizations, you need to save the original keymap with
xkbcomp :0 - as above.
Alternatively, use an interface that provides per-window switching layout, and stick to the one terminal window (remember not to launch any GUI application). Some desktop environments provide this feature (e.g. XFCE), as does kbdd. Kbdd is unlikely to be available, let alone configured, on a default installation, but maybe your default desktop environment has that feature.
A few remarks that don't apply to your use case, but apply to the use case of intervening on a user's machine while logged into their account:
- This can introduce subtle differences in layouts that were originally set by
xmodmap, but people who rely on such subtleties would know how to restore their hand-crafted keymap from a layout where the basics work.
- I'm not sure if the current active level is restored correctly in multi-level (i.e. multi-language) configuration, but again, people who routinely switch between layouts would just use their usual method to switch layouts.
- This may interfere badly with layout switching provided through a background application that changes the XKB layout (as opposed to changing levels). In such a case, you should ask them to switch to Dvorak through their layout-changing method.