Touch - changing date and time


 
Thread Tools Search this Thread
Operating Systems Linux Red Hat Touch - changing date and time
# 1  
Old 01-09-2015
Touch - changing date and time

Hi,

I am facing a problem with the command - TOUCH on Linux.

See the example below:

File on Linux: rw-rw-r-- user1 user1 Jan 01 09:00 test.txt

The file - test.txt was created by the user - user1.

Now, I want to change the date and time, but using other user - user2

The user2 belongs to the same user1 group.

if the command: touch -c -t 201501091030 test.txt - is issued, I have been receiving an error that the operation is not permitted.

However, if I use the command: touch -t 201501091030 xxx.txt , at the same directory, the file xxx.txt is created.

Does anyone know if the touch command is not allowed to change any information from other user, even if it is the same group?

Does anyone know how to solve it?

Tks.
# 2  
Old 01-09-2015
Quote:
The user2 belongs to the same user1 group.
Can you verify that? Run groups as user2. Remember that adding a user to a group only takes effect if they re-login.

It looks like it ought to work to me.
# 3  
Old 01-09-2015
Hi Corona688,

yes, and the user2 belongs to the same group of user1.

if I use just the command: touch -c test.txt , it works - the current date/time is setting, but any other date/time, it didnt work.

If it is not possible, I would like to know if anyone knows a workaround for this problem.

tks.

---------- Post updated at 11:47 AM ---------- Previous update was at 11:38 AM ----------

Hi,

I was looking for more information about this issue and I found out the website: linux - Change file time (touch) - Stack Overflow and the problem is the same, however, it seems that there is not a solution:

"On Linux you must be the file owner (or root) to change the modification time to a time other than the current time. There are some other restrictions as well. man utime for complete details."

Even though, I am still looking for a workaround for it.

Tks.

---------- Post updated at 11:52 AM ---------- Previous update was at 11:47 AM ----------

BTW, the file needs to remain to the same owner, I cannot change the owner for the file.
# 4  
Old 01-09-2015
Quote:
Originally Posted by brjohnsmith
"On Linux you must be the file owner (or root) to change the modification time to a time other than the current time."
Interesting. I did not know that.
Quote:
Even though, I am still looking for a workaround for it.
Doing it as another user without permissions would be a "bug" or "security hole", not a workaround. You'll just have to arrange for it to be done as a different user. You could use sudo.

In sudoers:

Code:
%user1 ALL=(user1) NOPASSWD:  /path/to/update-time.sh

# % means 'group user1'.  If you only wanted user2 in particular to
# be able to do so, use this instead.
# user2 ALL=(user1) NOPASSWD: /path/to/update-time.sh

In update-time.sh:

Code:
#!/bin/sh

NEWUSER="user1"
ALLOW="/path/to/folder"

if [ "$#" -ne 2 ]
then
        echo "Must be run with exactly two parameters, time and file" >&2
        echo "like $0 201501091030 test.txt" >&2
        exit 0
fi

# If we're not $NEWUSER already, quit and reload from the beginning as $NEWUSER
[ "$USER" = "$NEWUSER" ] || exec sudo -u "$NEWUSER" "$0" "$@"

# $2 is the filename.  Convert relative paths to absolute ones,
# so we can check if they're inside /path/to/folder.
case "$2" in
/*) FILE_LOCATION="$2" ;;
*)  FILE_LOCATION="${PWD}/$2" ;;
esac

case "$FILE_LOCATION" in
$ALLOW/*)
        touch -c -t "$1" "$FILE_LOCATION"
        ;;
*)
        echo "you are only allowed to update files inside the $ALLOW folder" >&2
        exit 1
esac

This will allow user2 to run /path/to/update-time.sh 201501091030 file.txt to update the time of files inside the /path/to/folder directory, and nowhere else.

Last edited by Corona688; 01-09-2015 at 01:30 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 01-09-2015
Hi Corona688,

Thank you for providing me this code, but the problem is that I never know who is the user, because it could be anyone, it is not a specific user. Besides this, the folder where the file is, it could be anyone as well.

Think this:

userX running the application on Linux. This application was sending to Linux the command: touch -c -t 201501091010 /xxx/yyy/zzz/file.txt .

for the example above, I build on Linux application the date/time, the folder and the filename.

as this script is executed by the application, I thought that this script could run under root and the touch command would be executed as well. I am not sure if it is possible a script be executed as root via sudoers.

would it be possible? How the script could be and the changes on sudores?

tks.
# 6  
Old 01-09-2015
Quote:
Originally Posted by brjohnsmith
Hi Corona688,

Thank you for providing me this code, but the problem is that I never know who is the user, because it could be anyone
Read it more carefully please. sudo will allow anyone in the group user1 to touch user1's files.

If anyone not in the user1 group tries to use the script, sudo will refuse it.

If the file in question does not actually belong to the user user1, touch will refuse it.

Quote:
Besides this, the folder where the file is, it could be anyone as well.
Do you mean anyone or anywhere?

If you're content with anyone in the user1 group being able to set dates on any one of user1's files, you can rip out my error checking in a quarter-second.

Quote:
as this script is executed by the application, I thought that this script could run under root and the touch command would be executed as well.
Never never never.

Any bugs in the script, etc can only do as much damage as the user it runs under.

If you have no reason to run it as root, never run it as root.

Quote:
I am not sure if it is possible a script be executed as root via sudoers.
It is possible. I just showed you how to do it.
Quote:
Would it be possible? How the script could be and the changes on sudores?
I showed you an example script and the necessary changes to sudoers.

Last edited by Corona688; 01-09-2015 at 02:22 PM..
# 7  
Old 01-09-2015
Quote:
NEWUSER="user1"
in your script, the user1 is a static user, as the file could be created by anyone, how do I change it to be according to the file owner?


Quote:
Do you mean anyone or anywhere?
anywhere, sorry....

Quote:
If you have no reason to run it as root, never run it as root.
I dont need to run as root, the idea is to use root instead of any other users, just to make easy the process, because it could be anyone to run the touch command, and the file owner could be anyone as well. Besides this, I cannot change the owner.

So, if you will provide me other option, I would appreciate it a lot.

tks.

Last edited by brjohnsmith; 01-09-2015 at 03:33 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. SCO

Changing date time will cause the X11 restart

I am using SCO Unix System V/386 Release 3.2v4.2 in some legacy machine. I find that when I change the date time of the system, it will sometimes restart to the scologin page It seems to be related to the X11 because other sessions (not scologin session) will not be affected. Is there... (10 Replies)
Discussion started by: dannychan
10 Replies

2. UNIX for Advanced & Expert Users

Help with touch: bad time specification

Here is the part of the script: I have modified the file name.:) SSFILE=${My_HOME_DIR}/log/my_file_ss.log export MM=`date '+%m'` export DD=`date '+%d'` export HH=`date '+%H'` export MIN=`date '+%M'` export HOURAGO=`echo ${HH} -1 |bc ` echo $HOURAGO export TTIME=${MM}${DD}${HOURAGO}00... (5 Replies)
Discussion started by: N1a_Raider
5 Replies

3. Shell Programming and Scripting

How to change time stamp with touch command?

Hi, I wish to change time stamp of a directory with all its subdirectories and files on server. I am able to find following two ways but want to know which will be the better one. I have not tried anyone of them because I am not sure if it can effect my data: find * -type d -exec touch... (5 Replies)
Discussion started by: bioinfo
5 Replies

4. Linux

Strange, date and touch returning different times.

Hi guys, Hope one of you guys can sort this out for me. I first noticed this when users were uploading files from our website to our server and the time uploaded was +18 hours ahead of the current time. At first I assumed this was an issue with the coding, but didn't think it was likely as we... (2 Replies)
Discussion started by: markn86
2 Replies

5. UNIX for Advanced & Expert Users

Changing Time and date on Virtual server

Hello Everyone I am running a Sunos 5.10 Generic_138888-02 sun4u sparc SUNW,SPARC-Enterprise machine with more than one virtual servers are on this box. My question is possable to change the date and time on the virtual server without change the time on the main server? Thanks Peter (4 Replies)
Discussion started by: Peterh
4 Replies

6. UNIX for Dummies Questions & Answers

touch -t time, using different userid

Hi, I am reciveing files from a remote system on my linux box. These files are named based on time, which I can use to 'touch' the time . I can access/modify these files using my id. but when I tried touching time using my id I am getting error; touch -t 1001261234 1001261234_job2333... (15 Replies)
Discussion started by: rajivbravo
15 Replies

7. Shell Programming and Scripting

how to make a log.txt and add date and time when use ls,touch and find

Hey guy, how to make the log.txt file and record date and time when ls, touch and find command run? Thanks Boly (13 Replies)
Discussion started by: chenboly
13 Replies

8. Shell Programming and Scripting

how to touch a file with prev time stamp

i want to find the files which are modified in last 30 to 120 minutes i am using "find . -mmin +30 -mmin -120 " it is giving me the error find: bad option -mmin find: path-list predicate-list can somebody help me out . Thank you (5 Replies)
Discussion started by: Prat007
5 Replies

9. UNIX for Dummies Questions & Answers

Changing Creation Date to a Prespecified Date of a File In Unix

Dear Expert, Is there a command to do that in Unix? In such a way that we don't need to actually "write" or modified the content. -- monkfan (4 Replies)
Discussion started by: monkfan
4 Replies
Login or Register to Ask a Question