User setup script

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
BigSteve_G

User setup script

Post by BigSteve_G »

A bit of advice please, - what I'm trying to write is a script that a user with no Linux skills can use (a simple, click this, type your name, click ok - sort of thing) that will setup a new user account.

Before testing (later, as I'm at work on a xp machine at the moment) I just woundered if someone with more scripting knowledge then me could have a quick look & let me know if the following looks ok? :)

#!/bin/sh
#
# Simple OEM-type user setup script
#
zenity --info --text="Welcome to user setup."
# Collect user login and full name and make user account
theusername=$(zenity --entry --text "Please enter a username - No Spaces, just letters only please." --entry-text "")
thefullname=$(zenity --entry --text "Please enter your name." --entry-text "")
useradd -c $thefullname -d /home/$theusername $theusername
# Collect and set user password
thepwd=$(zenity --entry --text "Please enter a password you would like to use." --entry-text "")
echo $thepwd | passwd $theusername --stdin
# Optional - make a sudo user
add $theusername sudo
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Habitual

Re: User setup script

Post by Habitual »

can unprivileged users execute "useradd -c"?
BigSteve_G

Re: User setup script

Post by BigSteve_G »

Forgot to mention the script will be ran either with sudo or better still will be called upon pre-login & therefore be ran as root (I know its not a secure way to do it but it will only be ran once then remove its self.)

The idea being so someone can install Mint, put this script in place then pass the computer on. The (2nd) user (with no Linux skills) can then boot & be asked what name & password they would like to use.
Habitual

Re: User setup script

Post by Habitual »

BigSteve_G wrote:Forgot to mention the script will be ran either with sudo or better still will be called upon pre-login & therefore be ran as root...
someone can install Mint
So, let me understand this,
1.) you are giving a "2nd user with no Linux skills" sudo privs? How can you do that if the username is NOT known?
2.) How can you run it "pre-login"? zenity is a GTK(?) app with a gui. Can i t be "run" c-li?

Most distros these days force you to create a user during installation.
Your zenity script doesn't show any indication of a forced logout (gnome-session-save --logout) to return the 2nd user to the login screen.

I love zenity coding (rather yad, it's forked successor) but you're scaring the be-jesus out of me.

Subscribed with interest.

JJ

Edit: I don't doubt the process can be done somehow, but I do, however doubt the logic behind your pseudo-code. :)
BigSteve_G

Re: User setup script

Post by BigSteve_G »

Truth be told this is kind-of my 1st attempt at anything like this - the only other script I've wrote was a basic command line user setup for my own system.

The reason for the above script is explained here http://forums.linuxmint.com/viewtopic.php?f=189&t=98146

Just to clearify;
- The giving the 2nd user thing is optional - just thought it would be handy but I see your point
- By pre-login - maybe my terminology is a little off, I mean at the point that the gui has loaded but the user hasn't entered their name & password
- Scaring the be-jesus out of you ? - :lol: thanks - I think? - but why?

If you can think of a better solution for the other topic (that the link above goes to) any suggestions are very welcome.
In terms of passing veribals (if thats the right term) from zenity boxes to stuff in command lines would it work like how I've done it?
Habitual

Re: User setup script

Post by Habitual »

Steve:

...have to think this one over. It's a doozie :)
I'm no Developer, but I have written my share of code and admin a couple of 100 boxes.

re: be-jesus...
It's never a good idea to let users pick their own passwords.
and a escalated privileged script controlled by the "user".

I have seen "users" and it is not pretty.
Without intending to, they can really mess things up.

to be continued...

Still subscribed with even more interest.

See what you started? :P

JJ
BigSteve_G

Re: User setup script

Post by BigSteve_G »

Just test ran it on my laptop (not my main system) - all ran ok except the passwd part but I think I already have a way around that...

#!/bin/sh
#
# Simple OEM-type user setup script
#
zenity --info --text="Welcome to user setup."

# Collect user login and full name and make user account
theusername=$(zenity --entry --text "Please enter a username - No Spaces, just letters only please." --entry-text "")
thefullname=$(zenity --entry --text "Please enter your name." --entry-text "")

useradd -c $thefullname -d /home/$theusername -m $theusername

# Collect and set user password
thepwd=$(zenity --entry --text "Please enter a password you would like to use." --entry-text "")

echo "#!/bin/sh" > setpwd.sh
echo "passwd $theusername $thepwd" >> setpwd.sh
chmod a+x setpwd.sh
./setpwd.sh

# Optional - make a sudo user
# add $theuser name sudo
BigSteve_G

Re: User setup script

Post by BigSteve_G »

I know I need to delete the setpwd.sh file but.... :oops: I dont know the delete command :oops:
Habitual

Re: User setup script

Post by Habitual »

Code: Select all

rm /path/to/setpwd.sh
LorinRicker

Re: User setup script

Post by LorinRicker »

BigSteve: With all respect, I'm gonna go out on a limb here, right along side Habitual, and just encourage you to rethink this whole approach: 1) Yes, most distros will ask/force you to set up an initial (non-root) user during the installation process... So why would you need to reinvent this particular wheel? 2) There are several more things to consider and accommodate in setting up a new user account than your Zenity script addresses. 3) Getting sudo and root-privs involved for "a user with no Linux skills can use" is really asking for trouble, esp during the installation/post-installation stage -- why would you open such a hole on a brand new, untarnished box?! 4) Setting up new user accounts is typically considered a sys-admin function, not something for users to do themselves... There's too much mischief, innocent/unintentional or otherwise, to invite here!

My advice -- free, so you can certainly ignore it :? -- would be to get yourself one of those nice, thick "Everything about Linux" book/bibles (in Ubuntu flavor, etc) and study it thoroughly before continuing. Such a manual would give you necessary -- and (forgive me) obviously missing -- background and context for setting up and managing your Linux boxes, including things like the rm command, and so much more.

Welcome to the Linux community -- and kudos for your intrepid nature! :) But, IMHO, it looks like you're headed into a blind alley here. Get some Linux sys-admin context under your belt -- You'll find/see that there are much more logical and better ways to accomplish what you're thinking of here... Best of luck!
Habitual

Re: User setup script

Post by Habitual »

Steve:

Have a peek at these:
https://help.ubuntu.com/community/LiveCDCustomization
https://help.ubuntu.com/community/LiveC ... romScratch

The short answer/solution from my limited non-DEV perspective would be
1.) Create the oem-user in a sandbox environment LiveCDCustomization
2.) give oem-user sudo privs in the sandbox environment.
4.) have oem-user automatically login
4.a) startup application for oem-user would have your zenity script.
5.) Remove sudo privs for oem-user
6.) Delete zenity script or otherwise obfuscate the script (echo "" > /path/to/setpwd.sh would suffice)
6.) Disable auto-login for oem-user
7.) reboot.
8.) Pray.

Once you have managed that in a "sandbox" for a customized livecd enviroment, then re-compile the .iso

I probably forgot something really important or layed out the steps kinda funky, but this should server as a basic pseudo-code for your roadmap.
Other more elegant solutions probably exist, like I said I'm no DEV".
It's a huge task.

Good luck.
BigSteve_G

Re: User setup script

Post by BigSteve_G »

-Habitual
rm - well that explains why I was stumped, I was trying rn :oops:

-LorinRicker
The background to this is kind-of explained here - http://forums.linuxmint.com/viewtopic.p ... 58#p560858
Basicaly its for someone (with some Linux knowledge) can instal & configure Mint for someone else but then let them pick their own username & password on 1st boot without being present - it also needs to be easy for person A to implament for very easy for person B to use. - also person A is helping person B not acting as an admin, hence giving person B sudo privlages.

-Habitual
Good point about using a custom LiveDisc & if it was for myself its probably the way I'd do it so I can also customise installation aswell.

I've had a re-think & am thinking instead of creating a new user, simply using a script to let the end-user (person B from above) simply change the name & password after loging in to the inital account setup during installation (avoiding the need to run anything as root)
BigSteve_G

Re: User setup script

Post by BigSteve_G »

Slight change of plan - Im now going with chaging the current users name & password using;

#!/bin/sh
# Display a welcome type message
zenity --info --text="Welcome to user setup."
# Collect user login and full name and make user account
theusername=$(zenity --entry --text "Please enter a username - No Spaces, just letters only please." --entry-text "")
# Change the user name
usermod -l $theusername user
# Collect and set user password
thepwd=$(zenity --entry --text "Please enter a password you would like to use." --entry-text "")
# Change the password
passwd user $thepwd
# Self remove this file
rm ~/Desktop/Setup
Locked

Return to “Scripts & Bash”