Question: What does the `apt-get install ...` command do?

When I enter `apt-get install ...` command, there are some texts appearing on the screen, but that does not have enough information for me. I want to know if any file is created / edited, any service is started and other activities...

Is there any `.sh` file executed when the `apt-get install ...` run? If so, how can I see the content of that `sh` file?

The reason for this question is recently I tried to install tomcat7 with `apt-get install tomcat7`. Everything works fine until I install `tomcat7-admin` (manager web application), the server became unresponsive to any request. I tried this many times, and this always happen.

Answer: 
Mostly, `apt-get` does the following things:

* checks for dependencies (and asks to install them),
* downloads the package, verifies it and then tells `dpkg` to install it.

`dpkg` will:

* extract the package and copy the content to the right location, and check for pre-existing files and modifications on them,
* run [package maintainer scripts](https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html): `preinst`, `postinst`, (and `prerm`, `postrm` before these, if a package is being upgraded)
* execute some actions based on [triggers](https://wiki.debian.org/DpkgTriggers)

You might be interested in the maintainer scripts, which are usually located at `/var/lib/dpkg/info/<package-name>.{pre,post}{rm,inst}`. These are usually shell scripts, but there's no hard rule. For example:

```
$ ls /var/lib/dpkg/info/xml-core.{pre,post}{rm,inst}
/var/lib/dpkg/info/xml-core.postinst
/var/lib/dpkg/info/xml-core.postrm
/var/lib/dpkg/info/xml-core.preinst
/var/lib/dpkg/info/xml-core.prerm

```
For the actual *under-the-hood* stuff, you'll need to grab the Apt source. Fairly simple if you have source repositories enabled:

```
apt-get source apt

```

The `apt-get` command itself lives in `cmdline/apt-get.cc`. It's a pain to read through but most of `apt-get`'s actions are spelled out quite extensively in there. Installation however, is mapped through a `DoInstall` function which lives in `apt-private/private-install.{cc,h}`.

You have to remember that apt-get is merely one side of the coin.  

`dpkg` is handling the actual installation but `DoInstall` doesn't know about `dpkg` directly. `apt-get` is actually surprisingly package-manager agnostic. All the functionality is abstracted through `apt-pkg/package-manager.cc`

I'm only looking briefly but even there I can't see where this actually attaches to the `dpkg` systems. Some of this seems to be autoconfigured through `apt-pkg/aptconfiguration.cc` but this is a deep well. You could spend days unravelling this.

The source documentation is good though. You could do worse things than to go through each file and read the header to work out what's actually happening.