Servers For Hackers Server Administration For Programmers 1 (2018-06-19) Edition Chris Fidao 2024 Scribd Download
Servers For Hackers Server Administration For Programmers 1 (2018-06-19) Edition Chris Fidao 2024 Scribd Download
com
https://textbookfull.com/product/servers-for-
hackers-server-administration-for-
programmers-1-2018-06-19-edition-chris-fidao/
https://textbookfull.com/product/servers-for-hackers-server-
administration-for-programmers-1-2018-06-19-edition-chris-fidao-2/
textbookfull.com
https://textbookfull.com/product/linux-basics-for-hackers-
occupytheweb/
textbookfull.com
https://textbookfull.com/product/ubuntu-and-centos-linux-server-
administration-md-tanvir-rahman/
textbookfull.com
https://textbookfull.com/product/time-series-analysis-1st-edition-
palma/
textbookfull.com
Being John Lennon A Restless Life First Pegasus Books
Hardcover Edition Beatles.
https://textbookfull.com/product/being-john-lennon-a-restless-life-
first-pegasus-books-hardcover-edition-beatles/
textbookfull.com
https://textbookfull.com/product/the-ethics-of-policing-and-
imprisonment-molly-gardner/
textbookfull.com
https://textbookfull.com/product/social-psychology-australian-new-
zealand-edition-saul-kassin/
textbookfull.com
https://textbookfull.com/product/medical-decision-making-3rd-edition-
harold-c-sox/
textbookfull.com
https://textbookfull.com/product/open-living-with-an-expansive-mind-
in-a-distracted-world-1st-edition-nate-klemp/
textbookfull.com
Framing Community Disaster Resilience Maureen Fordham
https://textbookfull.com/product/framing-community-disaster-
resilience-maureen-fordham/
textbookfull.com
Servers for Hackers
Server Administration for Programmers
Chris Fidao
This book is for sale at http://leanpub.com/serversforhackers
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing
process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and
many iterations to get reader feedback, pivot until you have the right book and build traction once
you do.
Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i
Video Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i
Book Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ii
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii
Accidental Sysadmin Syndrome . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv
Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv
Linux Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v
The Sandbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Install Virtualbox and Vagrant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Configure Vagrant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Vagrant Up! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Basic Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Basic Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Users and Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
IP Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Creating a New User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Making Our User a Super User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Fail2Ban . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Iptables Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Package Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Apt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
New (as of 16.04 Xenial, continuing into 18.04 Bionic) . . . . . . . . . . . . . . . . . . . . . . . 46
Installing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Repositories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Searching Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
User Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Creating Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Umask & Group ID Bit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
ACL’s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Running Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Webservers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
HTTP, Web Servers and Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
A Quick Note on DNS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Installing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
CONTENTS
Virtual Hosts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Apache and Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
MPM Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Security Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Envvars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Nginx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Web Server Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Servers (virtual hosts) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Integration with Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
PHP-FPM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
PHP in Nginx: Subdirectories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
LetsEncrypt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Web Server Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Install Letsencrypt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Install a Certificate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Nginx Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Renewal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
CONTENTS
Varnish . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Origin Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Install Varnish . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Basic Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Increasing Cache Hit Rate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Varnish Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Extra Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Logrotate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
What does Logrotate do? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Configuring Logrotate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Going Further . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Rsyslog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Should I Use Rsyslog? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Sending To Rsyslog From An Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
SSH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Logging in . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Supervisord . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
A Chain of Process Monitors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Controlling Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Web Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Forever . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Circus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
CONTENTS
Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Controlling Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Web Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Starting on Boot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Video Site
Since publishing this book, I’ve also collected my newsletter editions, articles and videos to
https://serversforhackers.com1 .
I’ll be continuously adding new videos! These continue to concentrate on topics important to web
servers and web development, from the basic to the complex.
The videos all come with a write-up of the commands and information presented in the video,
usually along with some extra resources. This makes the videos easy to come back to for quick
reference later.
1 https://serversforhackers.com
Servers ii
Book Issues
All feedback is hugely appreciated! Any questions, comments, issues, stories of glory/valor and
praise can be directed to the Github repository2 created for feedback!
https://github.com/Servers-for-Hackers/the-book
2 https://github.com/Servers-for-Hackers/the-book
Introduction
Accidental Sysadmin Syndrome
You’re a developer.
A server broke, and you’re the only one around to fix it.
You have a special-needs application that requires specific software.
You need to setup a development server, and will spend half of your day trying to get some “simple”
configuration to work.
These are symptoms of Accidental Sysadmin Syndrome.
This book is for developers who find themselves needing or wanting to be a SysAdmin.
Assumptions
This book assumes at least a passing familiarity with the command line. Those who have logged
into the shell and poked around a server before will benefit the most.
If you are new to the command line, concentrate on getting comfortable with Vagrant. This
will help familiarize you with using the command line and servers.
Linux Distributions
There are many distributions of Linux. Some popular ones are Arch, Debian, Ubuntu, Redhat,
CentOS, Fedora and CoreOS.
Many of these distributions are related to each other in some way. For example, some of these
distributions are “downstream” from others.
A downstream Linux distribution includes the upstream’s distribution’s changes, and may add their
own.
For example, Ubuntu is based on Debian and is considered downstream of Debian. CentOS is based
on RedHat and is therefore downstream from RedHat. RedHat sponsors Fedora and so Fedora is
very similar to RedHat and CentOS (although it has a much more rapid release cycle).
Each distribution has opinions about Linux and its use. It would be too cumbersome to cover all
topics for each distribution and so I’ve chosen to concentrate on Ubuntu.
Ubuntu is one of the most popular server and desktop distributions. It has a great set of configurations
that come out of the box, allowing us to worry less about configuration until we need to. This makes
it easy to use.
Ubuntu updates software packages quickly relative to some other distributions. However, updating
to the latest software makes it easier to introduce new bugs and version conflicts.
Luckily, Ubuntu’s LTS releases are a good solution to this potential issue.
LTS versions are released every 2 years but support for them last 5 years. This makes them ideal for
longer-term use.
As major versions are released yearly, only every other major release of Ubuntu is an LTS. The
current LTS is 18.04 - the following LTS release will be 20.04.
Xenial, the codename for Ubuntu 16.04, was released in April of 2016. Support officially ends 5 years
later (April of 2021).
3 https://serversforhackers.com
Linux Distributions vi
Bionic, the codename for Ubuntu 18.04, was released in April of 2018. This will be a relevant server
for at least 2 years, but likely beyond that with support officially ending 5 years later (April of 2023).
Ubuntu LTS releases offer more stability and security, but generally do not prevent us from installing
the latest software when we need to. This makes them ideal candidates for every-day server usage.
Popularity is Relative
RedHat Enterprise (RHEL) is a popular distribution in the enterprise world. Many hosting
companies use CentOS along with cPanel/WHM or Plesk control panels. In the open
source/startup worlds, Ubuntu is one of the most popular distributions of Linux.
Because Ubuntu is closely tied to Debian, most, but certainly not all, topics included here will be
exactly the same for Debian.
For RedHat/CentOS distributions, most topics will have small-to-large differences from what you
read here.
In any case, much of what you learn here will be applicable to all distributions.
I recommend this Rackspace knowledge-base article for more information on the various Linux
distributions: https://community.rackspace.com/products/f/25/t/66774 .
4 https://community.rackspace.com/products/f/25/t/6677
The Sandbox
If you want a sandbox - a place to safely play with a server - this chapter is for you.
The topics of the “Sandbox” section is not necessary to follow along in this book, but it will be
helpful.
You’ll learn how to setup a local server on which you can develop an application or experiment with
new technology. As a bonus, you’ll avoid mucking up your computer with development software!
We’ll briefly cover using Vagrant to setup a local server.
The benefit of Vagrant is that it will let us use a “real” server to test on. You can create a server
also used in production. Virtual servers are also safe - we can thoroughly mess them up, throw them
away and recreate them as much as we need.
Let’s get started with Vagrant!
A Virtual Machine is a (guest) computer running inside of your (host) computer. VirtualBox
“virtualizes” hardware by making virtual servers think they are running on real hardware.
A guest computer can be almost anything - Windows, Mac, Linux or other operating
systems.
Here’s some important vocabulary: Your computer is called the “host” machine. Any virtual machine
running within the host machine is called a “guest” machine.
I’ll use the term “virtual machine” with “server” interchangeably, as we’ll be creating Ubuntu
servers (VMs) to learn on.
To get started, the first step is to install Virtualbox and Vagrant. These are available for Windows,
Mac and Linux. Installing them only involves browsing to their websites and downloading/running
their installers. You may need to restart your Windows after installing Vagrant.
For this book, you will need Vagrant version 1.5 or higher. Most versions of Virtualbox
should work, I always update to the latest of these two tools.
The Sandbox 2
Configure Vagrant
Once you have installed these, we can get started! We’ll get Vagrant going by running the following
commands on our host machine.
On Mac, open up the Terminal app. On Windows, you can use the CMD prompt (or your command
line tool of choice) to run Vagrant commands.
On Mac:
1 mkdir -p ~/Sites/sfh
2 cd ~/Sites/sfh
3 vagrant init bento/ubuntu-18.04
On Windows:
1 mkdir C:\sfh
2 cd C:\sfh
3 vagrant init bento/ubuntu-18.04
From here on, I won’t differentiate between Windows and Mac commands. We’ll mostly be
within a server in any case, so the commands will not vary no matter what type of computer
your host is.
The vagrant init command creates a new file called Vagrantfile. This file is configured to use
Ubuntu LTS server.
See the included video for getting started with Vagrant on Virtualbox.
The Vagrantfile created will look something like this (when all the comments are stripped out):
File: Vagrantfile
1 Vagrant.configure(2) do |config|
2
3 config.vm.box = "bento/ubuntu-18.04"
4
5 end
If you look at your file, you’ll see lots of comments, which show some configurations you can use.
I’ll cover a few that you should know about.
The Sandbox 3
Networking
The basic install of Vagrant will do some “port forwarding”. For example, if Vagrant forwards port
8080 to the server’s port 80, then we’ll go to http://localhost:8080 in your browser to reach the server’s
web server at port 80. This has some side effects.
A side effect of this port forwarding has to do with interacting with web applications. You’ll need
to access web pages in your browser using the port which Vagrant sets up, often “8888”. Instead of
“http://localhost”, you’ll use “http://localhost:8888” in the browser. However, your application may
not be coded to be aware of the non-standard port (8888). The application may redirect to, create
links for or submit forms to standard port 80 instead of the forwarded port!
I like to get around this potential problem by assigning a private-network IP address to my Vagrant
server.
To do this, open up your Vagrantfile and make it look like this:
The Sandbox 4
File: Vagrantfile
1 Vagrant.configure(2) do |config|
2
3 config.vm.box = "bento/ubuntu-18.04"
4
5 config.vm.network :private_network, ip: "192.168.22.10"
6
7 end
The private_network directive tells Vagrant to setup a private network. Our host and guest machines
can communicate on this network. This assigns the guest server the IP address of 192.168.22.10.
Note that each server should have a unique IP address just in case they are run at the same time.
There are IP address ranges set aside for private networks. Generally you can use 10.0.0.0
- 10.255.255.255, 172.16.0.0 - 172.31.255.255, and 192.168.0.0 - 192.168.255.255.
However, always avoid the lower and upper IP addresses within those ranges, as they are
often reserved.
Vagrant Up!
Once the Vagrantfile changes are saved, we can run the vagrant up command. This will download
the bento/ubuntu-18.04 base server (“box”) and run it with our set configuration.
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
The Sandbox 5
1 vagrant up
If Vagrant cannot find the Vagrantfile, you need to cd into the directory containing the
Vagrantfile.
You’ll see some output as Vagrant sets up the Ubuntu server. Once it’s complete, run vagrant status
to see that it’s powered on and running.
1 vagrant status
Congratulations, you’re now inside of a real server! Poke around a bit - try some of these commands
out if they are not familiar to you:
• ll - A built-in alias for the command ls -lah, this will list all files and directories within the
current directory, along with their current size in human-readable format.
• lsb_release -a - A command to show all release information about this server.
• top - A command to show running processes and various system information. Use the ctrl+c
keyboard shortcut to return to the prompt.
• clear - A command to clear currently visible output within your terminal. The keyboard
shortcut ctrl+l will do this as well.
• df -h - See how much hard drive space is used/available.
• free -m - See how much memory (RAM) is used/available. Due to various caching mechanisms,
this output can be a tad confusing. To see the actual amount of available memory, take the value
from the free column in the row that says -/+ buffers/cache.
File Sharing
Vagrant sets up file sharing for you. The default shares the server’s /vagrant directory to the host’s
directory containing the Vagrantfile.
In our example, the host machine’s ∼/Sites/sfh directory is shared with the guest’s /vagrant
directory.
The Sandbox 7
The tilde ∼ expands to the current user’s home directory. ∼/Sites/sfh expands to
/Users/fideloper/Sites/sfh on my Macintosh.
From within our server, let’s list the contents of the /vagrant:
1 ls -la /vagrant
We see our Vagrantfile and a hidden .vagrant directory containing some meta data used by
Vagrant.
On my host machine (my Macintosh), I’ll create a new text file in ∼/Sites/sfh named hello.txt:
1 echo "Hello World" > ~/Sites/sfh/hello.txt
Now if I log into the guest server, I’ll see that file is available there as well:
1 # See files in /vagrant
2 cd /vagrant
3 ls -la
4
5 # Output the content of "hello.txt"
6 # with the "cat" command
7 cat /vagrant/hello.txt # Output: "Hello World"
This allows us to edit files from our host machine while running the server software within our
guest server!
A Vagrantfile with the default file sharing configuration in place would look like this:
File: Vagrantfile
1 Vagrant.configure(2) do |config|
2
3 config.vm.box = "bento/ubuntu-18.04"
4
5 config.vm.network :private_network, ip: "192.168.22.10"
6
7 # Share Vagrantfile's directory on the host with /vagrant on the guest
8 config.vm.synced_folder ".", "/vagrant"
9
10 end
Server Network
Let’s check out the network configuration. Within the server, run the command ifconfig:
The Sandbox 8
1 ifconfig
The ifconfig command will one day be replaced by the ip command, but not yet!
The ifconfig command output a lot of content! What are we looking at? Well without getting too
deep into this, we are looking at three networks setup within this server. Each network is called an
“interface”.
• lo - The loopback interface. This is used for internal communication between services within
the server. This is “localhost” - 127.0.0.1
• eth0 and eth1 - These are two additional networks created as well. We can see the IP address
we assigend the server at eth1 - 192.168.22.10. The server also has its own private network,
with the IP address 10.0.2.15 assigned to this machine.
The ifconfig command is a quick way to check the IP address of your server as well as see the
various networks the server is connected to.
The Sandbox 9
You’ll always see a loopback interface. You’ll usually see an internal network, useful for communi-
cating within a local network such as a data center.
Most server providers will assign a server a public IP address. Servers with a public IP address can
usually be reached by users on the internet.
Basic Commands
We’ll be using the command line for 99.9% of this book. On Mac and most Linux desktop
distributions, this means using the Terminal app.
On Windows, this means using the CMD prompt, or any other shell you might install. I’m personally
partial to Git Bash, which is usually installed alongside Git on Windows. You can run the most
common Linux commands with it.
If you’re not logged into your Vagrant server, log back in using vagrant ssh.
Here are some commands you’ll need to know for getting around a server:
pwd - Print working directory. The “working directory” is the directory you are currently in. When
you first log into a server, you’re usually placed in the user’s “home” directory, most often at
/home/username. In our Vagrant server, we’ll be placed in the /home/vagrant directory when we
log in.
ls - List Directory Contents
cd - Change Directory.
FOOTNOTES:
[10] A singular fact connected with this custom of making a short
prayer, whilst slaughtering the victim, I gathered from a note in a
recent edition of “Sale’s Koran.” It appears that by a decision of
those learned in the law, which is laid down in that book, animals
killed by the Jews may be partaken of by Mahomedans. A
representation to the Cadi of Cairo having been made, that nearly
all the butchers of that city were followers of the law of Moses,
they were about to be suspended from that employment, when
their Chief Rabbi proved to the satisfaction of their Moslem
judges, that the Koran bids Mahomedans not to refuse food which
has been sanctified to the one true God, which was always done
by those who professed the faith of Abraham and the law of
Moses, when killing animals for food. This circumstance, and also
the disrespect shown by the Whaabbees to the tomb of the
Prophet, and the temple at Mecca, demonstrate to my satisfaction
that education alone is required to show to the Mahomedans, the
absurdity of the false hopes with which their Prophet has
surrounded the worship of the only one God, and of the
inapplicability of his laws to improve or humanize mankind. I
could point out, if this were a proper place, proofs without end, of
the liberality and extreme toleration of learned and enlightened
Mahomedans, and we ought not to attribute the bigotry of
ignorance, alike fierce and cruel in Christian, Mahomedan, and
Jew, to their religious belief, which on examination will be found
to have been originally very similar amongst all these
denominations, and that the greatest differences appear to be in
the several codes of social laws adopted by each.