RHCE Notes v2.1
RHCE Notes v2.1
RHCE
I. vim /etc/yum.repos.d/rhce.repo
====
[RHCE_RHEL7]
name=RHCE_RHEL7
baseurl=http://.../.../...
enabled=1
gpgcheck=0
yum repolist
II. a/ allow SSH for xyz.com and deny SSH to all the others:
vim /etc/hosts.allow -> sshd: .xyz.com
vim /etc/hosts.deny -> sshd: ALL
b/ allow SSH for only specific IP and block all the others:
vim /etc/hosts.deny -> sshd: ALL EXCEPT 192.168.0.1
c/ denies all services to all hosts unless permitted in hosts.allow:
vim /etc/hosts.allow -> ALL: .foobar.edu EXCEPT
terminalserver.foobar.edu
vim /etc/hosts.deny -> ALL
d/ access granted by default, redundant file hosts.allow
vim /etc/hosts.deny -> some.host.name, .some.domain
vim /etc/hosts.deny -> ALL EXCEPT in.fingerd:
other.host.name, .other.domain
e/ rules can be also only in one file, for example:
vim /etc/hosts.allow -> ALL: .friendly.domain: ALLOW
1 SERVICES
systemctl --failed --type=service
systemctl status <-l> <unit>
systemctl stop|start|restart|reload <unit>
systemctl mask|unmask <unit>
systemctl enable|disable <unit>
systemctl list-dependencies <unit>
systemctl list-units --type=service --all
systemctl list-unit-files --type=service
systemctl get-default
systemctl set-default <graphical|multi-user|rescue|emergency>
systemctl isolate <graphical|multi-user|rescue|emergency>
2 IPV4
nmcli dev status
nmcli con show <name>
nmcli con show --active
ip addr show <eth0> ... ip a
ip link ... ip l
nmcli con add con-name <name> type ethernet ifname <eth0> ip4 xxx.xxx.xx.x/24 gw4
xxx.xxx.xx.x
nmcli con <up|down> <name>
nmcli dev status
nmcli dev dis <eth0>
nmcli con mod <name> +ipv4.dns xxx.xxx.xx.x
vim /etc/sysconfig/network-script/ifcfg-<name>
nmcli con reload
nmcli con del <name>
hostname
hostnamectl set-hostname <name>
vim /etc/hostname
hostnamectl status
ip route ... ip r
ss -tulpn | grep sshd (-another utility to investigate sockets)
3 IPV6
nmcli con add con-name <name> type ethernet ifname <eth0> ip6
xxxx:xxxx:xxx:x:x:x/64 gw6 xxxx:xxxx:xxx:x:x:x
ip -6 route show
ping6 xxxx:xxxx:xxx:x:x:x
ping6 xxxx:xxxx:xxx:x:x:x<%eth1> for link-local addresses and multicast groups
tracepath6 xxxx:xxxx:xxx:x:x:x
ss -A inet -n
netstat -46n (-print network connections, routing tables, interface statistics,
masquerade connections, and multicast memberships)
5 BRIDGING
a/ nmcli con add con-name <bridge0> type bridge ifname <br0>
b/ nmcli con add con-name <bridge0-port1> type bridge-slave ifname <eth0> master
<br0>
c/ nmcli con add con-name <bridge0-port2> type bridge-slave ifname <eth1> master
<br0>
brctl show
9 iSCSI
a/ Targets - server creating
yum -y install targetcli
LVM: fdisk <device> => type 8e; pvcreate <partition>; vgcreate <vgname>
<partition>; lvcreate -n <lvname> -L <size> <vgname>
fdisk /dev/vdb => type 8e; pvcreate /dev/vdb1; vgcreate
iSCSI_vg /dev/vdb1; lvcreate -n disk1_lv -L 100m iSCSI_vg
targetcli
cd /backstores
block/ create <block1> /dev/iSCSI_vg/disk1_lv
block/ create <block2> /dev/vdb2
block/ create <file1> /root/disk1_file 100M
cd /iscsi
create iqn.2015-10.com.example:server
cd iqn.2015-10.com.example:server/tpg1
acls/ create iqn.2015-10.com.example:<client.example.com>
luns/ create /backstores/block/block1
luns/ create /backstores/block/block2
luns/ create /backstores/fileio/file1
portals/ create 172.25.0.11
exit
firewall-cmd --permanent --add-port=3260/tcp
firewall-cmd --reload
systemctl enable target
b/ Targets - client accessing
yum -y install iscsi-initiator-utils
vim /etc/iscsi/initiatorname.iscsi (InitiatorName=client.example.com)
systemctl restart iscsi
systemctl enable iscsi
iscsiadm -m discovery -t sendtargets -p 172.25.0.11:3260
iscsiadm -m node -T iqn.2015-10.com.example:server -p 172.25.0.11 -l
lsblk
fdisk, mkfs.xfs ...
vim /etc/fstab
UUID=xxxxx-xxxxx-xxxxx /mnt/iscsi xfs _netdev 0 2
mount -av
iscsiadm -m session -P 3
ls -lR /var/lib/iscsi/nodes
c/ Targets - client disconnecting
iscsiadm -m node -T iqn.2015-10.com.example:server -p 172.25.0.11 -u
iscsiadm -m node -T iqn.2015-10.com.example:server -p 172.25.0.11 -o delete
lsblk
systemctl restart iscsi
+--------------------+------------------+---------------------------------+
| Special permission | Effect on files | Effect on directories |
+--------------------+------------------+---------------------------------+
| u+s (suid) | Executes as user | --- |
| 4xxx | who owns, not | |
| | who runs | |
+--------------------+------------------+---------------------------------+
| g+s (sgid) | Executes as grp | New files have grp owner match |
| 2xxx | that owns, not | grp owner of the dir |
| | who runs | |
+--------------------+------------------+---------------------------------+
| o+t (sticky) | --- | Users who can write to the dir |
| 1xxx | | can only remove their own files |
+--------------------+------------------+---------------------------------+
13 APACHE #http://localhost/manual#
yum -y install httpd httpd-manual
grep -v '^#' /etc/httpd/conf.d/httpd.conf >
/etc/httpd/conf.d/httpd_without_comments.conf
cp /etc/httpd/conf/httpd.conf ~/httpd.conf.orig
vim /etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd" (-where are the config files)
Listen 80 (-can be
1.2.3.4:80, multiple ports must be specified on separate lines)
Include conf.modules.d/*.conf (-if multiple are present, they will be
alphabetically included)
User apache
Group apache
ServerAdmin root@localhost
<Directory /> (-directives
specific to the dir and all descendent dirs)
AllowOverride none (-.htaccess will not be
used)
Require all denied (-refuse to serve conten
from dir)
</Directory>
DocumentRoot "/var/www/html" (-where apache looks for HTML files)
<Directory "/var/www/">
AllowOverride none
Require all granted
</Directory>
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride none
Require all granted
</Directory>
<IfModule dir_module> (-if this module is loaded, what
happens)
DirectoryIndex index.html (-this file will be used when the
direcory is requested)
</IfModule>
<Files ".ht*"> (-same as
directory, but for file wildcards)
Require all denied
</Files>
ErrorLog "logs/error_log" (-it will go to
/etc/httpd/logs/error_log, which is symlink to /var/log/httpd/error_log)
LogLevel warn
CustomLog "logs/access_log" combined
AddDefaultCharset UTF-8 (-can be disabled by
AddDefaultCharset Off)
IncludeOptional conf.d/*.conf (-same as regular include)
httpd -t (-this is to validate the config files)
systemctl enable httpd
systemctl start httpd
firewall-cmd --permanent --add-service=http --add-service=https
firewall-cmd --reload
a/ New DocumentRoot for group 'webmasters'
mkdir -p -m 2775 /new/web (-same as chmod u+rw,g+rws,o+rx /new/web)
chgrp webmasters /new/web
chmod 2775 /new/web
setfacl -R -m g:webmasters:rwX /new/web (X=retain executable
settings,directories allow directory search,x=executable)
setfacl -R -m d:g:webmasters:rwX /new/web
semanage fcontext -a -t httpd_sys_content_t "/new/web(/.*)?"
restorecon -Rv /new/web
systemctl reload httpd
b/ Virtual hosts
vim /etc/httpd/conf.d/00-site1.conf
<Directory /srv/site1/www> (-this block provides access to
document root further down)
Require all granted
AllowOverride none
</Directory>
<VirtualHost 192.168.0.1:80> (-this block must be considered for all
connections on 192.168.0.1:80, can be _default_:80 or *:80)
DocumentRoot /srv/site1/www (-only applies for within this virtual
host)
ServerName site1.example.com (-name-based virtual hosting, if multiple
virtual hosts are defined, the one where hostname matches this will be used)
ServerAlias site1 (-if the virtual
host needs to be used for more than one domain name)
ServerAdmin [email protected]
ErrorLog "logs/site1_error_log"
CustomLog "logs/site1_access_log" combined
</VirtualHost>
c/ Access control directives:
<RequireAll></RequireAll> - none must fail and at least one must succeed
<RequireAny></RequireAny> - one or more must succeed
<RequireNone></RequireNone> - none must succeed
If it is not enclosed in directives, it is automatically <RequireAny>
e.g.
I. <RequireAll>
Require all granted
Require not ip 10.252.46.125 (-address is an IP, partial
IP, network/mask, network/CIDR, ipv4/ipv6)
</RequireAll>
II. <RequireAll>
Require all granted
Require not ip 192.168.2.1
Require not host phishers.example.com moreidiots.example (-
address is FQDN or part of it, multiple may be provided)
Require not host gov
</RequireAll>
III. Require all denied
Require local
d/ SSL/TLS
yum -y install crypto-utils mod_ssl
genkey <www.example.com>
cp /etc/httpd/conf.d/ssl.conf ~/ssl.conf.orig
grep -v '^#' /etc/httpd/conf.d/ssl.conf >
/etc/httpd/conf.d/ssl_without_comments.conf
vim /etc/httpd/conf.d/ssl.conf
Listen 443 https
SSLPassPhraseDialog exec:/usr/libexec/httpd-ssl-pass-dialog
(-if the private key uses passphrase)
<VirtualHost _default_:443>
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
(SSLHonorCipherOrder On)
SSLCertificateFile /etc/pki/tls/certs/www.example.com.crt
(-public key)
SSLCertificateKeyFile /etc/pki/tls/private/www.example.com.key (-
private key)
SSLCertificateChainFile /etc/pki/tls/certs/example-ca.crt
(-copy of all CA certificates)
</VirtualHost>
ls -Zd /etc/pki/tls/
semanage fcontext -a -t cert_t "/etc/pki/tls(/.*)?" (-it is already the default)
restorecon -Rv /etc/pki/tls/
chmod 0600 /etc/pki/tls/private/*.key (-same as chmod u+rw *.key)
chmod 0644 /etc/pki/tls/certs/*.crt (-same as chmod u+rw,g+r,o+r *.crt)
e/ HSTS - strict transport security
<VirtualHost *:80>
Header always set Strict-Transport-Security "max_age=15768000"
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_POST}$1 [redirect=301]
<VirtualHost>
f/ Dynamic content
I. CGI
vim /etc/httpd/conf/httpd.conf
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" (first directory
is part of the URL, second is the location of the script)
SELinux fcontext: httpd_sys_script_exec_t, httpd_enable_cgi
II. PHP (cp /etc/httpd/conf.d/php.conf ~/php.conf.orig)
yum -y install mod_php php php-mysql
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
DirectoryIndex index.php
III. Python
yum -y install mod_wsgi
vim /etc/httpd/conf/httpd.conf
WSGIScriptAlias /myapp "/srv/my.py" (a request for
www.example.com/myapp will cause the server to run the WSGI application defined
in /srv/my.py)
SELinux fcontext: httpd_sys_content_t
g/ SELinux: #man 8 httpd_selinux#
semanage port -l | grep '^http_'
semanage port -a -t http_port_t -p tcp 88 (-for non-standard HTTP ports)
semanage fcontext -a -t httpd_sys_content_t "/srv/site1/www(/.*)?"
restorecon -Rv /srv/site1/www
context:
httpd_sys_content_t - dirs where Apache is allowed to access
httpd_sys_content_rw_t - dirs where Apache is allowed to read/write
httpd_sys_script_exec_t - dirs that contain executable scripts
cert_t - dirs where Apache is allowed to read SSL certificates
booleans:
httpd_unified [default=off] - simplified/unified policy when turned on
httpd_enable_cgi [default=on] - allowed to run scripts
httpd_tty_comm [default=off] - Apache is allowed to access TTY, switch on when
using private key with passkey
httpd_can_network_connect_db [default=off] - if the database is on remote host
httpd_can_network_connect [default=off] - if the known port number is used for db
connection
14 SHELL ENVIRONMENT
a/ Global
/etc/profile
/etc/profile.d/*.sh
/etc/bashrc
b/ User
~/.bash_profile, .bash_login, .profile
~/.bashrc
I. Profiles are for setting and exporting of environment variables, as well as
running commands that should only be run upon login. Usually, profiles are only
executed in a login shell, whereas RCs are executed every time a shell is created,
login or non-login.
II. RCs are for running commands, setting aliases, defining functions and other
settings that cannot be exported to sub-shells.
export MYVAR
alias
unalias
function () {...}
set
unset
15 BASH
$VARIABLENAME vs. ${VARIABLENAME}
$FIRST_$LAST = $FIRST_ + $LAST
${FIRST}_$LAST = $FIRST +_ + $LAST
`CMD` == $(CMD)
$[<ARITHEMTIC EXPRESSION>]
Troubleshooting:
bash -x <SCRIPT> or 'set -x' ... 'set +x'
bash -v <SCRIPT> or 'set -v' ... 'set +v'
Comparison
[ "$A" -eq "$B" ]; ... $?
'eq' or '=' = equal
'ne' or '!=' = not equal
'gt' = greater than
'ge' = greater/equal than
'lt' = less than
'le' = less/equal than
'z' = string is null
'n' = string is not null
'b' = file exists & block special
'c' = file exists & character special
'd' = is directory
'e' = exists
'f' = is regular file
'L' = is symbolic lins
'r' = read permission granted
's' = non-zero size
'w' = write permission granted
'x' = execute permission granted
'ef' = same device & inode
'nt' = newer modification date
'ot' = older modification date
&& = AND
|| = OR
IF <CONDITION>; THEN
<CMD>
ELIF <STATEMENT>
ELSE <STATEMENT>
FI
CASE <VALUE> IN
<PATTERN1>) <STATEMENT>;;
<PATTERN2>) <STATEMENT>;;
<PATTERN3>) <STATEMENT>;;
<*>) ;;
ESAC
e.g.
a/
vim dbbackup
#!/bin/bash
#RHCE page 341, guided exercise
#Variables
DBUSER=root
FMTOPTIONS='--skip-column-names -E'
COMMAND='SHOW DATABASES'
BACKUPDIR=/dbbackup
#Report name, size, and percentage of total for each database dump
echo
for DBDUMP in $BACKUPDIR/*; do
SIZE=$(stat --print "%s\n" $DBDUMP)
echo "$DBDUMP,$SIZE,$[ 100 * $SIZE / $TOTAL ]%"
done
b/
vim mkaccounts.orig
#!/bin/bash
#RHCE page 347, lab exercise
#Variables
NEWUSERSFILE=/tmp/support/newusers
#Loop
for ENTRY in $(cat $NEWUSERSFILE); do
#Extract first, last and tier fields
FIRSTNAME=$(echo $ENTRY | cut -d: -f1)
LASTNAME=$(echo $ENTRY | cut -d: -f2)
TIER=$(echo $ENTRY | cut -d: -f4)
#Make account name
FIRSTINITIAL=$(echo $FIRSTNAME | cut -c 1 | tr 'A-Z' 'a-z')
LOWERLASTNAME=$(echo $LASTNAME | tr 'A-Z' 'a-z')
ACCTNAME=$$FIRSTINITIAL$LOWERLASTNAME
#Create account
useradd $ACCTNAME -c "$FIRSTNAME $LASTNAME"
done
TOTAL=$(cat $NEWUSERSFILE | wc -l)
TIER1COUNT=$(grep -c :1$ $NEWUSERSFILE)
TIER2COUNT=$(grep -c :2$ $NEWUSERSFILE)
TIER3COUNT=$(grep -c :3$ $NEWUSERSFILE)
TIER1PCT=$[ $TIER1COUNT * 100 / $TOTAL ]
TIER2PCT=$[ $TIER2COUNT * 100 / $TOTAL ]
TIER3PCT=$[ $TIER3COUNT * 100 / $TOTAL ]
c/
vim mkvhost
#!/bin/bash
#RHCE page 363, guided exercise
#Variables
VHOSTNAME=$1
TIER=$2
HTTPDCONF=/etc/httpd/conf/httpd.conf
VHOSTCONFDIR=/etc/httpd/conf.vhost.d
DEFHOSTCONFFILE=$VHOSTCONFDIR/00-default-vhost.conf
VHOSTCONFFILE=$VHOSTCONFDIR/$VHOSTNAME.conf
WWWROOT=/srv
DEFVHOSTDOCROOT=$WWWROOT/default/www
VHOSTDOCROOT=$WWWROOT/$VHOSTNAME/www
#Check arguments
if [ "$VHOSTNAME" = '' ] || [ "$TIER" = '' ]; then
echo "Usage: $0 VHOSTNAME TIER"
exit 1
else
if [ ! -d $DEFVHOSTDOCROOT ]; then
mkdir -p $DEFVHOSTDOCROOT
restorecon -Rv /srv/
fi
d/
vim mkaccounts
#!/bin/bash
#RHCE page 370, lab exercise
#Variables
OPTION=$1
NEWUSERSFILE=/tmp/support/newusers
case $OPTION in
'')
;;
-v) VERBOSE=y
;;
-h) echo "Usage: $0 [-h|-v]"
echo
exit
;;
*) echo "Usage: $0 [-h|-v]"
echo
exit 1
;;
esac
e/
vim myusers
#!/bin/bash
#RHCE page 419, comprehensive review lab
if [ $# -eq 0 ]; then
echo "$(basename $0) userlist"
echo "$(basename $0) userinfo <USERNAME>"
fi
case $1 in
userlist) grep -v ':/sbin/nologin$' /etc/passwd | cut -d: -f1 | sort
;;
userinfo) if [ "$2" == "" ]; then
echo "Please specify a username"
exit 132
fi
if ! getent passwd $2 &> /dev/null; then
echo "Invalid user"
exit
fi
getent passwd $2 | cut -d: -f7
;;
*) exit
;;
esac