LINUX下用户登录脚本

转载自:http://hi.baidu.com/shiqiang_hx/blog/item/aab77006e673217a030881cc.html


查看bash的手册页(man bash),发现以下内容:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists.   After reading that file, it looks for ~/.bash_profile,   ~/.bash_login,   and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.   The --noprofile option may be used when the shell is started to inhibit this behavior.

When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file   exists. This   may   be   inhibited   by using the --norc option.   The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.

当Bash以交互方式被调用或以带--login参数的非交互方式被调用时,它首先查找/etc/profile文件,如果文件存在就读取并执行该文件中的命令,然后依次查找“~/.bash_profile”,“~/.bash_login”和“~/.profile”这三个文件,然后执行第一个存在并且可读取的文件。使用“--noprofile”选项可以使shell在启动时不执行这些文件。

当退出一个登录Shell时,bash会查找“~/.bash_logout”文件,如果存在就读取并执行它。

当启动一个非登录交换式Shell时,bash尝试~/.bashrc文件,如果存在就读取并执行它。使用“--norc”选项可以不执行它。使用“--rcfile 文件”选项将强制bash读取并执行指定的文件来代替“~/.bashrc”。

因此,如果需要在用户一登录时就执行某些程序(如zhcon)就可以加到以上文件中。



在CentOS 7上创建一个脚本以便使用SSH登录新用户是相对简单的。下面是一个简单的步骤: 1. 以root用户身份登录到CentOS 7系统。 2. 打开一个文本编辑器,并创建一个名为`create_user_ssh_login.sh`的新文件。 3. 在文件中输入以下内容: ```bash #!/bin/bash # 设置新用户的用户名 USERNAME="newuser" # 创建新用户 useradd $USERNAME # 设置新用户的密码 PASSWORD="newpassword" echo $PASSWORD | passwd --stdin $USERNAME # 为新用户创建.ssh目录并设置权限 mkdir /home/$USERNAME/.ssh chown $USERNAME:$USERNAME /home/$USERNAME/.ssh chmod 700 /home/$USERNAME/.ssh # 将公钥复制到新用户的authorized_keys文件中 cat /path/to/public_key.pub >> /home/$USERNAME/.ssh/authorized_keys chown $USERNAME:$USERNAME /home/$USERNAME/.ssh/authorized_keys chmod 600 /home/$USERNAME/.ssh/authorized_keys # 允许新用户通过SSH登录 sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config systemctl restart sshd ``` 4. 保存并关闭文件。 5. 使用以下命令将脚本文件设置为可执行: ```bash chmod +x create_user_ssh_login.sh ``` 6. 运行脚本以创建用户和配置SSH登录: ```bash ./create_user_ssh_login.sh ``` 7. 根据需要替换`USERNAME`和`PASSWORD`变量的值,并将公钥的路径替换为你的公钥路径。 注意:脚本中的默认用户名为`newuser`,默认密码为`newpassword`,请根据实际需要进行修改和替换。 这个脚本将创建一个新用户,指定的用户名和密码,并为该用户创建.ssh目录,将公钥添加到authorized_keys文件中,然后禁用密码身份验证,并重新启动SSH服务器以使更改生效。这样,新用户就可以使用SSH登录到系统。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值