1

I'm trying to run a script which downloads a file from a VM, but I cannot specify the VM that is passed in. However, I want to ensure that the download is successful regardless of the state of the passed in VM, and I know it is a unix/linux system.

My question is - is there a specific file I can use to download that would be guaranteed to exist on all unix/linux systems?

Thanks!

jj172
  • 313

4 Answers4

14

Just about the only thing that's guaranteed to exist is the root directory itself. There are a lot of standards that are almost universally adhered to, but it's entirely possible for a bored systems engineer to roll out a Linux build where /etc lives in /andsoforth, /bin lives in /cupboard, /usr lives in /luser, and so forth. In such a system, all bets are off.

DopeGhoti
  • 76,081
  • 5
    +1 for /andsoforth and /cupboard. Thanks for all the Ghoti. – Timothy Martin Apr 26 '18 at 23:12
  • /etc/passwd is almost sure to exist. You'll find /bin/sh most of the time, directories /lib (or perhaps /lib64), and also /usr, usr/bin, /usr/lib and perhaps /usr/lib64 – vonbrand Apr 27 '18 at 17:13
  • 4
    You're missing the point of my answer entirely in that while any system that conforms to the well established standard will indeed have /bin/sh and /etc/passwd, they are in fact not guaranteed to exist at those locations. If /etc is moved by a sufficiently bored/deviant/creative administrator to /et_cetera, then the passwd file now lives at /et_cetera/passwd and /etc/passwd does not exist. – DopeGhoti Apr 27 '18 at 17:52
  • 2
    @vonbrand, and then some joker hands you a copy of GoboLinux, and you find yourself using /Programs/bash/5.0/bin/bash as the default shell. – Mark Jan 21 '22 at 01:40
  • When I saw the title of the question, I immediately thought of GoboLinux, I just couldn't remember it's name. – Henrik supports the community Jan 21 '22 at 08:49
  • @vonbrand being Unix or Unix-like doesn't mean that FHS is used – phuclv Jan 21 '22 at 13:59
7

The /etc/passwd is commonly used by security professionals as proof of concept/ flag capture exercise, and for a good reason - this file exists on every unix like system.

However, this is worth noting that it doesn't mean passwd will be the main access database, as other databases such as LDAP play a role, too.

  • I think this answer makes more sense than the other... – Rui F Ribeiro Apr 27 '18 at 07:42
  • 3
    The thing is, /etc/passwd is not guaranteed to exist, as it's possible to move /etc, which is the main point of my answer. – DopeGhoti Apr 27 '18 at 15:17
  • @DopeGhoti It's absolutely true, but 1) is it a normal occurrence and unix filesystem hierarchy standard exists just for lols and giggles, or is the owner of the system consciously moving /etc somewhere else ? 2) POSIX doesn't guarantee it but pwd.h and the fact that this file exists on even older versions of *nix systems makes it a good candidate. I'm sure both your and this answer can be beneficial, it's just the question of context. – Sergiy Kolodyazhnyy Apr 27 '18 at 16:13
  • 1
    Also this https://github.com/hak8or/Embedded-Linux-System/blob/master/at91sam9n12/gists/kernel-booting-without-rootfs.txt – Sergiy Kolodyazhnyy Apr 27 '18 at 16:30
  • 3
    I agree; it's just that magic word 'guaranteed' in the question that makes it a hard question to answer. It's kind of a shame an OP can't split their acceptance between to equally valid answers (: – DopeGhoti Apr 27 '18 at 16:56
2

POSIXly, the only things that are promised ("shall exist on conforming systems") are /, /dev, /tmp, /dev/null, /dev/tty and /dev/console, though not all may be writable (or even readable).

Depending on what you use to copy the files, all that is either really not helpful, or you could try to copy /dev/null. scp seems to give an error when trying to copy it, but if you use something that just reads the data and ignores the file being special, like cp /dev/null ., you end up with a successfully copied zero-length regular file.

But if you try to copy it with something copies the device node, you probably can't create it on the local system without privileges, and I don't think there's any guarantee the device numbers are the same across systems, so if you can copy it, the result might be a device node that means something completely different on the local system.

The standard utilities are supposed to be in PATH, so if you can parse that on the remote, you should be able to find e.g. sh or ls in some listed directory (though they might be symlinks). On a conforming system, you should also be able to find cd, but Linuxes don't tend to have it, since it's pretty much useless as an external utility.

In practice, you'll probably find /bin/ls or /bin/sh in many systems (though they still might be symlinks). Maybe not on the most esoteric ones, but you probably know if you're dealing with those.

ilkkachu
  • 138,973
-1

Based on my experience:

  • /dev/stdout, although that's sometimes a symlink, so /dev/null might be better, although neither of these is a plain file
  • /etc/hosts and probably /etc/resolv.conf would have to exist for any UNIX system that is networked
  • /bin/sh and /bin/init are highly likely to exist, but I can imagine some embedded Linux systems or Linux-powered appliances and NAS boxes could have one of these in a different location, or be a symlink (eg to /bin/busybox) rather than a file; although /etc/inittab is perhaps more likely to exist AND be a plain file
  • /etc/issue and /etc/profile seem to exist everywhere that anyone can login to a shell.
AdminBee
  • 22,803
jezzaaaa
  • 21
  • 2
  • I wouldn't count on /bin/init: my Gentoo system has it as /sbin/init, and I've seen plain /init used by some systems. – Mark Jan 21 '22 at 01:45
  • 1
    As @DopeGhoti has pointed out, /etc can be renamed by a suitably pathological or paranoid administrator. /dev could also be changed. Very non-standard but the key word in the question is "guaranteed". – doneal24 Jan 21 '22 at 02:19
  • I'm not sure /etc/hosts is really required, even on a "normal" OS. resolv.conf looks to be rather common, though. I think inittab is tied to SysV-style init, and not all systems might have it. At least the "FreeBSD Quickstart Guide for Linux® Users" (from 2008, on freebsd.org) says "Under the BSD-style init(8), there are no run-levels and /etc/inittab does not exist." Systemd probably doesn't require inittab either, I guess? – ilkkachu Jan 21 '22 at 13:47