####################
#### vsftpd服务    ####
####################
(接11.13的笔记)

虚拟用户只在ftp上是本地用户,而不是系统的本地用户

#<ftp虚拟用户的设定>
创建虚拟帐号身份)
vim /etc/vsftpd/loginusers    ##文件名称任意
ftpuser1
123
ftpuser2
123
ftpuser3
123

db_load -T -t hash -f /etc/vsftpd/loginusers loginusers.db
-T    ##表示转换
-t    ##指定加密方式

vim /etc/pam.d/ckvsftpd        ##文件名称任意
account        required    pam_userdb.so    db=/etc/vsftpd/loginusers
auth        required    pam_userdb.so    db=/etc/vsftpd/loginusers

vim /etc/vsftpd/vsftpd.conf
pam_service_name=ckvsftpd    ##此命令更改了认证方式,启用虚拟用户,停用本地用户
guest_enable=YES

虚拟帐号身份指定)
guest_username=ftpuser
chmod u-w /home/ftpuser

虚拟帐号家目录独立设定)
vim /etc/vsftpd/vsftpd.conf
local_root=/ftpuserhome/$USER
user_sub_token=$USER

==================================================

mkdir /ftpuserhome
chgrp ftpuser /ftpuserhome
chmod g+s /ftpuserhome
mkdir /ftpuserhome/ftpuser{1..3}

虚拟帐号配置独立)
vim /etc/vsftpd/vsftpd.conf
user_config_dir=/etc/vsftpd/userconf
mkdir -p /etc/vsftpd/userconf

vim /etc/vsftpd/userconf/ftpuser1
在此文件中设定配置文件中的所有参数,此文件的优先级更高

####################
[root@ftpserver ~]# vim /etc/sysconfig/selinux
--------------------------------------------------
  7 SELINUX=disabled
:wq
--------------------------------------------------
[root@ftpserver ~]# reboot
[root@ftpserver ~]# yum install vsftpd -y
......
[root@ftpserver ~]# systemctl start vsftpd
[root@ftpserver ~]# systemctl enable vsftpd
ln -s '/usr/lib/systemd/system/vsftpd.service' '/etc/systemd/system/multi-user.target.wants/vsftpd.service'
[root@ftpserver ~]# firewall-cmd --permanent --add-service=ftp
success
[root@ftpserver ~]# firewall-cmd --reload
success
[root@ftpserver ~]# firewall-cmd --list-all
public (default, active)
  interfaces: eth0
  sources:
  services: dhcpv6-client ftp ssh
  ports:
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:

[root@ftpserver ~]# vim /etc/vsftpd/vuser
--------------------------------------------------
westos1
123
westos2
123
westos3
123
:wq
--------------------------------------------------
[root@ftpserver ~]# db_load -T -t hash -f /etc/vsftpd/vuser /etc/vsftpd/vuser.db
[root@ftpserver ~]# ls /etc/vsftpd/
ftpusers  user_list  vsftpd.conf  vsftpd_conf_migrate.sh  vuser  vuser.db
[root@ftpserver ~]# rm -fr /etc/vsftpd/vuser
[root@ftpserver ~]# vim /etc/pam.d/vuser
--------------------------------------------------
account         required        pam_userdb.so   db=/etc/vsftpd/vuser
auth            required        pam_userdb.so   db=/etc/vsftpd/vuser
--------------------------------------------------
[root@ftpserver ~]# mkdir -p /ftpdir/westos1
[root@ftpserver ~]# mkdir -p /ftpdir/westos2
[root@ftpserver ~]# mkdir -p /ftpdir/westos3
[root@ftpserver ~]# touch /ftpdir/westos1/westosfile1
[root@ftpserver ~]# touch /ftpdir/westos2/westosfile2
[root@ftpserver ~]# touch /ftpdir/westos3/westosfile3
[root@ftpserver ~]# vim /etc/vsftpd/vsftpd.conf
--------------------------------------------------
125 pam_service_name=vuser

128 guest_enable=YES
129 local_root=/ftpdir/$USER
130 user_sub_token=$USER
:wq
--------------------------------------------------
[root@ftpserver ~]# systemctl restart vsftpd

[root@client ~]# yum install lftp -y
......
[root@client ~]# lftp 172.25.50.200 -u westos1
Password:
lftp westos1@172.25.50.200:~> ls      
-rw-r--r--    1 0        0               0 Nov 22 02:02 westosfile1
lftp westos1@172.25.50.200:/> put /etc/passwd
put: Access failed: 550 Permission denied. (passwd)
lftp westos1@172.25.50.200:/> exit

==================================================

[root@ftpserver ~]# vim /etc/vsftpd/vsftpd.conf
--------------------------------------------------
 29 anon_upload_enable=YES
:wq
--------------------------------------------------
[root@ftpserver ~]# systemctl restart vsftpd
[root@ftpserver ~]# mkdir /ftpdir/westos1/westos1pub
[root@ftpserver ~]# chmod 775 /ftpdir/westos1/westos1pub
[root@ftpserver ~]# chgrp ftp /ftpdir/westos1/westos1pub

[root@client ~]# lftp 172.25.50.200 -u westos1
Password:
lftp westos1@172.25.50.200:~> ls      
drwxrwxr-x    2 0        50              6 Nov 22 02:37 westos1pub
-rw-r--r--    1 0        0               0 Nov 22 02:02 westosfile1
lftp westos1@172.25.50.200:/> put /etc/passwd
put: Access failed: 553 Could not create file. (passwd)
lftp westos1@172.25.50.200:/> cd westos1pub/
lftp westos1@172.25.50.200:/westos1pub> put /etc/passwd
2005 bytes transferred
lftp westos1@172.25.50.200:/westos1pub> ls
-rw-------    1 14       50           2005 Nov 22 02:38 passwd
lftp westos1@172.25.50.200:/westos1pub> rm passwd
rm: Access failed: 550 Permission denied. (passwd)
lftp westos1@172.25.50.200:/westos1pub> exit

[root@ftpserver ~]# mkdir /ftpdir/westos2/westos2pub
[root@ftpserver ~]# mkdir /ftpdir/westos3/westos3pub
[root@ftpserver ~]# chmod 775 /ftpdir/westos*/*
[root@ftpserver ~]# chgrp ftp /ftpdir/westos*/*
[root@ftpserver ~]# man 5 vsftpd.conf
--------------------------------------------------
       user_config_dir
              This powerful option allows the override of  any  config  option
              specified in the manual page, on a per-user basis. Usage is sim‐
              ple, and is  best  illustrated  with  an  example.  If  you  set
              user_config_dir  to  be /etc/vsftpd/user_conf and then log on as
              the user "chris", then vsftpd will apply  the  settings  in  the
              file  /etc/vsftpd/user_conf/chris  for  the duration of the ses‐
              sion. The format of this file is  as  detailed  in  this  manual
              page!  PLEASE NOTE that not all settings are effective on a per-
              user basis. For example, many settings only prior to the  user's
              session  being  started.  Examples  of  settings  which will not
              affect any behviour on a per-user basis include  listen_address,
              banner_file, max_per_ip, max_clients, xferlog_file, etc.

              Default: (none)
--------------------------------------------------
[root@ftpserver ~]# vim /etc/vsftpd/vsftpd.conf
--------------------------------------------------
 29 #anon_upload_enable=YES

131 user_config_dir=/etc/vsftpd/vuser-conf
:wq
--------------------------------------------------
[root@ftpserver ~]# mkdir /etc/vsftpd/vuser-conf
[root@ftpserver ~]# cd /etc/vsftpd/vuser-conf/
[root@ftpserver vuser-conf]# vim westos1
--------------------------------------------------
anon_upload_enable=YES
:wq
--------------------------------------------------
[root@ftpserver vuser-conf]# systemctl restart vsftpd

[root@client ~]# lftp 172.25.50.200 -u westos1
Password:
lftp westos1@172.25.50.200:~> ls      
drwxrwxr-x    2 0        50             19 Nov 22 02:38 westos1pub
-rwxrwxr-x    1 0        50              0 Nov 22 02:02 westosfile1
lftp westos1@172.25.50.200:/> cd westos1pub/
lftp westos1@172.25.50.200:/westos1pub> ls
-rw-------    1 14       50           2005 Nov 22 02:38 passwd
lftp westos1@172.25.50.200:/westos1pub> put /etc/inittab
491 bytes transferred
lftp westos1@172.25.50.200:/westos1pub> ls
-rw-------    1 14       50            491 Nov 22 03:13 inittab
-rw-------    1 14       50           2005 Nov 22 02:38 passwd
lftp westos1@172.25.50.200:/westos1pub> exit
[root@client ~]# lftp 172.25.50.200 -u westos3
Password:
lftp westos3@172.25.50.200:~> ls
drwxrwxr-x    2 0        50              6 Nov 22 02:40 westos3pub
-rwxrwxr-x    1 0        50              0 Nov 22 02:02 westosfile3
lftp westos3@172.25.50.200:/> cd westos3pub/
lftp westos3@172.25.50.200:/westos3pub> ls
lftp westos3@172.25.50.200:/westos3pub> put /etc/inittab
put: Access failed: 550 Permission denied. (inittab)
lftp westos3@172.25.50.200:/westos3pub> exit
####################

550:请求操作未被执行,文件不可用
553:请求操作未被执行,文件名不允许

个人理解:550是配置文件的限制,553是目录权限或selinux的限制

##############################
###### selinux的初级管理    ######
##############################

1.什么是selinux
selinux,内核级加强型防火墙

2.如何管理selinux级别
selinux开启或者关闭)
vim /etc/sysconfig/selinux
selinux=disabled        ##关闭状态
selinux=Enforcing        ##强制状态
selinux=Permissive        ##警告状态

getenforce            ##查看状态
当selinux开启时
setenforce 0|1            ##更改selinux运行级别

####################
[root@ftpserver ~]# ll /etc/sysconfig/selinux
lrwxrwxrwx. 1 root root 17 May  6  2014 /etc/sysconfig/selinux -> ../selinux/config
####################

3.如何更改文件安全上下文
临时更改)
chcon -t 安全上下文    文件
chcon -t public_content_t /publicftp -R

永久更改)
semanage fcontext -l        ##列出内核安全上下文列表内容
semanage fcontext -a -t public_content_t '/publicftp(/.*)?'
restorecon -FvvR /publicftp/

####################
[root@ftpserver vuser-conf]# cd /mnt
[root@ftpserver mnt]# touch hello
[root@ftpserver mnt]# mv hello /var/ftp/pub/
[root@ftpserver mnt]# vim /etc/vsftpd/vsftpd.conf
--------------------------------------------------
 29 anon_upload_enable=YES
:wq
--------------------------------------------------
[root@ftpserver mnt]# systemctl restart vsftpd
[root@ftpserver mnt]# chmod 775 /var/ftp/pub/
[root@ftpserver mnt]# chgrp ftp /var/ftp/pub/

[root@client ~]# lftp 172.25.50.200
lftp 172.25.50.200:~> ls
drwxrwxr-x    2 0        50             18 Nov 22 06:52 pub
lftp 172.25.50.200:/> cd pub/
lftp 172.25.50.200:/pub> ls
-rw-r--r--    1 0        0               0 Nov 22 06:52 hello
lftp 172.25.50.200:/pub> put /etc/passwd
2005 bytes transferred
lftp 172.25.50.200:/pub> ls
-rw-r--r--    1 0        0               0 Nov 22 06:52 hello
-rw-------    1 14       50           2005 Nov 22 06:52 passwd
lftp 172.25.50.200:/pub> exit

[root@ftpserver mnt]# cd /var/ftp/pub/
[root@ftpserver pub]# ls --help
--------------------------------------------------
  -Z, --context              Display security context so it fits on most
                             displays.  Displays only mode, user, group,
                             security context and file name.
--------------------------------------------------
[root@ftpserver pub]# ls -Z
-rw-r--r-- root root ?                                hello
-rw------- ftp  ftp  ?                                passwd
[root@ftpserver mnt]# vim /etc/sysconfig/selinux
--------------------------------------------------
  7 SELINUX=enforcing
:wq
--------------------------------------------------
[root@ftpserver mnt]# getenforce
Disabled
[root@ftpserver mnt]# reboot
Connection to 172.25.50.200 closed by remote host.
Connection to 172.25.50.200 closed.
[root@foundation50 Desktop]# rht-vmctl view server
==============================
重新启动以后,系统会在一个地方卡很长时间
这是因为系统要给每一个文件加载一个安全上下文的标签
然后会再次重启,因为加载完标签以后要重新初始化
==============================
[root@ftpserver ~]# getenforce
Enforcing
[root@ftpserver ~]# cd /var/ftp/pub/
[root@ftpserver pub]# ls -Z
-rw-r--r--. root root system_u:object_r:public_content_t:s0 hello
-rw-------. ftp  ftp  system_u:object_r:public_content_t:s0 passwd
[root@ftpserver pub]# cd /mnt
[root@ftpserver mnt]# touch westos
[root@ftpserver mnt]# mv westos /var/ftp/pub/

[root@client ~]# lftp 172.25.50.200
lftp 172.25.50.200:~> cd pub/
lftp 172.25.50.200:/pub> ls
-rw-r--r--    1 0        0               0 Nov 22 06:52 hello
-rw-------    1 14       50           2005 Nov 22 06:52 passwd
lftp 172.25.50.200:/pub> put /etc/group
put: Access failed: 553 Could not create file. (group)
lftp 172.25.50.200:/pub> exit

[root@ftpserver mnt]# cd /var/ftp/pub/
[root@ftpserver pub]# ls -Z
-rw-r--r--. root root system_u:object_r:public_content_t:s0 hello
-rw-------. ftp  ftp  system_u:object_r:public_content_t:s0 passwd
-rw-r--r--. root root unconfined_u:object_r:mnt_t:s0   westos
[root@ftpserver pub]# ps axZ | grep vsftpd
system_u:system_r:ftpd_t:s0-s0:c0.c1023 1230 ? Ss     0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 1958 pts/0 S+   0:00 grep --color=auto vsftpd
[root@ftpserver pub]# > /var/log/messages

[root@client ~]# lftp 172.25.50.200
lftp 172.25.50.200:~> cd pub/
cd ok, cwd=/pub
lftp 172.25.50.200:/pub> ls
-rw-r--r--    1 0        0               0 Nov 22 06:52 hello
-rw-------    1 14       50           2005 Nov 22 06:52 passwd
lftp 172.25.50.200:/pub> exit

[root@ftpserver pub]# cat /var/log/messages
Nov 22 02:24:12 ftpserver dbus-daemon: dbus[527]: [system] Activating service name='org.fedoraproject.Setroubleshootd' (using servicehelper)
Nov 22 02:24:12 ftpserver dbus[527]: [system] Activating service name='org.fedoraproject.Setroubleshootd' (using servicehelper)
Nov 22 02:24:13 ftpserver dbus-daemon: dbus[527]: [system] Successfully activated service 'org.fedoraproject.Setroubleshootd'
Nov 22 02:24:13 ftpserver dbus[527]: [system] Successfully activated service 'org.fedoraproject.Setroubleshootd'
Nov 22 02:24:14 ftpserver setroubleshoot: Plugin Exception restorecon
Nov 22 02:24:14 ftpserver setroubleshoot: SELinux is preventing /usr/sbin/vsftpd from getattr access on the file . For complete SELinux messages. run sealert -l bdd154ae-9100-4e71-ae36-467e34e2c73c
Nov 22 02:24:14 ftpserver python: SELinux is preventing /usr/sbin/vsftpd from getattr access on the file .

*****  Plugin catchall_boolean (57.6 confidence) suggests   ******************

If you want to allow ftpd to full access
Then you must tell SELinux about this by enabling the 'ftpd_full_access' boolean.
You can read 'None' man page for more details.
Do
setsebool -P ftpd_full_access 1

*****  Plugin catchall_labels (36.2 confidence) suggests   *******************

If you want to allow vsftpd to have getattr access on the  file
Then you need to change the label on $FIX_TARGET_PATH        ##need to change the label
Do
# semanage fcontext -a -t FILE_TYPE '$FIX_TARGET_PATH'
where FILE_TYPE is one of the following: NetworkManager_log_t, NetworkManager_tmp_t, abrt_helper_exec_t, abrt_tmp_t, abrt_upload_watch_tmp_t, abrt_var_cache_t, abrt_var_log_t, abrt_var_run_t, acct_data_t, admin_crontab_tmp_t, afs_logfile_t, aide_log_t, alsa_tmp_t, amanda_log_t, amanda_tmp_t, antivirus_log_t, antivirus_tmp_t, apcupsd_log_t, apcupsd_tmp_t, apmd_log_t, apmd_tmp_t, arpwatch_tmp_t, asterisk_log_t, asterisk_tmp_t, auditadm_sudo_tmp_t, auth_cache_t, automount_tmp_t, awstats_tmp_t, bacula_log_t, bin_t, bitlbee_log_t, bitlbee_tmp_t, bluetooth_helper_tmp_t, bluetooth_tmp_t, boinc_log_t, boinc_project_tmp_t, boinc_tmp_t, boot_t, bootloader_tmp_t, calamaris_log_t, callweaver_log_t, canna_log_t, cardmgr_dev_t, ccs_tmp_t, ccs_var_lib_t, ccs_var_log_t, cdcc_tmp_t, cert_t, certmaster_var_log_t, cfengine_log_t, cgred_log_t, checkpc_log_t, chkpwd_exec_t, chrome_sandbox_tmp_t, chronyd_var_log_t, cloud_init_tmp_t, cloud_log_t, cluster_conf_t, cluster_tmp_t, cluster_var_lib_t, cluster_var_log_t, cluster_var_run_t, cobbler_tmp_t, cobbler_var_log_t, colord_tmp_t, comsat_tmp_t, condor_log_t, condor_master_tmp_t, condor_schedd_tmp_t, condor_startd_tmp_t, conman_log_t, consolekit_log_t, couchdb_log_t, couchdb_tmp_t, cpu_online_t, crack_tmp_t, cron_log_t, crond_tmp_t, crontab_tmp_t, ctdbd_log_t, ctdbd_tmp_t, cups_pdf_tmp_t, cupsd_log_t, cupsd_lpd_tmp_t, cupsd_tmp_t, cvs_tmp_t, cyphesis_log_t, cyphesis_tmp_t, cyrus_tmp_t, dbadm_sudo_tmp_t, dbskkd_tmp_t, dbusd_etc_t, dcc_client_tmp_t, dcc_dbclean_tmp_t, dccd_tmp_t, dccifd_tmp_t, dccm_tmp_t, ddclient_log_t, ddclient_tmp_t, deltacloudd_log_t, deltacloudd_tmp_t, denyhosts_var_log_t, devicekit_tmp_t, devicekit_var_log_t, dhcpc_tmp_t, dhcpd_tmp_t, dirsrv_snmp_var_log_t, dirsrv_tmp_t, dirsrv_var_log_t, dirsrvadmin_tmp_t, disk_munin_plugin_tmp_t, dkim_milter_tmp_t, dlm_controld_var_log_t, dnsmasq_var_log_t, docker_log_t, docker_tmp_t, dovecot_auth_tmp_t, dovecot_deliver_tmp_t, dovecot_tmp_t, dovecot_var_log_t, dspam_log_t, etc_runtime_t, etc_t, evtchnd_var_log_t, exim_log_t, exim_tmp_t, fail2ban_log_t, fail2ban_tmp_t, fail2ban_var_lib_t, faillog_t, fenced_tmp_t, fenced_var_log_t, fetchmail_log_t, file_context_t, fingerd_log_t, firewalld_tmp_t, firewalld_var_log_t, firewallgui_tmp_t, foghorn_var_log_t, fsadm_log_t, fsadm_tmp_t, fsdaemon_tmp_t, ftpd_etc_t, ftpd_exec_t, ftpd_keytab_t, ftpd_lock_t, ftpd_tmp_t, ftpd_tmpfs_t, ftpd_var_run_t, ftpdctl_tmp_t, games_tmp_t, gconf_tmp_t, gear_log_t, getty_log_t, getty_tmp_t, gfs_controld_var_log_t, gkeyringd_tmp_t, glance_log_t, glance_registry_tmp_t, glance_tmp_t, glusterd_log_t, glusterd_tmp_t, gpg_agent_tmp_t, gpg_pinentry_tmp_t, gpm_tmp_t, groupd_var_log_t, gssd_tmp_t, haproxy_var_log_t, hostname_etc_t, httpd_bugzilla_tmp_t, httpd_collectd_script_tmp_t, httpd_log_t, httpd_mojomojo_tmp_t, httpd_munin_script_tmp_t, httpd_php_tmp_t, httpd_suexec_tmp_t, httpd_tmp_t, httpd_w3c_validator_tmp_t, icecast_log_t, inetd_child_tmp_t, inetd_log_t, inetd_tmp_t, init_tmp_t, initrc_tmp_t, initrc_var_log_t, initrc_var_run_t, innd_log_t, ipsec_log_t, ipsec_tmp_t, iptables_tmp_t, iscsi_log_t, iscsi_tmp_t, iwhd_log_t, jetty_log_t, jockey_var_log_t, kadmind_log_t, kadmind_tmp_t, kdumpctl_tmp_t, kdumpgui_tmp_t, keystone_log_t, keystone_tmp_t, kismet_log_t, kismet_tmp_t, kismet_tmpfs_t, klogd_tmp_t, krb5_conf_t, krb5_home_t, krb5_host_rcache_t, krb5_keytab_t, krb5kdc_log_t, krb5kdc_tmp_t, ksmtuned_log_t, ktalkd_log_t, ktalkd_tmp_t, l2tpd_tmp_t, lastlog_t, ld_so_cache_t, ld_so_t, ldconfig_tmp_t, lib_t, livecd_tmp_t, local_login_home_t, locale_t, logrotate_exec_t, logrotate_mail_tmp_t, logrotate_tmp_t, logwatch_mail_tmp_t, logwatch_tmp_t, lpd_tmp_t, lpr_tmp_t, lsassd_tmp_t, lsmd_plugin_tmp_t, lvm_tmp_t, machineid_t, mail_munin_plugin_tmp_t, mailman_cgi_tmp_t, mailman_log_t, mailman_mail_tmp_t, mailman_queue_tmp_t, man_cache_t, man_t, mandb_cache_t, mcelog_log_t, mdadm_tmp_t, mock_tmp_t, mongod_log_t, mongod_tmp_t, motion_log_t, mount_tmp_t, mozilla_plugin_tmp_t, mozilla_tmp_t, mpd_log_t, mpd_tmp_t, mrtg_log_t, mscan_tmp_t, munin_log_t, munin_tmp_t, mysqld_log_t, mysqld_tmp_t, mythtv_var_log_t, nagios_eventhandler_plugin_tmp_t, nagios_log_t, nagios_openshift_plugin_tmp_t, nagios_system_plugin_tmp_t, nagios_tmp_t, named_log_t, named_tmp_t, net_conf_t, netutils_tmp_t, neutron_log_t, neutron_tmp_t, nova_ajax_tmp_t, nova_api_tmp_t, nova_cert_tmp_t, nova_compute_tmp_t, nova_console_tmp_t, nova_direct_tmp_t, nova_log_t, nova_network_tmp_t, nova_objectstore_tmp_t, nova_scheduler_tmp_t, nova_vncproxy_tmp_t, nova_volume_tmp_t, nscd_log_t, ntop_tmp_t, ntpd_log_t, ntpd_tmp_t, numad_var_log_t, nx_server_tmp_t, oddjob_mkhomedir_exec_t, openshift_cgroup_read_tmp_t, openshift_cron_tmp_t, openshift_initrc_tmp_t, openshift_log_t, openshift_tmp_t, opensm_log_t, openvpn_status_t, openvpn_tmp_t, openvpn_var_log_t, openvswitch_log_t, openvswitch_tmp_t, openwsman_log_t, osad_log_t, pam_timestamp_exec_t, pam_timestamp_tmp_t, passenger_log_t, passenger_tmp_t, passwd_file_t, pcp_log_t, pcp_tmp_t, pcscd_var_run_t, pegasus_openlmi_storage_tmp_t, pegasus_tmp_t, piranha_log_t, piranha_web_tmp_t, pkcsslotd_tmp_t, pki_ra_log_t, pki_tomcat_log_t, pki_tomcat_tmp_t, pki_tps_log_t, plymouthd_var_log_t, podsleuth_tmp_t, policykit_tmp_t, polipo_log_t, portmap_tmp_t, postfix_bounce_tmp_t, postfix_cleanup_tmp_t, postfix_local_tmp_t, postfix_map_tmp_t, postfix_pickup_tmp_t, postfix_pipe_tmp_t, postfix_qmgr_tmp_t, postfix_smtp_tmp_t, postfix_smtpd_tmp_t, postfix_virtual_tmp_t, postgresql_log_t, postgresql_tmp_t, pppd_log_t, pppd_tmp_t, pptp_log_t, prelink_exec_t, prelink_log_t, prelink_tmp_t, prelude_lml_tmp_t, prelude_log_t, privoxy_log_t, proc_t, procmail_log_t, procmail_tmp_t, psad_tmp_t, psad_var_log_t, public_content_rw_t, public_content_t, puppet_log_t, puppet_tmp_t, puppetmaster_tmp_t, pyicqt_log_t, qdiskd_var_log_t, qpidd_tmp_t, rabbitmq_var_log_t, racoon_tmp_t, radiusd_log_t, realmd_tmp_t, redis_log_t, rhev_agentd_log_t, rhev_agentd_tmp_t, rhsmcertd_log_t, ricci_modcluster_var_log_t, ricci_tmp_t, ricci_var_log_t, rkhunter_var_lib_t, rlogind_tmp_t, rpm_log_t, rpm_script_tmp_t, rpm_tmp_t, rsync_log_t, rsync_tmp_t, rtas_errd_log_t, rtas_errd_tmp_t, samba_etc_t, samba_log_t, samba_net_tmp_t, samba_var_t, sanlock_log_t, sblim_tmp_t, secadm_sudo_tmp_t, sectool_tmp_t, sectool_var_log_t, security_t, selinux_config_t, selinux_munin_plugin_tmp_t, semanage_tmp_t, sendmail_log_t, sendmail_tmp_t, sensord_log_t, services_munin_plugin_tmp_t, session_dbusd_tmp_t, setroubleshoot_var_log_t, sge_tmp_t, shell_exec_t, shorewall_log_t, shorewall_tmp_t, slapd_log_t, slapd_tmp_t, slpd_log_t, smbd_tmp_t, smoltclient_tmp_t, smsd_log_t, smsd_tmp_t, snapperd_log_t, snmpd_log_t, snort_log_t, snort_tmp_t, sosreport_tmp_t, soundd_tmp_t, spamc_tmp_t, spamd_log_t, spamd_tmp_t, speech-dispatcher_log_t, speech-dispatcher_tmp_t, squid_log_t, squid_tmp_t, squirrelmail_spool_t, src_t, ssh_agent_tmp_t, sssd_public_t, sssd_var_lib_t, sssd_var_log_t, staff_sudo_tmp_t, stapserver_log_t, stapserver_tmp_t, stunnel_tmp_t, svirt_tmp_t, svnserve_tmp_t, swat_tmp_t, sysadm_passwd_tmp_t, sysadm_sudo_tmp_t, sysfs_t, syslogd_tmp_t, sysstat_log_t, system_conf_t, system_cronjob_tmp_t, system_db_t, system_dbusd_tmp_t, system_dbusd_var_lib_t, system_mail_tmp_t, system_munin_plugin_tmp_t, systemd_logind_sessions_t, tcpd_tmp_t, telepathy_gabble_tmp_t, telepathy_idle_tmp_t, telepathy_logger_tmp_t, telepathy_mission_control_tmp_t, telepathy_msn_tmp_t, telepathy_salut_tmp_t, telepathy_sofiasip_tmp_t, telepathy_stream_engine_tmp_t, telepathy_sunshine_tmp_t, telnetd_tmp_t, tetex_data_t, textrel_shlib_t, tgtd_tmp_t, thin_aeolus_configserver_log_t, thin_log_t, thumb_tmp_t, tmp_t, tomcat_log_t, tomcat_tmp_t, tor_var_log_t, tuned_log_t, tuned_tmp_t, tvtime_tmp_t, udev_tmp_t, udev_var_run_t, ulogd_var_log_t, uml_tmp_t, unconfined_munin_plugin_tmp_t, update_modules_tmp_t, updpwd_exec_t, user_cron_spool_t, user_fonts_t, user_home_t, user_mail_tmp_t, user_tmp_t, usr_t, uucpd_log_t, uucpd_tmp_t, var_auth_t, var_log_t, var_spool_t, varnishd_tmp_t, varnishlog_log_t, vdagent_log_t, virt_log_t, virt_qemu_ga_log_t, virt_qemu_ga_tmp_t, virt_tmp_t, vmtools_tmp_t, vmware_host_tmp_t, vmware_log_t, vmware_tmp_t, vpnc_tmp_t, watchdog_log_t, webadm_tmp_t, webalizer_tmp_t, winbind_log_t, wireshark_tmp_t, wtmp_t, xauth_tmp_t, xdm_log_t, xdm_tmp_t, xend_tmp_t, xend_var_log_t, xenstored_tmp_t, xenstored_var_log_t, xferlog_t, xserver_log_t, ypbind_tmp_t, ypserv_tmp_t, zabbix_log_t, zabbix_tmp_t, zarafa_deliver_log_t, zarafa_deliver_tmp_t, zarafa_gateway_log_t, zarafa_ical_log_t, zarafa_indexer_log_t, zarafa_indexer_tmp_t, zarafa_monitor_log_t, zarafa_server_log_t, zarafa_server_tmp_t, zarafa_spooler_log_t, zarafa_var_lib_t, zebra_log_t, zebra_tmp_t, zoneminder_log_t.
Then execute:
restorecon -v '$FIX_TARGET_PATH'        ##解决方法


*****  Plugin catchall (7.64 confidence) suggests   **************************

If you believe that vsftpd should be allowed getattr access on the  file by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# grep vsftpd /var/log/audit/audit.log | audit2allow -M mypol
# semodule -i mypol.pp


[root@ftpserver pub]# man restorecon
--------------------------------------------------
       -R, -r change files and directories file  labels  recursively  (descend
              directories).
              Note:  restorecon  reports  warnings  on  paths  without default
              labels only if called non-recursively or in verbose mode.

       -v     show changes in file labels, if type or role  are  going  to  be
              changed.
--------------------------------------------------
##"-R"表示递归;"-v"显示改变前后的信息
[root@ftpserver pub]# restorecon -v *
restorecon reset /var/ftp/pub/westos context unconfined_u:object_r:mnt_t:s0->unconfined_u:object_r:public_content_t:s0
[root@ftpserver pub]# ls -Z
-rw-r--r--. root root system_u:object_r:public_content_t:s0 hello
-rw-------. ftp  ftp  system_u:object_r:public_content_t:s0 passwd
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 westos

[root@client ~]# lftp 172.25.50.200
lftp 172.25.50.200:~> cd pub/
cd ok, cwd=/pub
lftp 172.25.50.200:/pub> ls
-rw-r--r--    1 0        0               0 Nov 22 06:52 hello
-rw-------    1 14       50           2005 Nov 22 06:52 passwd
-rw-r--r--    1 0        0               0 Nov 22 07:10 westos
lftp 172.25.50.200:/pub> exit

[root@ftpserver pub]# rm -fr *
[root@ftpserver pub]# cd /mnt
[root@ftpserver mnt]# touch file
[root@ftpserver mnt]# mv file /var/ftp/pub/
[root@ftpserver mnt]# cd /var/ftp/pub/
[root@ftpserver pub]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:mnt_t:s0   file
[root@ftpserver pub]# ls -dZ
drwxrwxr-x. root ftp system_u:object_r:public_content_t:s0 .
[root@ftpserver pub]# chcon -t public_content_t /var/ftp/pub/ -R
[root@ftpserver pub]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 file

[root@client ~]# lftp 172.25.50.200
lftp 172.25.50.200:~> cd pub/
cd ok, cwd=/pub
lftp 172.25.50.200:/pub> ls
-rw-r--r--    1 0        0               0 Nov 22 08:10 file
lftp 172.25.50.200:/pub> exit

[root@ftpserver pub]# vim /etc/vsftpd/vsftpd.conf
--------------------------------------------------
 13 anon_root=/ftpdir
:wq
--------------------------------------------------
[root@ftpserver pub]# systemctl restart vsftpd

[root@client ~]# lftp 172.25.50.200
lftp 172.25.50.200:~> ls
lftp 172.25.50.200:/> exit

[root@ftpserver pub]# cd /ftpdir/
[root@ftpserver ftpdir]# ls -Z
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 westos1
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 westos2
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 westos3
[root@ftpserver ftpdir]# ls -dZ
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 .
[root@ftpserver ftpdir]# chcon -t public_content_t /ftpdir/ -R

[root@client ~]# lftp 172.25.50.200
lftp 172.25.50.200:~> ls
drwxr-xr-x    3 0        0              41 Nov 22 02:37 westos1
drwxr-xr-x    3 0        0              41 Nov 22 02:40 westos2
drwxr-xr-x    3 0        0              41 Nov 22 02:40 westos3
lftp 172.25.50.200:/> exit

[root@ftpserver ftpdir]# vim /etc/sysconfig/selinux
--------------------------------------------------
  7 SELINUX=disabled
:wq
--------------------------------------------------
[root@ftpserver ftpdir]# reboot
>等待重启
[root@ftpserver ~]# ls -Z /ftpdir/
drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 westos1
drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 westos2
drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 westos3
[root@ftpserver ~]# ls -dZ /ftpdir/
drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 /ftpdir/
[root@ftpserver ftpdir]# vim /etc/sysconfig/selinux
--------------------------------------------------
  7 SELINUX=enforcing
:wq
--------------------------------------------------
[root@ftpserver ftpdir]# reboot
>等待重启
[root@ftpserver ~]# ls -Z /ftpdir/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 westos1
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 westos2
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 westos3
[root@ftpserver ~]# ls -dZ /ftpdir/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /ftpdir/
[root@ftpserver ~]# semanage fcontext --help
--------------------------------------------------
  -a, --add             Add a record of the fcontext object type

  -l, --list            List records of the fcontext object type
--------------------------------------------------
[root@ftpserver ~]# semanage fcontext -l | grep ftpdir
/tftpboot                                          directory          system_u:object_r:tftpdir_t:s0
/tftpboot/.*                                       all files          system_u:object_r:tftpdir_t:s0
/var/lib/tftpboot(/.*)?                            all files          system_u:object_r:tftpdir_rw_t:s0
[root@ftpserver ~]# semanage fcontext -a -t public_content_t '/ftpdir(/.*)?'
[root@ftpserver ~]# semanage fcontext -l | grep ftpdir
/ftpdir(/.*)?                                      all files          system_u:object_r:public_content_t:s0
/tftpboot                                          directory          system_u:object_r:tftpdir_t:s0
/tftpboot/.*                                       all files          system_u:object_r:tftpdir_t:s0
/var/lib/tftpboot(/.*)?                            all files          system_u:object_r:tftpdir_rw_t:s0
[root@ftpserver ~]# man restorecon
--------------------------------------------------
       -F     Force reset of context to match  file_context  for  customizable
              files,  and  the  default file context, changing the user, role,
              range portion as well as the type.
--------------------------------------------------
[root@ftpserver ~]# restorecon -RvvF /ftpdir/
restorecon reset /ftpdir context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /ftpdir/westos1 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /ftpdir/westos1/westosfile1 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /ftpdir/westos1/westos1pub context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /ftpdir/westos1/westos1pub/passwd context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /ftpdir/westos1/westos1pub/inittab context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /ftpdir/westos2 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /ftpdir/westos2/westosfile2 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /ftpdir/westos2/westos2pub context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /ftpdir/westos3 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /ftpdir/westos3/westosfile3 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /ftpdir/westos3/westos3pub context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0

[root@client ~]# lftp 172.25.50.200
lftp 172.25.50.200:~> ls
drwxr-xr-x    3 0        0              41 Nov 22 02:37 westos1
drwxr-xr-x    3 0        0              41 Nov 22 02:40 westos2
drwxr-xr-x    3 0        0              41 Nov 22 02:40 westos3
lftp 172.25.50.200:/> exit
####################

4.如何控制selinux对服务功能的开关
getsebool -a | grep 服务名称
getsebool -a | grep ftp
setsebool -P 功能bool值 on|off        ##"-P"永久开启
setsebool -P ftpd_anon_write on

####################
[root@ftpserver ~]# vim /etc/vsftpd/vsftpd.conf
--------------------------------------------------
13 #anon_root=/ftpdir
:wq
--------------------------------------------------
[root@ftpserver ~]# systemctl restart vsftpd

[root@client ~]# lftp 172.25.50.200
lftp 172.25.50.200:~> ls
drwxrwxr-x    2 0        50             17 Nov 22 08:10 pub
lftp 172.25.50.200:/> cd pub/
lftp 172.25.50.200:/pub> ls
-rw-r--r--    1 0        0               0 Nov 22 08:10 file
lftp 172.25.50.200:/pub> put /etc/passwd
put: Access failed: 553 Could not create file. (passwd)
lftp 172.25.50.200:/pub> exit

[root@ftpserver ~]# chcon -t public_content_rw_t /var/ftp/pub

[root@client ~]# lftp 172.25.50.200
lftp 172.25.50.200:~> cd pub/
cd ok, cwd=/pub
lftp 172.25.50.200:/pub> put /etc/passwd
put: Access failed: 553 Could not create file. (passwd)
lftp 172.25.50.200:/pub> exit

[root@ftpserver ~]# setenforce 0
[root@ftpserver ~]# getenforce
Permissive

[root@client ~]# lftp 172.25.50.200
lftp 172.25.50.200:~> cd pub/
cd ok, cwd=/pub
lftp 172.25.50.200:/pub> put /etc/passwd
2005 bytes transferred                                           
lftp 172.25.50.200:/pub> ls
-rw-r--r--    1 0        0               0 Nov 22 08:10 file
-rw-------    1 14       50           2005 Nov 22 09:37 passwd
lftp 172.25.50.200:/pub> exit

[root@ftpserver ~]# setenforce 1
[root@ftpserver ~]# getenforce
Enforcing
[root@ftpserver ~]# man getsebool
--------------------------------------------------
       -a     Show all SELinux booleans.
--------------------------------------------------
[root@ftpserver ~]# getsebool -a | grep ftp
ftp_home_dir --> off
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off            ##允许ftpd访问数据库
ftpd_full_access --> off        ##开放ftpd全部功能,不受selinux影响
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off
[root@ftpserver ~]# man setsebool
--------------------------------------------------
       Without  the  -P option, only the current boolean value is affected;
       the boot-time default settings are not changed.

       If the -P option is given, all pending values  are  written  to  the
       policy file on disk. So they will be persistent across reboots.
--------------------------------------------------
[root@ftpserver ~]# setsebool -P ftpd_anon_write on
[root@ftpserver ~]# getsebool -a | grep ftp
ftp_home_dir --> off
ftpd_anon_write --> on
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off

[root@client ~]# lftp 172.25.50.200
lftp 172.25.50.200:~> cd pub/
cd ok, cwd=/pub
lftp 172.25.50.200:/pub> put /etc/group
850 bytes transferred
lftp 172.25.50.200:/pub> ls
-rw-r--r--    1 0        0               0 Nov 22 08:10 file
-rw-------    1 14       50            850 Nov 22 09:47 group
-rw-------    1 14       50           2005 Nov 22 09:37 passwd
lftp 172.25.50.200:/pub> exit
####################


#####################
#####  系统恢复    #####
#####################

####################系统启动流程####################
    通电
     ||
bios(主板上的只读存储中,basic input or output system)
作用,硬件检测,激活硬件
    ||
grub系统引导(grub引导分为两个阶段)
1)阶段1 mbr(主引导记录)主引导记录在硬盘上的0磁道,一扇区,446个字节
*)dd if=/dev/zero of=/dev/vda bs=446 count=1 可以清空mbr
*)进入到挽救模式,执行chroot /mnt/sysimage切换到真实根环境,
   并执行grub2-install /dev/vda

####################
>开始实验前,先把selinux关闭
[root@localhost ~]# vim /etc/sysconfig/selinux
--------------------------------------------------
  7 SELINUX=disabled
:wq
--------------------------------------------------
>开始实验
[root@localhost ~]# fdisk -l

Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@localhost ~]# dd if=/dev/zero of=/dev/vda bs=446 count=1
1+0 records in
1+0 records out
446 bytes (446 B) copied, 0.000172056 s, 2.6 MB/s
[root@localhost ~]# reboot
##注意:必须是446字节,多一点都不行。
>此时mbr已被破坏,一旦重启就启不起来了
>使用以下命令恢复:
[root@localhost ~]# grub2-install /dev/vda
Installing for i386-pc platform.
Installation finished. No error reported.
>如果没有使用以上命令,直接重启
[root@localhost Desktop]# reboot
>发现卡在"Booting from Hard Disk..."不往下走了
>之前总结过"使用pxe进行系统恢复",这里使用光盘来恢复,大同小异
=====使用光盘进行系统恢复=====
>Force Off虚拟机
>点击小灯泡
/Add Hardware-->Storage-->Select managed or other existing storage-->Browse[选中光盘/Open]-->Device type:CDROM device-->Finish-->Yes
/Boot Options-->打钩"IDE CDROM 1",并移动到最上面-->Apply
>开启虚拟机
Troubleshooting-->Rescue a Red Hat Enterprise Linux system
-->Continue
注意提示:
If you would like to make your system the root environment, run the command:
chroot /mnt/sysimage
-->OK
-->OK
sh-4.2# df                ##"/dev/vda1"挂载到了"/mnt/sysimage"目录下
sh-4.2# chroot /mnt/sysimage
bash-4.2# df                ##"/dev/vda1"挂载到了"/"根目录下
bash-4.2# grub2-install /dev/vda
Installing for i386-pc platform.
Installation finished. No error reported.
bash-4.2# exit
exit
sh-4.2# exit
系统重启
>Force Off虚拟机
>点击小灯泡
/Boot Options-->将"IDE CDROM 1"移动到后面-->Apply
>开启虚拟机
系统恢复正常!!!
####################

2)阶段2 grub文件引导阶段
   找到/boot分区
   读取/boot/grub2/grub.cfg
   文件丢失,grub2-mkconfig >/boot/grub2/grub.cfg

####################
[root@localhost ~]# cd /boot/grub2/
[root@localhost grub2]# ls
device.map  fonts  grub.cfg  grubenv  i386-pc  locale  themes
[root@localhost grub2]# rm -fr grub.cfg
>使用以下命令恢复:
[root@localhost ~]# grub2-mkconfig > /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-123.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-123.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-946cb0e817ea4adb916183df8c4fc817
Found initrd image: /boot/initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img
done
>如果没有使用以上命令,直接重启
[root@localhost grub2]# reboot
>重启后,系统进入如下命令行界面
grub> _
>查看当前可用分区
grub> ls
(hd0) (hd0,msdos1) (hd1)        ##只有一个分区可用,就是"hd0,msdos1"
>解决方法
grub> set root='hd0,msdos1'        ##第一个硬盘,第一个分区,即boot分区
grub> linux16 /boot/vmlinuz-3.10.0-123.el7.x86_64 ro root=/dev/vda1
grub> initrd16 /boot/initramfs-3.10.0-123.el7.x86_64.img
grub> boot                ##注意是boot,不是reboot
注意:以上命令均可以使用Tab键补齐
[root@localhost ~]# cd /boot/grub2
[root@localhost grub2]# ls
device.map  fonts  grubenv  i386-pc  locale  themes
[root@localhost grub2]# grub2-mkconfig > /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-123.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-123.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-946cb0e817ea4adb916183df8c4fc817
Found initrd image: /boot/initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img
done
[root@localhost grub2]# vim grub.cfg
--------------------------------------------------
menuentry 'Red Hat Enterprise Linux Server, with Linux 3.10.0-123.el7.x86_64' --class red --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-123.el7.x86_64-advanced-9bf6b9f7-92ad-441b-848e-0257cbb883d1' {
        load_video
        set gfxpayload=keep
        insmod gzio
        insmod part_msdos
        insmod xfs
        set root='hd0,msdos1'                                ##关键行1
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint='hd0,msdos1'  9bf6b9f7-92ad-441b-848e-0257cbb883d1
        else
          search --no-floppy --fs-uuid --set=root 9bf6b9f7-92ad-441b-848e-0257cbb883d1
        fi
        linux16 /boot/vmlinuz-3.10.0-123.el7.x86_64 root=UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 ro vconsole.keymap=us console=tty0 crashkernel=auto  vconsole.font=latarcyrheb-sun16     ##关键行2
        initrd16 /boot/initramfs-3.10.0-123.el7.x86_64.img                ##关键行3
}
--------------------------------------------------
/关键行1,2,3其实就是在"grub>"下输入的内容

=====如果安装的是双系统(xp + linux),想要打开xp系统=====
grub> set root='hd0,msdos1'        ##xp系统的C盘
grub> chainloader +1            ##windows的强制引导
grub> boot

chainloader    ##表示引导方式
'+1'        ##表示grub读入分区的第一个扇区的引导记录

以上方式,并不是所有windows都可用
xp,win7可以用
win8,win10不要用

vim grub.cfg
--------------------------------------------------
menuentry 'windows 7' {
        set root='hd0,msdos1'
        chainloader +1
}
--------------------------------------------------
/windows的引导
####################

       ||
启动内核,只读挂载/设备
检测设备
对设备驱动进行初始化
进入系统初始化阶段

内核丢失,从新安装内核安装包就可以解决
rpm -ivh kernel-xxxxx.rpm --force

####################
[root@localhost ~]# cd /boot
[root@localhost boot]# ls
config-3.10.0-123.el7.x86_64
grub
grub2
initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img
initramfs-3.10.0-123.el7.x86_64.img
initrd-plymouth.img
symvers-3.10.0-123.el7.x86_64.gz
System.map-3.10.0-123.el7.x86_64
vmlinuz-0-rescue-946cb0e817ea4adb916183df8c4fc817
vmlinuz-3.10.0-123.el7.x86_64
[root@localhost boot]# rm -fr vmlinuz-3.10.0-123.el7.x86_64
[root@localhost boot]# reboot
##必须reboot,如果直接Force Off,系统正常启动。文件"vmlinuz-3.10.0-123.el7.x86_64"自动恢复
>重启后,出现报错
error: file '/boot/vmlinuz-3.10.0-123.el7.x86_64' not found.
error: you need to load the kernel first.

Press any key to continue...

  Failed to boot both default and fallback entries.

Press any key to continue...
>使用光盘进入挽救模式
sh-4.2# chroot /mnt/sysimage
bash-4.2# mount /dev/cdrom /opt
mount: /dev/sr0 is write-protected, mounting read-only
bash-4.2# cd /opt/Packages
bash-4.2# cp kernel-3.10.0-123.el7.x86_64.rpm /mnt
bash-4.2# cd /mnt
bash-4.2# ls
kernel-3.10.0-123.el7.x86_64.rpm
--------------------------------------------------
方法1:
bash-4.2# rpm2cpio kernel-3.10.0-123.el7.x86_64.rpm | cpio -id
219282 blocks
bash-4.2# ls
boot  etc  kernel-3.10.0-123.el7.x86_64.rpm  lib
bash-4.2# cd boot/
bash-4.2# ls
config-3.10.0-123.el7.x86_64      System.map-3.10.0-123.el7.x86_64
symvers-3.10.0-123.el7.x86_64.gz  vmlinuz-3.10.0-123.el7.x86_64
bash-4.2# cp vmlinuz-3.10.0-123.el7.x86_64 /boot
方法2:
bash-4.2# rpm -ivh kernel-3.10.0-123.el7.x86_64.rpm --force
warning: kernel-3.10.0-123.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing...                         ################################# [100%])
Updating / installing...
   1:kernel-3.10.0-123.el7           ################################# [100%])
##安装"kernel"过程中不要乱动,千万不要使用"ctrl+c",可能会出现问题
>重新安装会产生一个后遗症,系统重启后会多出一个标题,如下所示:
Red Hat Enterprise Linux Server (3.10.0-123.el7.x86_64) 7.0 (Maipo)    ##新增的标题
Red Hat Enterprise Linux Server, with Linux 3.10.0-123.el7.x86_64
Red Hat Enterprise Linux Server, with Linux 0-rescue-946cb0e817ea4adb916183df8c4fc817
##前两条都是正常启动,效果完全一样
>解决方法:
恢复完成后,进入系统
vim /boot/grub2/grub.cfg
将多余的内容删除
--------------------------------------------------
bash-4.2# exit
exit
sh-4.2# exit
系统重启
>Force Off虚拟机
>点击小灯泡
/Boot Options-->将"IDE CDROM 1"移动到后面-->Apply
>开启虚拟机
>如果之前没有关闭selinux,这里会启动的比较慢。因为要等待一个100%的进度,然后再次重启
系统恢复正常!!!

=====如果使用pxe的方法进行恢复=====
就不能在本地下载软件,需要登陆远端ftp服务器下载
可以使用以下命令,配一个临时使用的地址
ifconfig eth0 x.x.x.x netmask x.x.x.x
然后使用lftp命令登陆远端ftp服务器下载

=====李哥上课出现的意外情况=====
李哥开启虚拟机出现以下报错:
Error: viewer connection to hypervisor host got refused or disconnected!
解决方法:重启服务
systemctl restart libvirtd.service
####################

    ||
系统初始化阶段
系统初始化阶段加载initrd镜像
开启初始化进程systemd
开始selinux
加载内核参数
初始化系统时钟,键盘,主机名称
重新读写挂载/设备
激活raid,lvm
激活配额
启动multi-user.target.wants中的所有服务
开启虚拟控制台
启动图形

initramfs-`uname -r`.img丢失用:
mkinitrd initramfs-`uname -r`.img `uname -r`

####################
[root@localhost ~]# cd /boot
[root@localhost boot]# ls
config-3.10.0-123.el7.x86_64
grub
grub2
initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img
initramfs-3.10.0-123.el7.x86_64.img
initrd-plymouth.img
symvers-3.10.0-123.el7.x86_64.gz
System.map-3.10.0-123.el7.x86_64
vmlinuz-0-rescue-946cb0e817ea4adb916183df8c4fc817
vmlinuz-3.10.0-123.el7.x86_64
[root@localhost boot]# rm -fr initramfs-3.10.0-123.el7.x86_64.img
[root@localhost boot]# reboot
>重启后,出现报错
error: file '/boot/initramfs-3.10.0-123.el7.x86_64.img' not found.

Press any key to continue...
>使用光盘进入挽救模式
sh-4.2# chroot /mnt/sysimage
bash-4.2# cd /boot
bash-4.2# mkinitrd /boot/initramfs-$(uname -r).img $(uname -r)
cat: write error: Broken pipe
bash-4.2# exit
exit
sh-4.2# exit
系统重启
>Force Off虚拟机
>点击小灯泡
/Boot Options-->将"IDE CDROM 1"移动到后面-->Apply
>开启虚拟机
>如果之前没有关闭selinux,这里会启动的比较慢。因为要等待一个100%的进度,然后再次重启
系统恢复正常!!!

man mkinitrd
--------------------------------------------------
       mkinitrd [OPTION...] [<initrd-image>] <kernel-version>
--------------------------------------------------
####################

linuz        ##内核
initrd         ##系统初始化镜像

####################删除目录/boot,然后reboot####################
修复方法:
进入挽救模式
rpm -ivh kernel-3.10.0-123.el7.x86_64.rpm --force
grub2-install /dev/vda
grub2-mkconfig > /boot/grub2/grub.cfg

####################
[root@localhost ~]# rm -fr /boot
[root@localhost ~]# reboot
>重启后,出现报错并且进入命令行界面
error: file '/boot/grub2/i386-pc/normal.mod' not found.
Entering rescue mode...
grub reseue> _
>使用光盘进入挽救模式
sh-4.2# chroot /mnt/sysimage
bash-4.2# cd /boot
bash: cd: /boot: No such file or directory
bash-4.2# cd /mnt
bash-4.2# ls
boot  etc  kernel-3.10.0-123.el7.x86_64.rpm  lib    ##之前复制过来的kernel,并且拆解过
bash-4.2# rpm -ivh kernel-3.10.0-123.el7.x86_64.rpm --force
warning: kernel-3.10.0-123.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing...                         ################################# [100%])
Updating / installing...
   1:kernel-3.10.0-123.el7           ################################# [100%])
##安装"kernel"过程中不要乱动,千万不要使用"ctrl+c",可能会出现问题
bash-4.2# cd /boot
bash-4.2# ls
config-3.10.0-327.el7.x86_64
initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img
initramfs-3.10.0-327.el7.x86_64.img
symvers-3.10.0-327.el7.x86_64.gz
System.map-3.10.0-327.el7.x86_64
vmlinuz-0-rescue-946cb0e817ea4adb916183df8c4fc817
vmlinuz-3.10.0-327.el7.x86_64
##缺少目录grub2/
bash-4.2# grub2-install /dev/vda
Installing for i386-pc platform.
Installation finished. No error reported.
bash-4.2# ls
config-3.10.0-327.el7.x86_64
grub2
initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img
initramfs-3.10.0-327.el7.x86_64.img
symvers-3.10.0-327.el7.x86_64.gz
System.map-3.10.0-327.el7.x86_64
vmlinuz-0-rescue-946cb0e817ea4adb916183df8c4fc817
vmlinuz-3.10.0-327.el7.x86_64
bash-4.2# cd grub2/
bash-4.2# ls
fonts  grubenv  i386-pc  locale
[root@localhost grub2]# grub2-mkconfig > /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-123.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-123.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-946cb0e817ea4adb916183df8c4fc817
Found initrd image: /boot/initramfs-0-rescue-946cb0e817ea4adb916183df8c4fc817.img
done
bash-4.2# exit
exit
sh-4.2# exit
系统重启
>Force Off虚拟机
>点击小灯泡
/Boot Options-->将"IDE CDROM 1"移动到后面-->Apply
>开启虚拟机
>如果之前没有关闭selinux,这里会启动的比较慢。因为要等待一个100%的进度,然后再次重启
系统恢复正常!!!

=====额外知识点=====
grub2-set-default 0    ##进入第一个标题下的系统
grub2-set-default 1    ##进入第二个标题下的系统
grub2-set-default 2    ##进入第三个标题下的系统