2

I have a problem with an embedded computer where it does not have a separate Real Time Clock and as such every time I reboot it, it loses track of the current date. This is very annoying because lots of processes depend on checking file time stamps.

I can connect to this machine through ssh and and can manually set the date every time I login but it is tedious and prone to forgetting and creating havoc on the time stamps. The machine with no date is the ssh server and I connect with my PC as an SSH client.

What I want is to know is how to set the date on the remote server(embedded machine) with the date of the SSH client (my laptop) automatically upon login.

I thought I should do it by setting the ssh_config of my client to execute a LocalCommand but from what I read in the man it does not work as I want. The command would be like the one described in this question but with the date.

The other way I thought would be that the .profile of the user I connect to the remote machine would try to connect back to my laptop and run date to update it's date.

How should I accomplish this?

  • 3
    Have you considered running NTP on the server? – Henrik May 14 '16 at 14:34
  • 3
    If you can't run an ntp daemon, you could run ntpdate or rdate once, during startup, to get the date right. – Mark Plotnick May 14 '16 at 14:40
  • Even if you do run an ntp daemon you need to run ntpdate once during startup, because the daemon will not modify the date if the offset is too large. I think that is often taken care of in the daemon startup scripts, though. – Law29 May 14 '16 at 15:26
  • I never considered the NTP server because I thought it was a bit overkill. May be i am wrong. So I could use the NTP server to connect to my desktop to query the date? Would I need any daemon also on my laptop? Also the server (embedded computer) is not connected to the internet. – Paulo Neves May 14 '16 at 15:44
  • What OS does your client/laptop run? – Jeff Schaller May 14 '16 at 16:38
3

You can set the date with

ssh root@embedded "date -s '$(date)'"

(i.e. run date locally and give the output to date -s on the remote system.)

Of course you need another ssh command to open the interactive session, so either use keys and ssh-agent or a master connection to login.

Normally, I would have also recommended using NTP, but if the machine doesn't have an internet connection, you'd need to set up an NTP server on some machine it can reach. Possibly the laptop.

The same goes for running ntpdate/rdate/whatever from the login script: you need a machine running something to serve the time for the embedded system.

0

You can create a script on your local server or any other Linux server within your command:

ssh root@server "date -s '$(date)'"

You can run it with cron every hour or every few minutes as you like. However, its looks like not the optimal thing to do.

Is your embedded system has the internet access or network access to any other Linux server you have?

The best way to implement what you need is to use NTP client at the embedded system. It will request the date and time data from NTP server and set local ones according to it.

You can read about how to install NTP server and client here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.