PXE多机无人值守安装+批量用户创建并限定登录地点

无人值守系统安装

需求:自动检测安装,支持多台主机

所用工具:dhcp、tftp-server、httpd、syslinux
(注:所用主机IP:192.168.230.164 /24)
实现:


1、  安装和配置所需服务
#挂载光盘
[root@localhost ~]# mount /dev/cdrom /mnt/
mount: block device /dev/sr0 is write-protected, mounting read-only
#注释原有的yum源
[root@localhost ~]# vim /etc/yum.repos.d/CentOS-Base.repo
[base]
…
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
baseurl=file:///var/www/html/OS  #修改光盘挂载点
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-62、  安装PXE所需服务
dhcp:             分配给客户端提供的IP地址;
tftp-server:         tftp服务器端, 提供系统安装所需文件;
xinetd:            tftp服务超级守护进程, 用于唤醒tftp服务;
httpd:            基于http服务提供安装源;
syslinux:         提供pxelinux.0文件, 此文件用于引导系统, 相当于bootloader;
[root@localhost ~]# yum -y install dhcp tftp-server xinetd syslinux httpd

3、  配置dhcp和tftp服务
[root@localhost ~]# vim /etc/dhcp/dhcpd.conf
allow booting;
allow bootp;
ddns-update-style interim;
ignore client-updates;
subnet 192.168.230.0 netmask 255.255.255.0 {
        option subnet-mask      255.255.255.0;
        option domain-name-servers  192.168.230.164;
        range dynamic-bootp 192.168.230.1 192.168.230.50;
        default-lease-time      21600;
        max-lease-time          43200;
        next-server             192.168.230.164;
        filename                "pxelinux.0";
}

[root@localhost ~]# vim /etc/xinetd.d/tftp
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /var/lib/tftpboot
        disable                 = no        #改这里,由yes改为no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}
[root@localhost ~]# service dhcpd configtest  #语法测试
[root@localhost ~]# service dhcpd start    #启动dhcp服务
[root@localhost ~]#chkconfig dhcpd on               #将dhcp设置为开机自启

[root@localhost ~]#service xinetd configtest         #测试语法是否正确
[root@localhost ~]#service xinetd start                  #启动xinetd服务
[root@localhost ~]#chkconfig xinetd on                #将xinetd设置为开机自启动
**######配置防火墙!!!见最下面**
    (如果service命令不管用,可以到/etc/init.d/下直接启动)

4、  配置http服务
[root@localhost ~]mkdir /var/www/html/OS
[root@localhost ~]cp -rf /mnt/* /var/www/html/OS
[root@localhost ~]service  httpd start
[root@localhost ~]chkconfig httpd on


5、  复制kickstart所需文件
[root@localhost ~]mkdir /var/lib/tftpboot/pxelinux.cfg
[root@localhost ~]cd /var/lib/tftpboot
[root@localhost tftpboot]#cp /usr/share/syslinux/pxelinux.0 . 
[root@localhost tftpboot]#cp /mnt/images/pxeboot/{vmlinux,initrd.img} .
[root@localhost tftpboot]#cp /mnt/isolinux/{vesamenu.c32,boot.msg} .
[root@localhost tftpboot]#cp /mnt/isolinux/isolinux.cfg   /pxelinux.cfg/default
[root@localhost ~]# ls /var/lib/tftpboot/
boot.msg  initrd.img  pxelinux.0  pxelinux.cfg  vesamenu.c32  vmlinuz

#修改/var/lib/tftpboot/pxelinux.cfg/default文件,添加ks.cfg文件位置
#修改成最简化
[root@localhost tftpboot]# vim pxelinux.cfg/default
default vesamenu.c32
#prompt 1
timeout 6

display boot.msg

label ks
  kernel vmlinuz
  append initrd=initrd.img ks=http://192.168.230.164/ks.cfg


6、  提供ks.cfg文件(可自己创建,也可用system-config-kickstart生成)记得改URL

#ks.cfg样板(可用自制ISO中的ks.cfg,修改网络安装)
[root@localhost ~]# vi /var/www/html/ks.cfg
#platform=x86, AMD64, 或 Intel EM64T
#version=DEVEL
# Firewall configuration
firewall --disabled
# Install OS instead of upgrade
install
# Use network installation
url --url=http://192.168.230.164/OS    #改这,由cdrom改为url
#repo --name="Centos" --baseurl=http://192.168.230.164 --cost=100
# Root password
rootpw --iscrypted $1$Kldv3XiW$fBgcVMAyKrKeHjPXh8IID/
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use graphical install
graphical
firstboot --disable
# System keyboard
keyboard us
# System language
lang zh_TW
# SELinux configuration
selinux --disabled
# Installation logging level
logging --level=info
# Reboot after installation
reboot
# System timezone
timezone  Asia/Shanghai
# Network information
network  --bootproto=dhcp --device=eth0 --onboot=on
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel
# Disk partitioning information
part /boot --asprimary --fstype="ext4" --size=500
part swap --fstype="swap" --size=2000
part / --asprimary --fstype="ext4" --grow --size=1

%packages
@base
@basic-desktop
@chinese-support
@debugging
@desktop-debugging
@desktop-platform
@directory-client
@fonts
@ftp-server
@general-desktop
@graphical-admin-tools
@input-methods
@internet-applications
@internet-browser
@java-platform
@legacy-x
@network-file-system-client
@office-suite
@print-client
@remote-desktop-clients
@server-platform
@x11

%end
[root@localhost ~]


最后一点!!!一定要配防火墙!!!

[root@linuxprobe ~]# firewall-cmd --permanent --add-port=69/udp
success
[root@linuxprobe ~]# firewall-cmd --reload 
success

[root@linuxprobe ~]# firewall-cmd --permanent --add-service=dhcp
success
[root@linuxprobe ~]# firewall-cmd --reload 
success

[root@linuxprobe ~]# firewall-cmd --permanent --add-service=ftp
success
[root@linuxprobe ~]# firewall-cmd --reload 
success

至此,完成服务器搭建
新建虚拟机测试,虚拟机不用加载镜像,选NAT模式。

————————————————————————————————————————



批量创建用户,并同时禁止本地登录

#!/bin/bash
sed -i '/account/{s/$/\naccount   required   \/lib64\/security\/pam\_access\.so/;:f;n;b f;}'  
/etc/pam.d/login
#添加禁止普通用户登录模块

i=0
#批量创建普通用户
while [ "$i" != "50" ]
do
        i=$(($i+1))
        username="YX"${i}
        echo "$username"
        useradd -m $username
        echo $username | passwd --stdin $username
        sed -i '/account/{s/$/\naccount   required   \/lib64\/security\/pam\_access\.so/;:f;n;b  
f;}' /etc/pam.d/login
        sed -i "i\-:\$username:locol" /etc/security/access.conf#确定禁止登录的普通用户
done
echo "creat 50 users"

**克隆时需要更改的点:
1、 vim /etc/dhcp/dhcpd.conf/
2、 vim /var/lib/tftpboot/pxelinux.cfg/default
3、 vi /var/www/html/ks.cfg**

任务难点:
1、 在配置dhcp服务时,总是开启不了服务,后来是换了镜像才弄好,原因待定。。。
2、 脚本的本地登录。由于自身知识漏洞,没有理解Linux本地登录原理,一个劲的在桌面登录面前瞎折腾,后来黄哥指点,通过黑屏登录才是locallogin.鸟哥继续学习ing。
3、 Sed命令不会用,卡了一天,通过网上询问,和问陈烽搞定。Shell继续学习ing。
4、 引导文件在最后运行脚本文件时一直卡死不动,但系统并没有死,不清楚原因,改变策略,将脚本文件写成开机自启动,并加入运行完后自我删除命令。




#!/bin/bash

expect -c "   
spawn su - root   
expect \"Password:\"   
send \"123456\r\"   
interact  "  
sed -i '/account/{s/$/\naccount   required   \/lib64\/security\/pam\_access\.so/;:f;n;b f;}' /etc/pam.d/login
i=0

while [ "$i" != "50" ]
do
        i=$(($i+1))
        username="YX"${i}
        echo "$username"
        useradd -m $username
        echo $username | passwd --stdin $username
        sed -i '/account/{s/$/\naccount   required   \/lib64\/security\/pam\_access\.so/;:f;n;b f;}' /etc/pam.d/login
        sed -i "i\-:\$username:locol" /etc/security/access.conf
done
echo "creat 50 users"

/bin/rm $0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值