0

There are several blogs online saying if sync is run as sudo this will sync all filesystems, but if it's run normally (no sudo) then it will sync "current" filesystem.

But it's not clear what is meant by "current", which filesystem is current?

If I have 3 disks where each has 2 ext4 partitions totaling in 6 filesystems then which one is "current", which one will sync work against if not run with sudo?

EDIT:

Blogs which state this:

https://www.baeldung.com/linux/sync-command

https://phoenixnap.com/kb/linux-sync

Quotes:

Generally, to sync the current file system, we run the sync command without any option:

$ sync

While using sync without arguments synchronizes the current file system, executing the command with sudo syncs all mounted file systems.

8
  • 3
    The only answer I can give is that this is completely wrong. The sync program doesn't care who called it: if you call it with no arguments, it calls the sync system call. And the sync system call doesn't care who called it: it syncs all filesystems. There are ways to sync only one filesystem, but they don't depend on the level of privileges, only on the instructions given (arguments passed to the sync command or using libc functions such as fsync or syncfs (with various underlying system calls)). Commented Aug 2, 2024 at 18:29
  • 2
    Your desire not to throw shade is commendable. OTOH, it’s useful to establish which sites have a track record of peddling bad information. (For example, my [anti-]favorite is TLDR, which is very misleading.) (1) Section 1 of the Baeldung page says “Additionally, sync can be helpful after crashes or when the file system becomes corrupted.” This is wrong and wrong. Sync is useful before a crash — it’s not clear how you would use it after a crash. And, when a file system becomes corrupted, sync can make it worse. … (Cont’d) Commented Aug 2, 2024 at 21:50
  • 2
    (Cont’d) …  (2) Section 3 of the Baeldung page says “Also, if we run sync with sudo privileges, it syncs all mounted systems.”  Section 1 of the phoenixNAP page says the same thing (with slightly different wording).  I agree with @Gilles and waltinator that this is also wrong.  But the word “mounted” is not just filler.  It means something, and you should not have left it out when you quoted it. … (Cont’d) Commented Aug 2, 2024 at 21:50
  • 2
    (Cont’d) …  (3) The phoenixNAP page says, “Without any arguments, the sync command synchronizes all cached data for the current user to the permanent memory.”  This is wrong; as the other respondents say, sync synchronizes all cached data for all users.  (4) The phoenixNAP page says, “The command is helpful for syncing individual files on the entire file system.”  This is probably a typo for “… syncing individual files or the entire file system.” Commented Aug 2, 2024 at 21:50
  • 2
    "Current file system" is rather meaningless. Would that be the FS containing the current user's home directory, or the FS containing the current $PWD, or some other interpretation? Commented Aug 2, 2024 at 21:52

1 Answer 1

2

The sync program (/bin/sync) is a simple wrapper around the sync() system call (look at strace /bin/sync), and doesn't even check for root (UID == 0).

The sync() system call documentation (man 2 sync) says nothing about requiring root.

/bin/sync has been around forever (since the Unix days), ha has always been available to users. It was originally used in the "Oh F*, it's about to crash" situation, and does no harm to the system.

Your references are wrong. sync is sync() and is for everybody.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.