Configuring Routing Parameters
You may have noticed that we didn’t configure two important IP
parameters in the preceding topics in this chapter: the default
gateway router address and the DNS server address.
Using the IP protocol, routers do just what their name implies: they
route data across multiple networks to deliver information to a
destination host. Routers operate at the Network layer and are used to
connect various networks together.
Routers are usually implemented in conjunction with a gateway. The
router hardware itself may be as simple as a computer system with two
NICs installed, or it may be a specialized hardware appliance
dedicated to routing.
One of the key jobs performed by routers is to determine the best way
to get information to the right destination host. To do this, a router
maintains a routing table of available routes. Routers use an
algorithm that evaluates distance, cost, and network status to
determine the best route to the destination host. Even if it isn’t
configured as a router, every Linux system maintains a routing table
in RAM that it uses to determine where to send data on a network.
When you’re configuring networking parameters on a Linux system, one
of the key tasks you need to perform is to configure the default
router address. The default router is the default location packets
are sent to if they are addressed to a host that doesn’t reside on the
local network segment.
Your default gateway router address is stored in the
/etc/sysconfig/network/routes
file, shown here:
openSUSE:/ # cat /etc/sysconfig/network/routes
default 10.0.0.1 - ens32
The syntax for specifying the default route in this file is
default router_IP_address
Notice in the preceding example that the default gateway router address is set to 10.0.0.1.
TIP If you have more than one NIC installed and each interface needs
its own routing configuration (for example, if each NIC is connected
to a different network), you can define an additional routing
configuration file in
/etc/sysconfig/network/
named
ifroute-interface_name
The syntax for adding a route to the routes file is shown next:
DESTINATION GATEWAY NETMASK INTERFACE [TYPE]
DESTINATION
The first column contains the route’s destination. It may contain the
IP address or DNS hostname of a network or host. Entering default in
this column indicates the route is the default route.
NOTE I recommend you use IP addresses, not DNS names, in this file. If
your DNS server were to go down or become unreachable, routing would
be gone!
GATEWAY
The second column contains the IP address of the router through which
the specified host or a network can be reached. In this column,
specify the IP address of a router that can route the information to
the remote network or host.
NETMASK
The third column contains the netmask for the network or host behind
the router. The fourth column applies the route to a specific
interface. If nothing is specified, the route applies to all
interfaces.
NOTE If you want to leave a column in this file blank, be sure to
enter a dash (-).
[TYPE]
The fifth column is optional. It is used to specify the route type.
You can enter one of the following:
• unicast The route specifies a real path to the destination route.
• local The destination is the localhost. Packets sent to this route
are looped back and delivered to the local machine.
• broadcast The destination is a broadcast address. Packets sent to
this route are sent as link broadcasts.
• multicast Used for multicast routing. This type of route is not
typically used with most routing tables.
• unreachable Configures the route destination as unreachable. Packets
sent to this route are silently dropped.
Here is a sample entry in a routes file:
207.68.156.51 207.68.145.45 255.255.255.0 ens32
After making any changes to the routes file, you will need to restart
your network interface by entering
ifdown interface
followed by
ifup interface
For your LPIC-1/CompTIA Linux+ exam, you also need to be familiar with
how to manage routes with the route command at the shell prompt. You
use the route command to display or modify the routing table on the
Linux host. If you enter
route
without options, it simply displays the current routing table, as shown in this example:
openSUSE:/ # route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 10.0.0.1 0.0.0.0 UG 0 0 0 ens32
10.0.0.0 * 255.255.255.0 U 0 0 0 ens32
loopback * 255.0.0.0 U 0 0 0 lo
You can add routes to the host’s route table by entering
route add –net network_address netmask netmask gw router_address
For example, suppose you need to add a route to the 192.168.2.0/24 network through
the router with an IP address of 10.0.0.254. You could do this by
entering
route add –net 192.168.2.0 netmask 255.255.255.0 gw 10.0.0.254
at the shell prompt.
You can also remove existing routes from the routing table on a Linux
host using the route command. This is done by entering
route del –net network_address netmask netmask gw router_address
at the shell prompt.
For example, suppose you want to remove the route just added in the
preceding paragraph. You could do this by entering
route del –net 192.168.2.0 netmask 255.255.255.0 gw 10.0.0.254
at the shell prompt.
You can also use route to set the default route. This is done by
entering
route add default gw router_address
at the shell prompt. For example, if you want to add 10.0.0.254 as your default gateway router,
you would enter
route add default gw 10.0.0.254
at the shell prompt.
TIP Changes made with the route command are not persistent! If you
want the route changes to be persistent across reboots, you need to
add them to your
/etc/sysconfig/network/routes
file.
You can also use the ip command at the shell prompt to manage routing.
For example, to view the routing table, you can enter ip route show at
the shell prompt, as shown here:
openSUSE:/ # ip route show
default via 10.0.0.1 dev ens32
10.0.0.0/24 dev ens32 proto kernel scope link src 10.0.0.83
127.0.0.0/8 dev lo scope link
You can also use the ip command to add a static route to the routing
table. This is done by entering
ip route add network/prefix via router_ip_address dev interface
at the shell prompt. In the following example shown, I’ve added a route to the
192.168.5.0/24 network through a router with an IP address of 10.0.0.254:
openSUSE:/ # ip route add 192.168.5.0/24 via 10.0.0.254 dev ens32
openSUSE:/ # ip route show
default via 10.0.0.1 dev ens32
10.0.0.0/24 dev ens32 proto kernel scope link src 10.0.0.83
127.0.0.0/8 dev lo scope link
192.168.5.0/24 via 10.0.0.254 dev ens32
You can also remove a route from the routing table by entering
ip route del network/prefix
at the shell prompt.
For example, to remove the 192.168.5.0/24 route I just added, I would enter
ip route del 192.168.5.0/24
LX0-104 Exam Objectives (S)
Showing posts with label commands. Show all posts
Showing posts with label commands. Show all posts
Monday, December 19, 2016
Tuesday, December 13, 2016
Printing VIII
Using the Line Printer Daemon (lpd)
By far, CUPS is the preferred printing system for modern Linux
distributions. Many years ago, however, the preferred printing system
was the Line Printer Daemon (lpd). You probably won’t work much with
lpd, but the LPIC-1/Linux+ exam still expects you to know some of the
commands used to manage this daemon.
Most of the lpd commands have functionality similar to that offered by a CUPS command, as shown in Table 16-2.
As an interesting side note,
lpc status
at the shell prompt and it will return the status of your CUPS
printers, if CUPS is installed instead of lpd.
LX0-104 Exam Objectives (Q)
By far, CUPS is the preferred printing system for modern Linux
distributions. Many years ago, however, the preferred printing system
was the Line Printer Daemon (lpd). You probably won’t work much with
lpd, but the LPIC-1/Linux+ exam still expects you to know some of the
commands used to manage this daemon.
Most of the lpd commands have functionality similar to that offered by a CUPS command, as shown in Table 16-2.
As an interesting side note,
lpc status
at the shell prompt and it will return the status of your CUPS
printers, if CUPS is installed instead of lpd.
LX0-104 Exam Objectives (Q)
Printing VII
Using Command-Line Tools to Manage CUPS
In addition to the CUPS web-based administration utility, you can also
use a variety of command- line tools to configure CUPS. To view CUPS
printer information, you can use the lpstat utility. One of the most
useful options you can use with
lpstat is –t
This will cause lpstat to display all information about all CUPS
printers on the system, as this next example shows:
openSUSE:~ # lpstat –t
scheduler running
no system default destination
device for HPLJ2: parallel:/dev/lp0
HPLJ2 accepting requests since Fri 13 May 2011 10:57:13 AM MDT
printer HPLJ2 is idle. enabled since Fri 13 May 2011 10:57:13 AM MDT
Printer is now online.
This shows the default CUPS printer (HPLJ2), how it’s connected
(/dev/lp0), the print job currently being processed (if any), and a
list of pending print jobs.
To cancel a pending print job, you can use the cancel command. The syntax is
cancel job_ID
For example, suppose I sent a huge print job (a Linux user manual from
/usr/share/doc/manual/) and it was assigned a print ID of HPLJ2-4.
While printing, I decided that this was a real waste of paper. I could
kill the job and remove it from the print queue by entering
cancel HPLJ2-4
at the shell prompt.
This can also be done from within the CUPS web-based administration
utility. Just go to the Jobs tab and select Show Active Jobs. Locate
the job that needs to be canceled and select Cancel Job, as shown in
Figure 16-10.
If you have more than one CUPS printer connected, you can use the
lpoptions –d printer
command to specify the default printer. For example, to set the HPLJ5
printer as the default, I would enter
lpoptions –d HPLJ5
This sets the default printer for all users on the system. Indi-
vidual users can override this setting, however, by creating a file
named
.lpoptions
in their home directory and adding the following directive:
default printer_name
If you want to view your printer’s configuration settings, you can enter
lpoptions –l
at the shell prompt.
In addition to the lpoptions command, you can also use the
cupsaccept printer_name
or
cupsreject printer_name
command to enable or disable a printer’s print queue. For example, I could enter
cupsreject HPLJ2
at the shell prompt to disable the printer’s print queue, as shown in
this example:
openSUSE:~ # cupsreject HPLJ2
openSUSE:~ # lpstat is –t
scheduler is running
system default destination: HPLJ2
device for HPLJ2: parallel:/dev/lp0
HPLJ2 not accepting requests since Fri 13 May 2011 11:03:07 AM MDT -
Rejecting Jobs
printer HPLJ2 is idle. enabled since Fri 13 May 2011 11:03:07 AM MDT
Rejecting Jobs
The printer itself will continue processing queued print jobs, but
cupsd will not allow any new jobs to enter the queue.
The cupsdisable command also includes the --hold option, which stops printing after
the current job is complete. To enable the queue again, I would enter
cupsaccept HPLJ2
at the shell prompt.
To disable the printer itself, not the queue, I could enter
cupsdisable HPLJ2
at the shell prompt, as this example shows:
openSUSE:~ # cupsdisable HPLJ2
openSUSE:~ # lpstat is –t
scheduler is running
system default destination: HPLJ2
device for HPLJ2: parallel:/dev/lp0
HPLJ2 accepting requests since Fri 13 May 2011 11:15:28 AM MDT
printer HPLJ2 disabled since Fri 13 May 2011 11:15:28 AM MDT -
Paused
The print queue will continue to accept jobs, but none of them will be
printed until I enter
cupsenable HPLJ2
at the shell prompt.
The cupsenable command also includes the --release option to release pending jobs for printing.
LX0-104 Exam Objectives (Q)
In addition to the CUPS web-based administration utility, you can also
use a variety of command- line tools to configure CUPS. To view CUPS
printer information, you can use the lpstat utility. One of the most
useful options you can use with
lpstat is –t
This will cause lpstat to display all information about all CUPS
printers on the system, as this next example shows:
openSUSE:~ # lpstat –t
scheduler running
no system default destination
device for HPLJ2: parallel:/dev/lp0
HPLJ2 accepting requests since Fri 13 May 2011 10:57:13 AM MDT
printer HPLJ2 is idle. enabled since Fri 13 May 2011 10:57:13 AM MDT
Printer is now online.
This shows the default CUPS printer (HPLJ2), how it’s connected
(/dev/lp0), the print job currently being processed (if any), and a
list of pending print jobs.
To cancel a pending print job, you can use the cancel command. The syntax is
cancel job_ID
For example, suppose I sent a huge print job (a Linux user manual from
/usr/share/doc/manual/) and it was assigned a print ID of HPLJ2-4.
While printing, I decided that this was a real waste of paper. I could
kill the job and remove it from the print queue by entering
cancel HPLJ2-4
at the shell prompt.
This can also be done from within the CUPS web-based administration
utility. Just go to the Jobs tab and select Show Active Jobs. Locate
the job that needs to be canceled and select Cancel Job, as shown in
Figure 16-10.
If you have more than one CUPS printer connected, you can use the
lpoptions –d printer
command to specify the default printer. For example, to set the HPLJ5
printer as the default, I would enter
lpoptions –d HPLJ5
This sets the default printer for all users on the system. Indi-
vidual users can override this setting, however, by creating a file
named
.lpoptions
in their home directory and adding the following directive:
default printer_name
If you want to view your printer’s configuration settings, you can enter
lpoptions –l
at the shell prompt.
In addition to the lpoptions command, you can also use the
cupsaccept printer_name
or
cupsreject printer_name
command to enable or disable a printer’s print queue. For example, I could enter
cupsreject HPLJ2
at the shell prompt to disable the printer’s print queue, as shown in
this example:
openSUSE:~ # cupsreject HPLJ2
openSUSE:~ # lpstat is –t
scheduler is running
system default destination: HPLJ2
device for HPLJ2: parallel:/dev/lp0
HPLJ2 not accepting requests since Fri 13 May 2011 11:03:07 AM MDT -
Rejecting Jobs
printer HPLJ2 is idle. enabled since Fri 13 May 2011 11:03:07 AM MDT
Rejecting Jobs
The printer itself will continue processing queued print jobs, but
cupsd will not allow any new jobs to enter the queue.
The cupsdisable command also includes the --hold option, which stops printing after
the current job is complete. To enable the queue again, I would enter
cupsaccept HPLJ2
at the shell prompt.
To disable the printer itself, not the queue, I could enter
cupsdisable HPLJ2
at the shell prompt, as this example shows:
openSUSE:~ # cupsdisable HPLJ2
openSUSE:~ # lpstat is –t
scheduler is running
system default destination: HPLJ2
device for HPLJ2: parallel:/dev/lp0
HPLJ2 accepting requests since Fri 13 May 2011 11:15:28 AM MDT
printer HPLJ2 disabled since Fri 13 May 2011 11:15:28 AM MDT -
Paused
The print queue will continue to accept jobs, but none of them will be
printed until I enter
cupsenable HPLJ2
at the shell prompt.
The cupsenable command also includes the --release option to release pending jobs for printing.
LX0-104 Exam Objectives (Q)
Printing VI
Configuring a CUPS Printer
All CUPS printers are defined in the
/etc/cups/printers.conf
file.
Although you can manually edit this file, you really should use the
CUPS web-based administration utility instead. Configuring a CUPS
printer is a snap with it. You can either configure CUPS to service a
locally attached printer (and optionally make it available to other
network users) or connect to a CUPS printer over the network. For
example, to configure CUPS to use a locally attached printer, do the
following:
1) On your Linux system, start a web browser and navigate to
http://localhost:631.
2) Select Administration. The screen in Figure 16-5 is displayed.
3) Under Printers, select Add Printer.
4) When prompted, log in as the administrative user you created
previously. The screen in Figure 16-6 is displayed.
5) Select a locally attached printer type under Local Printers and
then select Continue. A screen similar to that shown in Figure 16-7 is
displayed.
TIP You could also select a network printer in this screen. All
broadcasting CUPS printers on other network hosts are listed under
Discovered Network Printers. To send print jobs to one of these
printers, just select it.
6) In the Name field, enter a name for the printer.
7) In the Description field, enter a description of the printer.
8) In the Location field, enter a location for the printer.
9) If you want to share the printer with other network users, mark
Share This Printer.
10) Select Continue. The screen in Figure 16-8 is displayed.
11) Select the printer manufacturer; then select Continue.
12) In the Model field, select your printer model; then select Add Printer.
13) Configure your default options for the printer, such as paper
size, color model, media source, print quality, two-sided printing,
and so on. When complete, select Set Default Options.
At this point, a page is displayed indicating your printer has been
added. The current status of your printer is displayed, similar to
that shown in Figure 16-9.
From the Printer Status page, you can manage your CUPS printer. You
can send a test page, stop the printer, kill a print job, modify the
printer configuration, or delete the printer altogether. At this
point, you can send print jobs to the printer. If you’re using a
graphical X application, you can simply select File | Print; then
select the printer and click OK. You can also send print jobs from the
command line to the printer. This is done using the lp command, which
will send a specified file to the printer. The syntax for using lp is
lp –d printer_name filename
For example, if I wanted to print the myfiles file in the current
directory to the HPLJ2 printer I just created, I
would enter
lp –d HPLJ5 ./myfiles
at the shell prompt, as shown here: openSUSE:~ # lp -d HPLJ2 ./myfiles
request id is HPLJ2-2 (1 file(s))
As you can see in this example, the
job is created and assigned an ID (in this case, HPLJ2-2). The job is
added to the print queue and sent to the printer. The lp utility
includes a variety of options besides –d that you can use to create
print jobs, including the following:
• –n x Prints x number of copies
• –m E-mails a confirmation message to my local user account when the
job is finished printing
• –q x Sets the priority of the print job to x
• –o landscape Prints the file landscape instead of portrait
• –o sides=2 Prints the file double-sided on a printer that supports duplexing
You can also configure other Linux systems to print to the CUPS
printer. Simply configure a new printer, but specify that it listen
for CUPS announcements. The CUPS printer you configured should be
displayed within 30 seconds. After you select it, all print jobs sent
to that printer will be redirected over the network connection to your
CUPS printer.
In addition, if you’ve installed Samba on your system, your CUPS
printers are automatically shared. You can connect to them from
Windows workstations and submit print jobs. Now that’s cool!
LX0-104 Exam Objectives (Q)
All CUPS printers are defined in the
/etc/cups/printers.conf
file.
Although you can manually edit this file, you really should use the
CUPS web-based administration utility instead. Configuring a CUPS
printer is a snap with it. You can either configure CUPS to service a
locally attached printer (and optionally make it available to other
network users) or connect to a CUPS printer over the network. For
example, to configure CUPS to use a locally attached printer, do the
following:
1) On your Linux system, start a web browser and navigate to
http://localhost:631.
2) Select Administration. The screen in Figure 16-5 is displayed.
3) Under Printers, select Add Printer.
4) When prompted, log in as the administrative user you created
previously. The screen in Figure 16-6 is displayed.
5) Select a locally attached printer type under Local Printers and
then select Continue. A screen similar to that shown in Figure 16-7 is
displayed.
TIP You could also select a network printer in this screen. All
broadcasting CUPS printers on other network hosts are listed under
Discovered Network Printers. To send print jobs to one of these
printers, just select it.
6) In the Name field, enter a name for the printer.
7) In the Description field, enter a description of the printer.
8) In the Location field, enter a location for the printer.
9) If you want to share the printer with other network users, mark
Share This Printer.
10) Select Continue. The screen in Figure 16-8 is displayed.
11) Select the printer manufacturer; then select Continue.
12) In the Model field, select your printer model; then select Add Printer.
13) Configure your default options for the printer, such as paper
size, color model, media source, print quality, two-sided printing,
and so on. When complete, select Set Default Options.
At this point, a page is displayed indicating your printer has been
added. The current status of your printer is displayed, similar to
that shown in Figure 16-9.
From the Printer Status page, you can manage your CUPS printer. You
can send a test page, stop the printer, kill a print job, modify the
printer configuration, or delete the printer altogether. At this
point, you can send print jobs to the printer. If you’re using a
graphical X application, you can simply select File | Print; then
select the printer and click OK. You can also send print jobs from the
command line to the printer. This is done using the lp command, which
will send a specified file to the printer. The syntax for using lp is
lp –d printer_name filename
For example, if I wanted to print the myfiles file in the current
directory to the HPLJ2 printer I just created, I
would enter
lp –d HPLJ5 ./myfiles
at the shell prompt, as shown here: openSUSE:~ # lp -d HPLJ2 ./myfiles
request id is HPLJ2-2 (1 file(s))
As you can see in this example, the
job is created and assigned an ID (in this case, HPLJ2-2). The job is
added to the print queue and sent to the printer. The lp utility
includes a variety of options besides –d that you can use to create
print jobs, including the following:
• –n x Prints x number of copies
• –m E-mails a confirmation message to my local user account when the
job is finished printing
• –q x Sets the priority of the print job to x
• –o landscape Prints the file landscape instead of portrait
• –o sides=2 Prints the file double-sided on a printer that supports duplexing
You can also configure other Linux systems to print to the CUPS
printer. Simply configure a new printer, but specify that it listen
for CUPS announcements. The CUPS printer you configured should be
displayed within 30 seconds. After you select it, all print jobs sent
to that printer will be redirected over the network connection to your
CUPS printer.
In addition, if you’ve installed Samba on your system, your CUPS
printers are automatically shared. You can connect to them from
Windows workstations and submit print jobs. Now that’s cool!
LX0-104 Exam Objectives (Q)
Printing V
Configuring the CUPS Service
The CUPS service is configured using several text files within the
/etc/cups directory.
The
/etc/cups/cupsd.conf
file is the main configuration file you will use to configure the cupsd
daemon (calledt he scheduler). Remember that cupsd is also an HTTP server,
like Apache. Accordingly, the cupsd.conf file is very similar to the Apache
web server configuration file.
A sample cupsd .conf file is shown in Figure 16-4.
Figure 16-4 only shows a very small portion of the cupsd.conf file,
which is quite long. The cupsd.conf file is composed of many server
directives, which specify how cupsd operates. We don’t have the time
or space in this book to cover all the configuration options in
cupsd.conf. I’m just going to cover the most important ones here. For
more information, see the man page for cupsd.conf. You can also open
http://localhost:631/help/
in a browser on your Linux system to see an extensive list of cupsd.conf directives,
or you can visit
http://www.cups.org/documentation.php/ref-cupsd-conf.html
Some of the more useful cupsd.conf directives include those shown in Table
16-1.
The way you configure cupsd.conf will largely depend on the particular
network you are implementing the system in. The good news is that you
don’t need to do much with cupsd.conf to configure a basic
implementation that provides local printing.
However, if you want other Linux systems to be able to print through
your CUPS printer, you must
enable BrowseAddress
or else CUPS won’t announce its printers on the network. This directive is
not enabled by default on many distributions. Sample configurations for this
directive include the following:
BrowseAddress 255.255.255.255:631
BrowseAddress 192.168.1.255:631
BrowseAddress mydom.com:631
BrowseAddress @LOCAL
The BrowseAddress directive is usually set to a value of @LOCAL.
This causes CUPS to send printer announcement broadcasts to all local
network interfaces in the system.
You can set this directive to
@IF(interface_name)
to limit broadcasts to a specific network interface.
After making any changes to cupds.conf, be sure to restart the cupsd
daemon.
After configuring your cupsd.conf file, you next need to set
up a Linux user account that will be used as the CUPS administrator.
CUPS does not use the same user accounts that your Linux system uses.
Instead, CUPS is configured to use the
/etc/cups/passwd.md5
file to store user accounts.
To create an administrative user in the passwd.md5 file named root that
is a member of the CUPS administration group named sys, you would enter
lppasswd –g sys –a root
LX0-104 Exam Objectives (Q)
The CUPS service is configured using several text files within the
/etc/cups directory.
The
/etc/cups/cupsd.conf
file is the main configuration file you will use to configure the cupsd
daemon (calledt he scheduler). Remember that cupsd is also an HTTP server,
like Apache. Accordingly, the cupsd.conf file is very similar to the Apache
web server configuration file.
A sample cupsd .conf file is shown in Figure 16-4.
Figure 16-4 only shows a very small portion of the cupsd.conf file,
which is quite long. The cupsd.conf file is composed of many server
directives, which specify how cupsd operates. We don’t have the time
or space in this book to cover all the configuration options in
cupsd.conf. I’m just going to cover the most important ones here. For
more information, see the man page for cupsd.conf. You can also open
http://localhost:631/help/
in a browser on your Linux system to see an extensive list of cupsd.conf directives,
or you can visit
http://www.cups.org/documentation.php/ref-cupsd-conf.html
Some of the more useful cupsd.conf directives include those shown in Table
16-1.
The way you configure cupsd.conf will largely depend on the particular
network you are implementing the system in. The good news is that you
don’t need to do much with cupsd.conf to configure a basic
implementation that provides local printing.
However, if you want other Linux systems to be able to print through
your CUPS printer, you must
enable BrowseAddress
or else CUPS won’t announce its printers on the network. This directive is
not enabled by default on many distributions. Sample configurations for this
directive include the following:
BrowseAddress 255.255.255.255:631
BrowseAddress 192.168.1.255:631
BrowseAddress mydom.com:631
BrowseAddress @LOCAL
The BrowseAddress directive is usually set to a value of @LOCAL.
This causes CUPS to send printer announcement broadcasts to all local
network interfaces in the system.
You can set this directive to
@IF(interface_name)
to limit broadcasts to a specific network interface.
After making any changes to cupds.conf, be sure to restart the cupsd
daemon.
After configuring your cupsd.conf file, you next need to set
up a Linux user account that will be used as the CUPS administrator.
CUPS does not use the same user accounts that your Linux system uses.
Instead, CUPS is configured to use the
/etc/cups/passwd.md5
file to store user accounts.
To create an administrative user in the passwd.md5 file named root that
is a member of the CUPS administration group named sys, you would enter
lppasswd –g sys –a root
LX0-104 Exam Objectives (Q)
Printing IV
Configuring CUPS
The CUPS service appears complicated, and under the hood, it is.
Fortunately, the developers who wrote CUPS made it very easy for you
and me to configure and manage. In this part of this chapter, you’re
going learn how to configure CUPS by learning about the following
topics:
• Configuring the CUPS service
• Configuring a CUPS printer
• Using command-line tools to manage CUPS
Let’s begin by discussing how to configure the cupsd daemon.
LX0-104 Exam Objectives (Q)
The CUPS service appears complicated, and under the hood, it is.
Fortunately, the developers who wrote CUPS made it very easy for you
and me to configure and manage. In this part of this chapter, you’re
going learn how to configure CUPS by learning about the following
topics:
• Configuring the CUPS service
• Configuring a CUPS printer
• Using command-line tools to manage CUPS
Let’s begin by discussing how to configure the cupsd daemon.
LX0-104 Exam Objectives (Q)
Friday, December 9, 2016
MTA Management Commands: mail III
Using MTA Management Commands on Linux
Let’s first look at reading messages stored in your local MTA. When
you log in to a shell session, you will receive a notification if
there are mail messages waiting for you. You can read messages for
local users from the local MTA directly from the command line using
the mail command at the shell prompt. When you do, a list of messages
is displayed. An example is shown in Figure 16-15.
Some services running on Linux are configured to send notification
messages to the root user.
These messages are stored in your user’s mail queue, which is located
in the
/var/spool/mail/
directory. The mail utility reads yourmessages directly out of your user’s queue file.
Because you’rerunning the mail utility on the same system where your queue resides,
you don’t need POP3 or IMAP support configured. You can enter the mailcommands shown in
Table 16-4 at the ? prompt.
An example of viewing a received message with the t command is shown
in Figure 16-16.
To send a message, you can also enter
mail recipient_address
at the shell prompt. You can then enter a subject line and the text of
your message. Press
ctrl-d
when you’re done to actually send the message. When you do, the message is delivered to the other user’s mail queue by your local MTA.
To view a list of unread messages in your mail queue, you can enter
mailq
at the shell prompt.
In addition to mail, many other packages are available that you can
install to read mail from the shell prompt. The key thing to remember
is that the user must run the mail command from the local shell
prompt. If the user isn’t using the local computer system, they must
ssh into the system to read mail.
You can also configure aliases for the MTA running on your Linux
system. Mail aliases redi- rect mail addressed to one user to another
user’s account. You use the
/etc/aliases
file to configure aliases.
This file defines one alias per line. The alias you define must point
to an existing e-mail address. The syntax for this file follows:
alias: list of real e-mail addresses (separated by commas)
For example, the following two aliases must be present in this file on
most Linux distributions:
postmaster: root
mailer-daemon: postmaster
These aliases cause any e-mail messages sent to the postmaster to be
automatically redirected to the root user. Likewise, any e-mail
messages sent to mailer-daemon will be redirected to post- master
(which will then be redirected to root). Depending on your
distribution, you will probably find that many aliases are defined for
you by default.
Here is an example:
# General redirections for pseudo accounts in /etc/passwd.
administrator: root
daemon: root
lp: root
news: root
uucp: root
games: root
man: root
at: root
postgres: root
mdom: root
amanda: root
ftp: root
wwwrun: root
squid: root
msql: root
gnats: root
nobody: root
# "bin" used to be in /etc/passwd
bin: root
Of course, you can enter your own custom aliases if needed. Just open
the aliases file in a text editor and add the appropriate aliases, one
per line. When done configuring aliases, you must run the
newaliases
command at the shell prompt as root to enable them.
You can also use the
~/.forward
file in your user’s home directory to configure forwarding. Most Linux MTAs
check for the existence of this file in the user’s home directory to configure
forwarding of messages.
You can open/create the file in a text editor and enter the e-mail
address to which you want to forward e-mail. If you are forwarding to
a local user, just enter the username. If you’re forwarding to a
remote user account, enter username@domain.com. If you need to forward
messages to multiple recipients, separate them with a comma.
The MTA will treat the addresses you enter in this file as an alias.
This causes all e-mail to be forwarded to the forwarding e-mail
address. Messages will not be delivered to the original user’s
mailbox.
LX0-104 Exam Objectives (P)
Let’s first look at reading messages stored in your local MTA. When
you log in to a shell session, you will receive a notification if
there are mail messages waiting for you. You can read messages for
local users from the local MTA directly from the command line using
the mail command at the shell prompt. When you do, a list of messages
is displayed. An example is shown in Figure 16-15.
Some services running on Linux are configured to send notification
messages to the root user.
These messages are stored in your user’s mail queue, which is located
in the
/var/spool/mail/
directory. The mail utility reads yourmessages directly out of your user’s queue file.
Because you’rerunning the mail utility on the same system where your queue resides,
you don’t need POP3 or IMAP support configured. You can enter the mailcommands shown in
Table 16-4 at the ? prompt.
An example of viewing a received message with the t command is shown
in Figure 16-16.
To send a message, you can also enter
mail recipient_address
at the shell prompt. You can then enter a subject line and the text of
your message. Press
ctrl-d
when you’re done to actually send the message. When you do, the message is delivered to the other user’s mail queue by your local MTA.
To view a list of unread messages in your mail queue, you can enter
mailq
at the shell prompt.
In addition to mail, many other packages are available that you can
install to read mail from the shell prompt. The key thing to remember
is that the user must run the mail command from the local shell
prompt. If the user isn’t using the local computer system, they must
ssh into the system to read mail.
You can also configure aliases for the MTA running on your Linux
system. Mail aliases redi- rect mail addressed to one user to another
user’s account. You use the
/etc/aliases
file to configure aliases.
This file defines one alias per line. The alias you define must point
to an existing e-mail address. The syntax for this file follows:
alias: list of real e-mail addresses (separated by commas)
For example, the following two aliases must be present in this file on
most Linux distributions:
postmaster: root
mailer-daemon: postmaster
These aliases cause any e-mail messages sent to the postmaster to be
automatically redirected to the root user. Likewise, any e-mail
messages sent to mailer-daemon will be redirected to post- master
(which will then be redirected to root). Depending on your
distribution, you will probably find that many aliases are defined for
you by default.
Here is an example:
# General redirections for pseudo accounts in /etc/passwd.
administrator: root
daemon: root
lp: root
news: root
uucp: root
games: root
man: root
at: root
postgres: root
mdom: root
amanda: root
ftp: root
wwwrun: root
squid: root
msql: root
gnats: root
nobody: root
# "bin" used to be in /etc/passwd
bin: root
Of course, you can enter your own custom aliases if needed. Just open
the aliases file in a text editor and add the appropriate aliases, one
per line. When done configuring aliases, you must run the
newaliases
command at the shell prompt as root to enable them.
You can also use the
~/.forward
file in your user’s home directory to configure forwarding. Most Linux MTAs
check for the existence of this file in the user’s home directory to configure
forwarding of messages.
You can open/create the file in a text editor and enter the e-mail
address to which you want to forward e-mail. If you are forwarding to
a local user, just enter the username. If you’re forwarding to a
remote user account, enter username@domain.com. If you need to forward
messages to multiple recipients, separate them with a comma.
The MTA will treat the addresses you enter in this file as an alias.
This causes all e-mail to be forwarded to the forwarding e-mail
address. Messages will not be delivered to the original user’s
mailbox.
LX0-104 Exam Objectives (P)
Thursday, December 8, 2016
Tunnel Your X server traffic
Tunnel Your X server traffic
You can also tunnel your X server traffic to remote X clients using an
SSH connection. This is important because unencrypted X traffic
provides an attacker with a gold mine of information that he or she
can use to compromise your systems.
To configure a remote X client without encryption, you can use the
following procedure:
1) On the remote
X client, enter xhost +X_server_hostname.
This tells the client to accept connections from the X server.
2) On the X server, enter
DISPLAY=X_client_hostname:0.0
and then enter
export DISPLAY
This tells the X server to display its output on the remote X client.
3) From the X client, use the ssh client to access the shell prompt on
the X server and then run the graphical application you want displayed
on the X client. For example, you could enter gedit at the shell
prompt to remotely display the gedit text editor. You could also enter
office at the shell prompt to remotely display the OpenOffice.org suite.
This procedure works, but all the X traffic is transmitted unencrypted. This isn’t good.
Instead, you should use SSH to tunnel the X server traffic between the X server and the X client.
You can do this using one of the following options:
• Use the –X option with the ssh client program.
• Set the ForwardX11 option to a value of yes in the
/etc/ssh/ssh_config file on the X client system.
Once this is done, you then need to set the X11Forwarding option to
yes in the /etc/ssh/ sshd_config file on the X server system.
LX0-104 Exam Objectives (H)
You can also tunnel your X server traffic to remote X clients using an
SSH connection. This is important because unencrypted X traffic
provides an attacker with a gold mine of information that he or she
can use to compromise your systems.
To configure a remote X client without encryption, you can use the
following procedure:
1) On the remote
X client, enter xhost +X_server_hostname.
This tells the client to accept connections from the X server.
2) On the X server, enter
DISPLAY=X_client_hostname:0.0
and then enter
export DISPLAY
This tells the X server to display its output on the remote X client.
3) From the X client, use the ssh client to access the shell prompt on
the X server and then run the graphical application you want displayed
on the X client. For example, you could enter gedit at the shell
prompt to remotely display the gedit text editor. You could also enter
office at the shell prompt to remotely display the OpenOffice.org suite.
This procedure works, but all the X traffic is transmitted unencrypted. This isn’t good.
Instead, you should use SSH to tunnel the X server traffic between the X server and the X client.
You can do this using one of the following options:
• Use the –X option with the ssh client program.
• Set the ForwardX11 option to a value of yes in the
/etc/ssh/ssh_config file on the X client system.
Once this is done, you then need to set the X11Forwarding option to
yes in the /etc/ssh/ sshd_config file on the X server system.
LX0-104 Exam Objectives (H)
Manipulating SQL Data
Manipulating SQL Data
After installing your MySQL packages, you next need to set up your
MySQL server’s granttables. All MySQL access controls are managed from within the MySQL
service itself. It’s important to understand that MySQL has its own unique set of user accounts
defined in its grant tables; it doesn’t use the accounts defined on your Linux system. Five tables
are implemented within the MySQL database to do this:
user
Specifies whether a user is allowed to connect to the MySQL server
db
Defines which databases a user is allowed to access
host
Specifies which hosts are allowed to access a particular database
tables_priv
Defines access privileges for a given table
columns_priv
Specifies access privileges for specific columns of data for a given table
These tables must be initialized before you can use MySQL. This is
done by changing to the
/usr/bin directory
and running the
mysql_install_db
command at the shell prompt.
With the grant tables created, you next need to start the database
service.
If your distribution uses init, you can use the mysql init script located in your init
script directory. Then you can use the insserv or chkconfig
command to ensure the database service starts
every time the system
boots. If your distribution uses systemd, you can use the systemctl
command to enable and start the mysql service.
To verify that the server is running, you can enter the
mysqladmin version
command at the shell prompt.
you can view the databases currently on the server by entering
mysqlshow
at the command prompt.
You can also view the tables within any of the databases shown in the
output using the
mysqlshow table_name
command.
Now that MySQL is running properly, you need to assign passwords to
your MySQL user accounts.
After you run msql_install_db, your root database user account has been created but has no password assigned. To remedy this, enter
mysqladmin –u root password ‘your_new_password'
at the shell prompt. Then restrict root access to the system where MySQL is running
by entering
mysqladmin –u root –h system_hostname password mysql_root_user_password
at the shell prompt.
LX0-104 Exam Objectives (G)
After installing your MySQL packages, you next need to set up your
MySQL server’s granttables. All MySQL access controls are managed from within the MySQL
service itself. It’s important to understand that MySQL has its own unique set of user accounts
defined in its grant tables; it doesn’t use the accounts defined on your Linux system. Five tables
are implemented within the MySQL database to do this:
user
Specifies whether a user is allowed to connect to the MySQL server
db
Defines which databases a user is allowed to access
host
Specifies which hosts are allowed to access a particular database
tables_priv
Defines access privileges for a given table
columns_priv
Specifies access privileges for specific columns of data for a given table
These tables must be initialized before you can use MySQL. This is
done by changing to the
/usr/bin directory
and running the
mysql_install_db
command at the shell prompt.
With the grant tables created, you next need to start the database
service.
If your distribution uses init, you can use the mysql init script located in your init
script directory. Then you can use the insserv or chkconfig
command to ensure the database service starts
every time the system
boots. If your distribution uses systemd, you can use the systemctl
command to enable and start the mysql service.
To verify that the server is running, you can enter the
mysqladmin version
command at the shell prompt.
you can view the databases currently on the server by entering
mysqlshow
at the command prompt.
You can also view the tables within any of the databases shown in the
output using the
mysqlshow table_name
command.
Now that MySQL is running properly, you need to assign passwords to
your MySQL user accounts.
After you run msql_install_db, your root database user account has been created but has no password assigned. To remedy this, enter
mysqladmin –u root password ‘your_new_password'
at the shell prompt. Then restrict root access to the system where MySQL is running
by entering
mysqladmin –u root –h system_hostname password mysql_root_user_password
at the shell prompt.
LX0-104 Exam Objectives (G)
Commands to manage data in an SQL database
Managing SQL Data
You can use the following commands to manage data in an SQL database:
• SELECT
Retrieves information from a table
• UPDATE
Modifies information in a table
• DELETE
Removes information from a table
• INSERT INTO
Adds new data to a table
• CREATE TABLE
Creates a new table
• ALTER TABLE
Modifies an existing table
• DROP TABLE
Deletes and existing table
A key feature of relational databases is the fact that you can create
relationships between tables, which allows you to create interrelated data sets.
LX0-104 Exam Objectives (G)
You can use the following commands to manage data in an SQL database:
• SELECT
Retrieves information from a table
• UPDATE
Modifies information in a table
• DELETE
Removes information from a table
• INSERT INTO
Adds new data to a table
• CREATE TABLE
Creates a new table
• ALTER TABLE
Modifies an existing table
• DROP TABLE
Deletes and existing table
A key feature of relational databases is the fact that you can create
relationships between tables, which allows you to create interrelated data sets.
LX0-104 Exam Objectives (G)
Wednesday, September 28, 2016
find but exclude directory and exclude user
find . ! -path "/path/folder/*" -name "*" ! -user kenmsipe -ls
find all files that are not located in /path/folder/* and do not have a user of kenmsipe
find all files that are not located in /path/folder/* and do not have a user of kenmsipe
Thursday, September 22, 2016
fsck options
fsck options
y
a
r
n
-y For some filesystem-specific checkers, the -y option will cause the fs-specific fsck to always
attempt to fix any detected filesystem corruption automatically. Sometimes an expert may be able to do better driving the fsck manually. Note that not all filesystem-specific checkers
implement this option. In particular fsck.minix(8) and fsck.cramfs(8) do not support the -y
option as of this writing.
-a Automatically repair the filesystem without any questions (use this option with caution). Note that e2fsck(8) supports -a for backward compatibility only. This option is mapped to e2fsck's -p option which is safe to use, unlike the -a option that some filesystem checkers support.
-r Interactively repair the filesystem (ask for confirmations). Note: It is generally a bad idea
to use this option if multiple fsck's are being run in parallel. Also note that this is
e2fsck's default behavior; it supports this option for backward compatibility reasons only.
-n For some filesystem-specific checkers, the -n option will cause the fs-specific fsck to avoid
attempting to repair any problems, but simply report such problems to stdout. This is however not true for all filesystem-specific checkers. In particular, fsck.reiserfs(8) will not report
any corruption if given this option. fsck.minix(8) does not support the -n option at all.
Monday, September 19, 2016
Shell commands you can use to manage kernel modules
Shell commands you can use to manage kernel modules. These include the following:
lsmod {Views loaded kernel modules}
modinfo {Views module information}
depmod {Builds a module dependency list}
insmod {Installs a kernel module but doesn’t factor in module dependencies}
modprobe {Installs or removes a kernel module while taking module dependencies into account}
rmmod {Removes a kernel module but doesn’t factor in module dependencies}
lsmod {Views loaded kernel modules}
modinfo {Views module information}
depmod {Builds a module dependency list}
insmod {Installs a kernel module but doesn’t factor in module dependencies}
modprobe {Installs or removes a kernel module while taking module dependencies into account}
rmmod {Removes a kernel module but doesn’t factor in module dependencies}
Command-line tools to view information about the hardware
You can also use the following command-line tools to view information about the hardware in your system:
hdparm /dev/device
sg_scan
sginfo –l
hwinfo
lshw
lsusb
lspci
hdparm /dev/device
sg_scan
sginfo –l
hwinfo
lshw
lsusb
lspci
Sunday, September 11, 2016
Systemctl
https://www.freedesktop.org/wiki/
www/ Software/ systemd/ FrequentlyAskedQuestions
Frequently Asked Questions
Also check out the Tips & Tricks!
Q: How do I change the current runlevel?
A: In systemd runlevels are exposed via "target units". You can change them like this:
# systemctl isolate runlevel5.target
Note however, that the concept of runlevels is a bit out of date, and it is usually nicer to use modern names for this. e.g.:
# systemctl isolate graphical.target
This will only change the current runlevel, and has no effect on the next boot.
Q: How do I change the default runlevel to boot into?
A: The symlink /etc/systemd/system/default.target controls where we boot into by default. Link it to the target unit of your choice. For example, like this:
# ln -sf /usr/lib/systemd/system/multi-user.target /etc/systemd/system/default.target
or
# ln -sf /usr/lib/systemd/system/graphical.target /etc/systemd/system/default.target
Q: How do I figure out the current runlevel?
A: Note that there might be more than one target active at the same time. So the question regarding the runlevel might not always make sense. Here's how you would figure out all targets that are currently active:
$ systemctl list-units --type=target
If you are just interested in a single number, you can use the venerable runlevel command, but again, its output might be misleading.
Q: I want to change a service file, but rpm keeps overwriting it in /usr/lib/systemd/system all the time, how should I handle this?
A: The recommended way is to copy the service file from /usr/lib/systemd/system to /etc/systemd/system and edit it there. The latter directory takes precedence over the former, and rpm will never overwrite it. If you want to use the distributed service file again you can simply delete (or rename) the service file in /etc/systemd/system again.
Q: My service foo.service as distributed by my operating system vendor is only started when (a connection comes in or some hardware is plugged in). I want to have it started always on boot, too. What should I do?
A: Simply place a symlink from that service file in the multi-user.target.wants/ directory (which is where you should symlink everything you want to run in the old runlevel 3, i.e. the normal boot-up without graphical UI. It is pulled in by graphical.target too, so will be started for graphical boot-ups, too):
# ln -sf /usr/lib/systemd/system/foobar.service /etc/systemd/system/multi-user.target.wants/foobar.service
# systemctl daemon-reload
Q: I want to enable another getty, how would I do that?
A: Simply instantiate a new getty service for the port of your choice (internally, this places another symlink for instantiating another serial getty in the getty.target.wants/ directory).
# systemctl enable serial-getty@ttyS2.service
# systemctl start serial-getty@ttyS2.service
Note that gettys on the virtual console are started on demand. You can control how many you get via the NAutoVTs= setting in logind.conf(7). Also see this blog story.
Q: How to I figure out which service a process belongs to?
A: You may either use ps for that:
$ alias psc='ps xawf -eo pid,user,cgroup,args'
$ psc
...
Or you can even check /proc/$PID/cgroup directly. Also see this blog story.
Q: Why don't you use inotify to reload the unit files automatically on change?
A: Unfortunately that would be a racy operation. For an explanation why and how we tried to improve the situation, see the bugzilla report about this.
Q: I have a native systemd service file and a SysV init script installed which share the same basename, e.g. /usr/lib/systemd/system/foobar.service vs. /etc/init.d/foobar -- which one wins?
A: If both files are available the native unit file always takes precedence and the SysV init script is ignored, regardless whether either is enabled or disabled. Note that a SysV service that is enabled but overridden by a native service does not have the effect that the native service would be enabled, too. Enabling of native and SysV services is completely independent. Or in other words: you cannot enable a native service by enabling a SysV service by the same name, and if a SysV service is enabled but the respective native service is not, this will not have the effect that the SysV script is executed.
Q: How can I use journalctl to display full (= not truncated) messages even if less is not used?
A: Use:
# journalctl --full
Q: Whenever my service tries to acquire RT scheduling for one of its threads this is refused with EPERM even though my service is running with full privileges. This works fine on my non-systemd system!
A: By default, systemd places all systemd daemons in their own cgroup in the "cpu" hierarchy. Unfortunately, due to a kernel limitation, this has the effect of disallowing RT entirely for the service. See My Service Can't Get Realtime! for a longer discussion and what to do about this.
Q: My service is ordered after network.target but at boot it is still called before the network is up. What's going on?
A: That's a long story, and that's why we have a wiki page of its own about this: Running Services After the Network is up
Q: My systemd system always comes up with /tmp as a tiny tmpfs. How do I get rid of this?
A: That's also a long story, please have a look on API File Systems
Last edited Sun May 26 10:17:02 2013
www/ Software/ systemd/ FrequentlyAskedQuestions
Frequently Asked Questions
Also check out the Tips & Tricks!
Q: How do I change the current runlevel?
A: In systemd runlevels are exposed via "target units". You can change them like this:
# systemctl isolate runlevel5.target
Note however, that the concept of runlevels is a bit out of date, and it is usually nicer to use modern names for this. e.g.:
# systemctl isolate graphical.target
This will only change the current runlevel, and has no effect on the next boot.
Q: How do I change the default runlevel to boot into?
A: The symlink /etc/systemd/system/default.target controls where we boot into by default. Link it to the target unit of your choice. For example, like this:
# ln -sf /usr/lib/systemd/system/multi-user.target /etc/systemd/system/default.target
or
# ln -sf /usr/lib/systemd/system/graphical.target /etc/systemd/system/default.target
Q: How do I figure out the current runlevel?
A: Note that there might be more than one target active at the same time. So the question regarding the runlevel might not always make sense. Here's how you would figure out all targets that are currently active:
$ systemctl list-units --type=target
If you are just interested in a single number, you can use the venerable runlevel command, but again, its output might be misleading.
Q: I want to change a service file, but rpm keeps overwriting it in /usr/lib/systemd/system all the time, how should I handle this?
A: The recommended way is to copy the service file from /usr/lib/systemd/system to /etc/systemd/system and edit it there. The latter directory takes precedence over the former, and rpm will never overwrite it. If you want to use the distributed service file again you can simply delete (or rename) the service file in /etc/systemd/system again.
Q: My service foo.service as distributed by my operating system vendor is only started when (a connection comes in or some hardware is plugged in). I want to have it started always on boot, too. What should I do?
A: Simply place a symlink from that service file in the multi-user.target.wants/ directory (which is where you should symlink everything you want to run in the old runlevel 3, i.e. the normal boot-up without graphical UI. It is pulled in by graphical.target too, so will be started for graphical boot-ups, too):
# ln -sf /usr/lib/systemd/system/foobar.service /etc/systemd/system/multi-user.target.wants/foobar.service
# systemctl daemon-reload
Q: I want to enable another getty, how would I do that?
A: Simply instantiate a new getty service for the port of your choice (internally, this places another symlink for instantiating another serial getty in the getty.target.wants/ directory).
# systemctl enable serial-getty@ttyS2.service
# systemctl start serial-getty@ttyS2.service
Note that gettys on the virtual console are started on demand. You can control how many you get via the NAutoVTs= setting in logind.conf(7). Also see this blog story.
Q: How to I figure out which service a process belongs to?
A: You may either use ps for that:
$ alias psc='ps xawf -eo pid,user,cgroup,args'
$ psc
...
Or you can even check /proc/$PID/cgroup directly. Also see this blog story.
Q: Why don't you use inotify to reload the unit files automatically on change?
A: Unfortunately that would be a racy operation. For an explanation why and how we tried to improve the situation, see the bugzilla report about this.
Q: I have a native systemd service file and a SysV init script installed which share the same basename, e.g. /usr/lib/systemd/system/foobar.service vs. /etc/init.d/foobar -- which one wins?
A: If both files are available the native unit file always takes precedence and the SysV init script is ignored, regardless whether either is enabled or disabled. Note that a SysV service that is enabled but overridden by a native service does not have the effect that the native service would be enabled, too. Enabling of native and SysV services is completely independent. Or in other words: you cannot enable a native service by enabling a SysV service by the same name, and if a SysV service is enabled but the respective native service is not, this will not have the effect that the SysV script is executed.
Q: How can I use journalctl to display full (= not truncated) messages even if less is not used?
A: Use:
# journalctl --full
Q: Whenever my service tries to acquire RT scheduling for one of its threads this is refused with EPERM even though my service is running with full privileges. This works fine on my non-systemd system!
A: By default, systemd places all systemd daemons in their own cgroup in the "cpu" hierarchy. Unfortunately, due to a kernel limitation, this has the effect of disallowing RT entirely for the service. See My Service Can't Get Realtime! for a longer discussion and what to do about this.
Q: My service is ordered after network.target but at boot it is still called before the network is up. What's going on?
A: That's a long story, and that's why we have a wiki page of its own about this: Running Services After the Network is up
Q: My systemd system always comes up with /tmp as a tiny tmpfs. How do I get rid of this?
A: That's also a long story, please have a look on API File Systems
Last edited Sun May 26 10:17:02 2013
Thursday, September 8, 2016
Processing Text Streams
Processing Text Streams
When you’re processing text streams
within a script or when piping output at the shell prompt, there may be times
when you need to filter the output of one command so that only certain portions
of the text stream are actually passed along to the stdin of the next command.
You can use a variety of tools to do this. In the last part of this chapter,
we’ll look at using the following commands:
•cut
•expand
and unexpand
•fmt
•join
and paste
•nl
•od
•pr
•sed
and awk
•sort
•split
•tr
•uniq
•wc
cut
The cut command is used to print
columns or fields that you specify from a file to the standard output. By
default, the tab character is used as a delimiter. The following options can be used with cut:
–blist Select only these bytes.
–clist Select only these characters.
–ddelim Use the specified character instead
of tab for the field delimiter.
–flist Select only the specified fields.
Print any line that contains no delimiter character,
unless the –s option is specified.
–s Do
not print lines that do not contain delimiters.
For example, you could use the cut
command to display all group names from the /etc/group file. Remember, the name
of each group is contained in the first field of each line of the file.
However, the group file uses colons
as the delimiter between fields, so you must specify a colon instead of a tab
as the delimiter. The command to do this is cut –d: –f1 /etc/group
.
expand and unexpand
The expand command is used to process a text stream and remove all
instances of the tab character and replace them with the specified number of
spaces (the default is eight). You can use the –t number option to specify a different number of spaces. The
syntax is
expand –t
number filename
.
In Figure 14-3, the tab characters
in the tabfile file are replaced with five spaces.
You can also use the unexpand
command. The unexpand command works in the opposite manner as the expand
command. It converts spaces in a text stream into tab characters. By default, eight
contiguous spaces are converted into tabs. However, you can use the –t option
to specify a different number of spaces.
It’s important to note that, by
default, unexpand will only convert
leading spaces at the beginning of each line. To force it to convert all spaces
of the correct number to tabs, you must include the –a option with the unexpand command.
fmt
You can use the fmt command to reformat a text file. It is commonly used to change
the wrapping of long lines within the file to a more manageable width. The
syntax for using fmt is fmt option filename
For example, you could use the –w
option with the fmt command to narrow the text of a file
to 80 columns by entering fmt –w 80 filename
join and paste
The join command prints a line from each of two specified input files
that have identical join fields. The
first field is the default join field, delimited by white space. You can
specify a different join field using the –j
field option.
For example, suppose you have two
files. The first file (named firstnames) contains the following content:
1 Mike
2 Jenny
3 Joe
The second file (named lastnames)
contains the following content:
1 Johnson
2 Doe
3 Jones
You can use the join command to join
the corresponding lines from each file by entering
join –j 1 firstnames lastnames
. This is shown here:
rtracy@openSUSE:~> join -j 1 firstnames lastnames
1 Mike Johnson
2 Jenny Doe
3 Joe Jones
The paste command works in much the
same manner as the join command. It pastes togethe
corresponding lines from one or more
files into columns. By default, the tab character is used to
separate columns. You can use the –dn
option to specify a different delimiter character. You can also use the –s
option to put the contents of each file into a single line.
For example, you could use the paste
command to join the corresponding lines from the firstnames and lastnames files
by entering
paste firstnames lastnames
. An example is shown here:
rtracy@openSUSE:~> paste firstnames lastnames
1 Mike 1 Johnson
2 Jenny 2 Doe
3 Joe 3 Jones
nl
The nl command determines the number of lines in a file. When you run
the command, the
output is written with a line number
added to the beginning of each line in the file. The syntax is nl filename
For example, in the example shown
here, the nl command is used to add
a number to the beginning of each line in the tabfile.txt file:
rtracy@openSUSE:~> nl tabfile.txt
1 This file uses tabs.
2 This line used a tab.
3 This line used a tab.
4 After using expand, the tabs will be replaced with spaces.
od
The od (octal dump) command is used to dump a file, including binary
files. This utility can
dump a file in several different
formats, including octal, decimal, floating
point, hex, and character format. The output from od is simple text, so you can use the other stream-processing tools
we’ve been looking at to further filter it.
The od command can be very useful. For example, you can perform a dump
of a file to locate stray characters in a file. Suppose you created a script
file using an editor on a different operating system (such as Windows) and then
tried to run it on Linux. Depending on which editor you used, there may be
hidden formatting characters within the script text that aren’t displayed by your
text editor. However, they will be read by the bash shell when you try to run
the script, thus causing errors. When you look at the script in an editor,
everything seems fine.
You could use the od command to view a dump of the script
to isolate where the problem-
causing characters are located in
the file. The syntax for using od is od options
filename
. Some of the more commonly used options include the following:
–b Octal
dump
–d
Decimal dump
–x Hex
dump
–c Character
dump
For example, “Hello World” script
has been created in the LibreOffice word processor and saved as an .odt file.
As such, it has a myriad of hidden characters embedded in the text.
These characters obviously cannot be
viewed from within LibreOffice. However, they can be viewed using the od –c helloworld.odt command.
pr
The pr command is used to format text files for printing. It formats the file with pagination, headers, and columns. The header
contains the date and time, filename, and page number. You can use the
following options with pr:
–d Double-space
the output.
–l page_length Set
the page length to the specified number of lines. The default is 66.
–o margin
Offset each line with the specified
number of spaces. The default margin is 0.
sed and awk
The sed command is a stream text editor. Unlike the interactive text
editors that you’ve already learned how to use in this book, such as vi, a
stream editor takes a stream of text as its stdin and then performs operations
on it that you specify. Then, sed sends the results to stdout. You can use the following commands with sed:
s Replaces
instances of a specified text string with another text string. The syntax for
using the s command is sed s/term1/term2/
For example, I’ve used the cat command to display a file in
the tux user’s home directory named lipsum.txt. I then use cat to read
lipsum.txt and then pipe the stdout to the stdin of the sed command and specify
that the term “ipsum” be replaced with “IPSUM.”
d Deletes
the specified text. For example, to delete every line of text from the stdin
that
contains the term “eos,” you would
enter sed /eos/d
.
Remember, sed doesn’t actually
modify the source of the information—in this case, the lipsum.txt file. It
takes its stdin, makes the changes, and sends it to the stdout. If you want to
save the changes made by sed, you need to redirect its stdout to a file using >
For example, I could redirect the
output from the command in Figure 14-8 to a file named lipsum_out.txt by
entering cat lipsum.txt | sed
s/ipsum/IPSUM/ > lipsum_out.txt at the shell prompt.
In addition to sed, you can also use
awk to manipulate output. Like sed, awk
can be used to
receive output from another command as its stdin and
manipulate it in a manner you specify.
However, the way awk does this is a
little bit different. The awk command
treats each line of text it receives as a record. Each word in the line,
separated by a space or tab character, is treated as a separate field within
the record.
For example, consider the following
text file:
Lorem ipsum dolor sit amet,
consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua.
Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in
reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum
According to awk, this file has
seven records because it has seven separate lines of text. Each line of text
has a carriage return/linefeed character at the end that creates a new line.
This is the character awk uses to define the end of a record. The first record
has eight fields, the second record has 11 fields, and so on.
Notice that white space, not
punctuation, delimits the fields. Each field is referenced as $
field_number.
For example, the first field of any
record is referenced as $1, the second as $2, and so on.
Using awk, we can specify a field in
a specific record and manipulate it in some manner. The syntax for using awk is
awk ‘pattern{manipulation}’
For example, we could enter
cat lipsum2.txt | awk ‘{print $1,$2,$3}’
to print out the first three words
(“fields”) of each line (“records”).
Because we didn’t specify a pattern
to match on, awk simply prints out the first three words of every line.
You can also include a pattern to
specify exactly which records to search on. For example,
suppose we only wanted to display
the first three fields of any record that includes the text “do” somewhere in
the line. To do this, you add a pattern of /do/
to the command.
You can also add your own text to
the output. Just add it to the manipulation part of the command within quotes.
In fact, you can also add control characters to output as well. Use the
following:
\t
Inserts a tab
character
\n Adds
a newline character
\f Adds
a form feed character
\r Adds
a carriage return character
For example, in Figure 14-11, I’ve
entered
cat
lipsum.txt | awk ‘/do/ {print "Field 1: "$1"\t",
"Field 2: "$2"\t",
"Field
3: "$3"\t"}’
which causes each field to be
labeled Field 1, Field 2, and Field 3
.
It also inserts a tab character between each
field. As with sed, awk doesn’t modify the original file. It sends its output
to stdout (the screen). If you want to send it to a file, you can redirect it
using >
sort
The sort command sorts the lines of a text file alphabetically. The
output is written to the standard output. Some commonly used options for the sort command include
the following:
–f
Fold lowercase characters to
uppercase characters.
–M Sort
by month.
–n Sort
numerically.
–r Reverse
the sort order.
For example, the sort –n –r firstnames
command sorts the lines in the
firstnames file numerically in reverse order. This is shown here:
rtracy@openSUSE:~> sort –n –r firstnames
3 Joe
2 Jenny
1 Mike
The sort command can be used to sort
the output of other commands (such as ps) by piping
the standard output of the first
command to the standard input of the sort command.
split
The split command splits an input file into a series of files (without
altering the original input file). The default is to split the input file into
1,000-line segments. You can use the –n option to specify a different number of
lines.
For example, the split –1 firstnames outputfile_ command
can be used to split the firstnames file into three separate files, each
containing a single line.
tr
The tr command is used to translate
or delete characters. However, be aware that this command does not work
with files. To use it with files, you must first use a command such as cat to
send the text stream to the standard input of tr. The syntax is
tr options X Y
Some commonly used options for the tr command include the
following:
–c Use
all characters not in X.
–d Delete
characters in X; do not translate.
–s Replace
each input sequence of a repeated character that is listed in X with a single occurrence
of that character.
–t First
truncate X to the length of Y.
For example, to translate all
lowercase characters in the lastnames file to uppercase characters, you could
enter cat lastnames | tr a-z A-Z
, as shown in this example:
rtracy@openSUSE:~> cat lastnames | tr a-z A-Z
1 JOHNSON
2 DOE
3 JONES
uniq
The uniq command reports or omits repeated lines. The syntax is
uniq options input output
You can use the following options with the
uniq command:
–d Only
print duplicate lines.
–u Only
print unique lines.
For example, suppose our lastnames
file contained duplicate entries:
1 Johnson
1 Johnson
2 Doe
3 Jones
You could use the uniq lastnames command to remove the
duplicate lines. This is shown in
the following example:
rtracy@openSUSE:~> uniq lastnames
1 Johnson
2 Doe
3 Jones
Be aware that the uniq command only works if the duplicate
lines are adjacent to each other.
If the text stream you need to work with contains duplicate
lines that are not adjacent, you can use the sort command to first make them
adjacent and then pipe the output to the standard input of uniq.
wc
The wc command prints the number of newlines, words, and bytes in a file. The syntax is
wc options files
. You can use the following options
with the wc command:
–c Print
the byte counts.
–m Print
the character counts.
–l Print
the newline counts.
–L Print
the length of the longest line.
–w Print
the word counts.
For example, to print all counts and
totals for the firstnames file, you would use the
wc firstnames
command, as shown in this example:
rtracy@openSUSE:~> wc firstnames
3 6 21 firstnames
Subscribe to:
Posts (Atom)