2

As I know,the Linux is a open source project,which mean we can find all code that build this system.

But that is too hard for me.

Is there a simple method which can check the source code of any command? Such as the function hostname. I find it will write the information into its configure file(it may be not /etc/sysconfig/network in CentOS 6.3 I think). I hope to find the real configure file. So how to find a source code about a command(such as hostname) by a simple method?

closed as too broad by steve, Stephen Rauch, hildred, Romeo Ninov, Anthon Jul 4 '17 at 7:04

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    duplicate of stackoverflow.com/questions/11528267/… – steve Jul 3 '17 at 22:13
  • 2
    @steve Thanks for your link..That's very useful. – yode Jul 3 '17 at 22:15
  • 1
    welcome. sometimes a google search such as "hostname.c site:github.com" yields useful results for this type of query. – steve Jul 3 '17 at 22:20
  • 1
    On the other hand, in this instance it does not yield useful results. – Thomas Dickey Jul 3 '17 at 22:42
  • 1
    You must ask the packaging system which package provided the command, and how it was built. This might need to be done recursively for dependencies. – Thorbjørn Ravn Andersen Jul 4 '17 at 3:13
4

There's no simple method. But it's doable for files which are part of an RPM:

  • rpm -qf for a given filename identifies the package
  • rpm -qi for a given package tells a little about where the program came from.
  • In particular, if it's a CentOS package, you can find a source-rpm (".srpm") on CentOS's web/ftp-server. That can be unpacked to get the source-tarball and patches.

If it's not in an RPM, it's hard to find the source unless you know how the file got there. For non-CentOS packages, the packager may identify the source location, maybe not.

For instance, your example hostname might be provided by coreutils, but RPM says it is not:

$ rpm -qi `rpm -qf /usr/bin/hostname`
Name        : hostname
Version     : 3.13
Release     : 3.el7
Architecture: x86_64
Install Date: Fri Jul  4 13:23:13 2014
Group       : System Environment/Base
Size        : 19449
License     : GPLv2+
Signature   : RSA/SHA256, Thu Jul  3 21:54:35 2014, Key ID 24c6a8a7f4a80eb5
Source RPM  : hostname-3.13-3.el7.src.rpm
Build Date  : Mon Jun  9 17:48:44 2014
Build Host  : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://packages.qa.debian.org/h/hostname.html
Summary     : Utility to set/show the host name or domain name
Description :
This package provides commands which can be used to display the system's
DNS name, and to display or set its hostname or NIS domain name.

Since the Vendor is CentOS, you can find the source on CentOS's site. Or you could follow the given URL (which can take some work). In this case, Debian offers a link to browse the source code, because Debian maintains the program. In cases where they do not, often a tar-ball is the best you'll find.

Not the answer you're looking for? Browse other questions tagged or ask your own question.