[SOLVED] Scripts after login but before the desktop loads

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
DJNightchild

[SOLVED] Scripts after login but before the desktop loads

Post by DJNightchild »

Hey guys

I need some help with scripts after login but before the desktop loads.

First I'll show the scripts:

Script 1

Code: Select all

#!/bin/bash

HOSTNAME=`hostname`

if [[ $HOSTNAME = "LM17-Cinnamon" ]]
then
    sudo reboot
fi

if [[ $HOSTNAME = "LM17-XFCE" ]]
then
    sudo reboot
fi
Script 2

Code: Select all

#!/bin/bash
#Assign existing hostname to $hostn
hostn=$(cat /etc/hostname)

#Display existing hostname
echo "Existing hostname is $hostn"

#Ask for new hostname $newhost
echo "Enter new hostname: "
read newhost

#change hostname in /etc/hosts & /etc/hostname
sudo sed -i "s/$hostn/$newhost/g" /etc/hosts
sudo sed -i "s/$hostn/$newhost/g" /etc/hostname

#display new hostname
echo "Your new hostname is $newhost"

#Press a key to reboot
read -s -n 1 -p "Press any key to reboot"
sudo reboot
As you can see, these are scripts meant for "after imaging" with Clonezilla.

The situation is as follows:

1. If the hostname of the machine equals: LM17-XFCE or LM17-Cinnamon reboot the system. The script which takes care of this is placed in the folder "/etc/skel/.config/autorun"
2. To avoid this: the users has to login via tty1, since autostart doesn't activate in text mode
3. The second script is located in the /root folder. After activating this script via sudo, the hostname is changed and user can login normally.

This is quite a annoying process to do and should be easier.

What I'm searching for is this solution:

If the user logins and the hostname is still LM17-XFCE of LM17-Cinnamon, the desktop wil load but only shows a empty background (no menu's etc). It does not matter if I have to force a gui-switch (for example IF the hostname hasn't change, the users logs in with a openbox gui).

Script 2 will automatically launch


I hope I gave you guys enough info and you can help me with this problem.


Thanks in advance


P.S. Clonzilla itself has a function to change the hostname of the client. However, this only works with MS-Windows hostnames.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
JohnBobSmith

Re: Scripts after login but before the desktop loads

Post by JohnBobSmith »

I'm note entirely sure what it is you're trying to achieve... Can you elaborate more on the problem itself?
DJNightchild

Re: Scripts after login but before the desktop loads

Post by DJNightchild »

What I'm trying to do is run a script that will disbale the gui interfaceand runs a terminal script the asks you to change the hostname.
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Scripts after login but before the desktop loads

Post by Pilosopong Tasyo »

DJNightchild wrote:What I'm searching for is this solution:

If the user logins and the hostname is still LM17-XFCE of LM17-Cinnamon, the desktop wil load but only shows a empty background (no menu's etc). It does not matter if I have to force a gui-switch (for example IF the hostname hasn't change, the users logs in with a openbox gui).

Script 2 will automatically launch
Try the following. No need for a reboot in your script 1. Once it's known the hostname needs to be changed, just switch to a terminal login screen and advise user to run the script that changes the hostname.

This method uses rc.local, which gets run as root during boot, before the GUI login screen IINM. So, the user doesn't even have to log in graphically. Make the necessary adjustments when required.

Do the following prepwork:

1. Make a backup copy of the /etc/issue file:

Code: Select all

sudo cp /etc/issue /root/issue.orig
2. Replace the current issue file with:

Code: Select all

This machine's hostname needs to be changed.
Log in using your credentials.  Then at the shell prompt, enter:

     sudo /root/change-hostname.sh

\n \l
3. Pseudocode for /root/check-host-name.sh (this replaces your script 1):

Code: Select all

#! /bin/sh

CURRENT_HOST_NAME = get local machine's current hostname

if $CURRENT_HOST_NAME = "LM17-Cinnamon" or "LM17-XFCE"
then  # hostname needs to be changed

  # switch to the first virtual terminal
  chvt 1

else  # hostname has been changed previously; clean up

  # restore the original issue file
  cp /root/issue.orig /etc/issue

  # don't need to run this script every startup anymore 
  # can be removed from rc.local
  sed -i s/'/root/check-host-name.sh'//g /etc/rc.local

fi
4. Code for /root/change-hostname.sh:

Code: Select all

<put your second script here>
5. Open the /etc/rc.local file and add the following line:

Code: Select all

/root/check-host-name.sh
near the end (above the exit command). I'll leave it to you as exercise to change the pseudocode to something more appropriate and/or customize the /etc/issue file. Test / tweak / debug. If all goes well, create a new Clonezilla image and deploy.

Be aware of the following pitfall: You don't have any sanity-checking code for the new hostname. Your system might misbehave if the new hostname does not conform with hostname-naming standards (e.g. a trailing hyphen is not allowed / prohibited characters in the hostname).

Questions / clarifications, don't hesitate to ask here.

Good luck! HTH.
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
DJNightchild

Re: Scripts after login but before the desktop loads

Post by DJNightchild »

Pilosopong Tasyo wrote:
DJNightchild wrote:What I'm searching for is this solution:

If the user logins and the hostname is still LM17-XFCE of LM17-Cinnamon, the desktop wil load but only shows a empty background (no menu's etc). It does not matter if I have to force a gui-switch (for example IF the hostname hasn't change, the users logs in with a openbox gui).

Script 2 will automatically launch
Try the following. No need for a reboot in your script 1. Once it's known the hostname needs to be changed, just switch to a terminal login screen and advise user to run the script that changes the hostname.

This method uses rc.local, which gets run as root during boot, before the GUI login screen IINM. So, the user doesn't even have to log in graphically. Make the necessary adjustments when required.

Do the following prepwork:

1. Make a backup copy of the /etc/issue file:

Code: Select all

sudo cp /etc/issue /root/issue.orig
2. Replace the current issue file with:

Code: Select all

This machine's hostname needs to be changed.
Log in using your credentials.  Then at the shell prompt, enter:

     sudo /root/change-hostname.sh

\n \l
3. Pseudocode for /root/check-host-name.sh (this replaces your script 1):

Code: Select all

#! /bin/sh

CURRENT_HOST_NAME = get local machine's current hostname

if $CURRENT_HOST_NAME = "LM17-Cinnamon" or "LM17-XFCE"
then  # hostname needs to be changed

  # switch to the first virtual terminal
  chvt 1

else  # hostname has been changed previously; clean up

  # restore the original issue file
  cp /root/issue.orig /etc/issue

  # don't need to run this script every startup anymore 
  # can be removed from rc.local
  sed -i s/'/root/check-host-name.sh'//g /etc/rc.local

fi
4. Code for /root/change-hostname.sh:

Code: Select all

<put your second script here>
5. Open the /etc/rc.local file and add the following line:

Code: Select all

/root/check-host-name.sh
near the end (above the exit command). I'll leave it to you as exercise to change the pseudocode to something more appropriate and/or customize the /etc/issue file. Test / tweak / debug. If all goes well, create a new Clonezilla image and deploy.

Be aware of the following pitfall: You don't have any sanity-checking code for the new hostname. Your system might misbehave if the new hostname does not conform with hostname-naming standards (e.g. a trailing hyphen is not allowed / prohibited characters in the hostname).

Questions / clarifications, don't hesitate to ask here.

Good luck! HTH.

I had to edit a few commands in this script and change it to #!/bin/bash to make it work.
However, Linux Mint seems to auto-edit the /etc/issue if I make a change to it.

Do you know how to fix this? Or which program manages this?
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Scripts after login but before the desktop loads

Post by Pilosopong Tasyo »

DJNightchild wrote:...Linux Mint seems to auto-edit the /etc/issue if I make a change to it. Do you know how to fix this? Or which program manages this?
No idea. But doing a casual search in Google for the terms linux mint /etc/issue landed me on this page at GitHub. It looks like the source that overwrites the issue files are found near the end of the Python script (starts line 113).

I'm not 100% sure if mintadjust.py is the main culprit, though (I'm using Ubuntu 12.04 as my main OS right now). Perhaps another member more versed in the changes done by the Mint devs can chime in to verify.
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
DJNightchild

Re: Scripts after login but before the desktop loads

Post by DJNightchild »

Pilosopong Tasyo wrote: No idea. But doing a casual search in Google for the terms linux mint /etc/issue landed me on this page at GitHub. It looks like the source that overwrites the issue files are found near the end of the Python script (starts line 113).

I'm not 100% sure if mintadjust.py is the main culprit, though (I'm using Ubuntu 12.04 as my main OS right now). Perhaps another member more versed in the changes done by the Mint devs can chime in to verify.
In that case I will state this post as solved (since it really is), and I will ask the question regarding the /etc/issue file on a different board since that is a new question itself.

Thanks for all your help :)


EDIT: Here are the scripts I used:

Script 1:

Code: Select all

#!/bin/bash

CURRENT_HOST_NAME=$(cat /etc/hostname)
FILE=/root/image-script

if [[ $CURRENT_HOST_NAME = "LM17-Cinnamon" ]]
then  # host name needs to be changed

  if [[ -f $FILE ]]
  then # image-script will be changed to change-hostname

  mv $FILE /root/change-hostname

  fi

  # switch to the first virtual terminal
  chvt 1

elif [[ $CURRENT_HOST_NAME = "LM17-XFCE" ]]
then  # host name needs to be changed

  if [[ -f $FILE ]]
  then # image-script will be changed to change-hostname

  mv $FILE /root/change-hostname

  fi

  # switch to the first virtual terminal
  chvt 1

fi
Script 2

Code: Select all

#!/bin/bash

#Assign existing hostname to $hostn
hostn=$(cat /etc/hostname)

#Display existing hostname
echo "Existing hostname is $hostn"

#Ask for new hostname $newhost
echo "Enter new hostname: "
read newhost

#remove immutable attribute from the issue file
sudo chattr -i /etc/issue

#change hostname in /etc/hosts & /etc/hostname
sudo sed -i "s/$hostn/$newhost/g" /etc/hosts
sudo sed -i "s/$hostn/$newhost/g" /etc/hostname

#display new hostname
echo "Your new hostname is $newhost"

#Press a key to reboot
sudo read -s -n 1 -p "Press any key to reboot"
sudo mv /root/change-hostname /root/image-script
sudo reboot
Locked

Return to “Scripts & Bash”