How to change the Linux time by the command line?

Asked

Viewed 12,143 times

6

I can see Linux time from the command line like this:

 >date 
 Qua Mai 25 14:49:58 BRT 2016

But now I need to change the Linux time by the command line, because as I use a Linux server, I only have access via SSH.

How to change the time of Linux from the command line?

Note: I know it is not in the scope of the site to ask questions about operating system, but as it is a problem related to programming posted here. All my systems were on the wrong date because of this, but I was able to solve with the solution given in the answer (Thank you for the answer).

  • Sorry, I hadn’t read the SSH part. Ignore the removed answer

  • I do not think it is outside the scope, I believed it would be a famous vote for revenge but has just received another -1. It would be a case of resorting to META?

  • 3

    @Wallacemaxters didn’t give the -1, but I voted to close, don’t get me wrong, but just because you have the problem, it doesn’t mean that it is in scope (that’s what you implied in the comment). I think the question belongs to the superuser.

  • 3

    It was not I who gave -1, however, I voted to close as out of scope, your doubt this more directed the configuration of the system than in something related to programming, if I was wondering how to change the time of the served via shell script would even be valid, maybe here may be a suitable place to ask about terminal commands.

  • You might think it was me @Wallace Maxters, but no. I even agree that you should not give an answer without being sure that it can work. I think it’s as legitimate a question as any other

  • @drmcarvalho this site of the network I didn’t even know, it seems even more indicated that the superuser.

  • 1

    @Miguel, by the way, your answer was correct ;)

  • 1

    But yours deserves more, more complete and safe mutio. I replied without testing, I wasn’t sure

  • I still don’t understand why negative points are a question that can help many programmers who manage their servers on Amazon for example. Deserves +1

  • 1

    You can replace the file /etc/localtime' pelos arquivos da pasta /usr/share/zoneinfo`, they are responsible for the time zone, (if you want to change only the time)

  • (Delayed) For most UNIX/Linux commands the option --help brings the basic help related to this command. Example: date --help

Show 6 more comments

2 answers

10

I had made an answer, but it was change by terminal, my fault. Good to change by command line in SSH is very easy too.

Change time (ex. 10 hours and 52 minutes: 10:00:00):

date +%T -s "10:52:00"

Change date (ex: 25 October 2016, 18 hours):

date -s "25 OCT 2016 18:00:00"

And I’ll also leave the option of @Miguel that posted on a short time response, but unfortunately erased.

Change date and time:

sudo date –s "25 DEZ 2016 15:04:00"

Change time only:

sudo date +%T -s "15:04:00"

Basically sudo works with the command date –set="STRING"

0

I was able to make a script that uses the command "doas" which runs root commands without password, I could only get the time right

site='https://www.horariodebrasilia.org/'
dia=$(curl -s "$site" | grep -Poh '(?<=\<h3 id="dia-topo"\>).*(?=\</h3\>)')
hora=$(curl -s "$site" | grep -Poh '(?<=\<p id="relogio"\>).*(?=\</p\>)')

# echo "$dia"
# echo "$hora"

doas date +%T -s "$hora"

The options for grep are:

-P ........... perl regex (expressão regular avançada)
-o ........... imprime somente o padrão buscado
-h ........... não imprime nome de arquivo
(?<=padrão) .. positive look behind 
(?=padrão)  .. positive look ahead

I thought it was also possible to use the Beautifulsoup4 python library to take the values and use the sys library or something similar to set the right time on the system.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.