Tag Info

Hot answers tagged

131

You can run the loader directly, and pass it the command you want to run: /lib/ld-linux.so /bin/chmod +x /bin/chmod Your path to the loader might vary, and on a 64-bit system you need to choose the right one based on how chmod was compiled; the 64-bit version is named something like /lib/ld-linux-x86-64.so


73

bash does cache the full path to a command. You can verify that the command you are trying to execute is hashed with the type command: $ type svnsync svnsync is hashed (/usr/local/bin/svnsync) To clear the entire cache: $ hash -r Or just one entry: $ hash -d svnsync For additional information, consult help hash and man bash.


68

The chmod utility relies on the chmod() system call (see man 2 chmod). So you could do this with a few lines of C, or just about any other language that has a wrapper around it (which would be most of them). Very few *nix systems are going to lack a C compiler and a perl interpreter; most linux distros require the later to work. perl -e 'chmod 0755, ...


46

Some systems also have busybox installed in which case you may run: busybox chmod +x /bin/chmod Since you were asking for hacks, I just thought of another one: mv /bin/chmod /bin/chmod.orig cp -a /bin/chown /bin/chmod Now you have a /bin/chmod that's executable but it's actually chown (i.e. some other binary). Now all we have to do is overwrite it with ...


45

The literal answer is as others have given: because the current directory isn't in your $PATH. But why? In short, it's for security. If you're looking in someone else's home directory (or /tmp), and type just gcc or ls, you want to know you're running the real one, not a malicious version your prankster friend has written which erases all your files. Having ...


40

If you mean, why do you need ./ at the start - that's because (unlike in Windows), the current directory isn't part of your path by default. If you run: $ ls your shell looks for ls in the directories in your PATH environment variable (echo $PATH to see it), and runs the first executable called ls that it finds. If you type: $ a.out the shell will do ...


38

The whole ABI is different, not just the binary format (Mach-O versus ELF) as sepp2k mentioned. For example, while both Linux and Darwin/XNU (the kernel of OS X) use sc on PowerPC and int 0x80/sysenter/syscall on x86 for syscall entry, there's not much more in common from there on. Darwin directs negative syscall numbers at the Mach microkernel and ...


31

For your specific script either way will work, except that ./script.sh requires execution and readable bits, while bash script.sh only requires readable bit. The reason of the permissions requirement difference lies in how the program that interprets your script is loaded: ./script.sh makes your shell run the file as if it was a regular executable. ...


28

/usr/local/bin is for programs that a normal user may run. The /usr/local hierarchy is for use by the system administrator when installing software locally. It needs to be safe from being overwritten when the system software is updated. It may be used for programs and data that are shareable amongst a group of hosts, but not found in /usr. Locally ...


28

You can determine the nature of an executable in Unix using the file command and the type command. type You use type to determine an executable's location on disk like so: $ type -a ls ls is /usr/bin/ls ls is /bin/ls So I now know that ls is located here on my system in 2 locations:/usr/bin/ls & /bin/ls. Looking at those executables I can see ...


27

The shebang #! is an human readable instance of a magic number consisting of the byte string 0x23 0x21, which is used by the exec() family of functions to determine whether the file to be executed is a script or a binary. When the shebang is present, exec() will run the executable specified after the shebang instead. Note that this means that if you invoke ...


26

Easy. What you can do is prepare some other executable file, and then cp chmod over it. $ cp /bin/ls chmod $ cp /bin/chmod . The first cp creates a file called chmod with executable permissions, but which is really the ls executable. The second cp populates this file with the binary code of chmod, while preserving the execute permissions of the target ...


25

Virtual machine can give you highest security without reboot, but lowest performance. Another option, for even higher security than a virtual machine: boot a "live" CD/DVD/pendrive without access to the hard drive (temporarily disable the HDD in BIOS; if you can't, at least do not mount the drive / unmount it, if mounted automatically - but this is much ...


25

The issue is that the script is not what is running, but the interpreter (bash, perl, python, etc.). And the interpreter needs to read the script. This is different from a "regular" program, like ls, in that the program is loaded directly into the kernel, as the interpreter would. Since the kernel itself is reading program file, it doesn't need to worry ...


20

Another way is by calling the interpreter and passing the path to the script to it: /bin/sh /path/to/script The dot and source are equivalent. (EDIT: no, they're not: as KeithB points out in a comment on another answer, "." only works in bash related shells, where "source" works in both bash and csh related shells.) It executes the script in-place (as if ...


20

You need the ./ bit to tell the shell where the executable is, since the current directory is unlikely to be in $PATH. You can use which to get the full path to "other commands."


20

When you fail to execute a file that depends on a “loader”, the error you get may refer to the loader rather than the file you're executing. The loader of a dynamically-linked native executable is the part of the system that's responsible for loading dynamic libraries. It's something like /lib/ld.so or /lib/ld-linux.so.2, and should be an executable file. ...


20

In short, because the execute bit is considered special; if it's not set at all, then the file is considered to be not an executable and thus can't be executed. However, if even ONE of the execute bits is set, root can and will execute it. Observe: caleburn: ~/ >cat hello.sh #!/bin/sh echo "Hello!" caleburn: ~/ >chmod 000 hello.sh caleburn: ~/ ...


19

Linux deletes a file completely differently than the way Windows does. First, a brief explanation on how files are managed in the *unix native file systems. The file is kept on the disk in the multilevel structure called i-node. Each i-node has an unique number on the single filesystem. The i-node structure keeps different information about a file, like its ...


18

/usr/, I assume is the user of the computer. Originally, yes, it did refer to the system's users. Before AT&T changed the default location for user folders to /home in System V Release 4 (SVR4), the default was /usr. That is to say, your $HOME might have been /usr/jfw on a System III box. (Other schemes also existed in the past.) /usr also ...


18

If you compile an executable with gcc's -g flag, it contains debugging information. That means for each instruction there is information which line of the source code generated it, the name of the variables in the source code is retained and can be associated to the matching memory at runtime etc. Strip can remove this debugging information and other data ...


18

In general, if a non-system installed and maintained binary needs to be accessible system-wide to multiple users, it should be placed by an administrator into /usr/local/bin. There is a complete hierarchy under /usr/local that is generally used for locally compiled and installed software packages. If you are the only user of a binary, installing into ...


16

There isn't a universal way, but you can make an educated guess by looking for things only done by one compiler. GCC is the easiest; it writes a .comment section that contains the GCC version string (the same string you get if you run gcc --version). I don't know if there's a way to display it with readelf, but with objdump it's: objdump -s --section ...


16

glibc has a configure option called --enable-kernel that lets you specify the minimum supported kernel version. When object files are linked with that glibc build, the linker adds a SHT_NOTE section to the resulting executable named .note.ABI-tag that includes that minimum kernel version. The exact format is defined in the LSB, and file knows to look for ...


16

Usually, this kind of message is due to an extra carriage return at the end of the first line. Run $ head -1 yourscript | od -c and see how it ends: This is wrong 0000000 # ! / b i n / b a s h \r \n This is correct: 0000000 # ! / b i n / b a s h \n Use dos2unix to fix your script if this is the issue.


16

Updating Userland is Rarely a Problem You can often update packages on a live system because: Shared libraries are stored in memory, not read from disk on each call, so the old versions will remain in use until the application is restarted. Open files are actually read from file-descriptors, not the file names, so the file contents remain available to the ...


15

You forget one crucial thing, namely that your program will have to interact with the operating system to do anything interesting. The conventions are different between Linux and OS X so the same binary cannot run as-is without essentially having a chunk of operating system dependent code to be able to interact with it. Many of these things are hidden away ...


15

shc is what you're looking for. get it here: shc Extract, cd into dir, make and then ./shc -f SCRIPT. Done. Everything you need to do this, you find here: SHC Howto


14

To clear just one entry you need a different flag: hash -d svnsync The -r flag doesn't take a parameter and will always delete the entire cache. (At least in bash 3.2.39 on Debian Lenny)


14

Why OSX applications won't run natively on linux: First of all OSX uses a different binary format than Linux, so Linux can't execute binaries compiled for OSX (the same way it can't execute binaries compiled for Windows or BSD). Second of all, if you're talking about GUI applications, Apple's GUI toolkit Cocoa a) is only available for OSX and b) does not ...



Only top voted, non community-wiki answers of a minimum length are eligible