OpenBSD Set Date and Time Command

last updated in Categories , ,

I am a new OpenBSD system user. How do I set new date and time under an OpenBSD UNIX server or desktop?

You can set date and time using date command on OpenBSD. You must be a root user i.e. login as root user either using the su command or doas command. When you run date command without any arguments, the command displays the current system date and time. This page shows how to set data and time on OpenBSD based system using the CLI.

OpenBSD see the current data and time

Simply type the following date command:
$ date
OpenBSD see the current data and time command

OpenBSD set date and time command

The syntax is as follows:
date yyyymmddhhss
Run it via the doas command:
doas date yyyymmddhhss
where,

  • yyyy – Year
  • mm – Month
  • dd – Day
  • HHSS – Hour Second in 24 clock format
WARNING! These examples requires root level privileges (you must login as root).

Set the date to May 10, 1985, 4:27 PM, enter:
# date 198505101627
OR
$ doas date 198505101627
Set the time to 3:42 PM, without modifying the date, enter
# date 1542
OR
$ doas date 1542
To display current date and time, enter date command without option:
$ date
Sample outputs:

Fri May 10 15:42:01 IST 1985

How to automatically set correct date and time without using date command

Simply run the following command:
$ doas ntpd -s
OpenBSD Set Date and Time Commands
I strongly suggest that you configure ntpd on OpenBSD to sync date and time (clock) using the Internet. The default config file /etc/ntpd.conf should work without any problems. Here is my ntpd.conf disaplyed using the ntpd command
$ doas rcctl enable ntpd
$ doas rcctl start ntpd
$ doas rcctl ls started

How to display date and time in specific format on OpenBSD

The syntax is:
date +"format"
To display the date using the specified format string:
$ date +"%d/%m/%Y"
$ echo "Today is `date +'%A, %B %d %Y'`"
date "+DATE: %Y-%m-%d%nTIME: %H:%M:%S"

OpenBSD see date and time in custom format

An date command with a leading plus sign (+) signals a user-defined format:

  • %Y : Year
  • %m : Month
  • %d : Day
  • %n : New line
  • %H : Hour
  • %M : Minutes
  • %S : Seconds
  • %A : Full weekday name
  • %B : Full month name

See strftime(3) man page for full list of format here.

Conclusion

You just learned about setting up date and time on OpenBSD. I strongly suggest you read the man pages for more info using the man command:
man 3 strftime
man date

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

1 comment

    Have a question? Post it on our forum!