echo "Current date and time: $(date)" >> /etc/motd
The above code displays the current time that was added to /etc/motd.
I want the user to see the actual current time and date after the login. Is there a way to do this?
Rather than a one-shot time how about if the user sees the current time at each command line prompt?
Try this first from the command line prompt:
PS1="\# \w$bk"
The prompt will look like: 11:41 AM ~
More information for the user might be to add the username and local machine name with the time. With the user "DrPete" on a local machine named "LittleSorrel" the prompt:
PS1="\u on \h \# \w$bk "
Looks like: DrPete on LittleSorrel 10:34 AM ~
To reset the prompt to default to "# ":
PS1="# "
To make the prompt permanently available, add the prompt construction of your choice, i.e.,
export PS1="\# \w$bk "
To ~/.profile.
From this guide
https://ownyourbits.com/2017/04/05/customize-your-motd-login-message-in-debian-and-ubuntu/
sudo mkdir -p /etc/update-motd.d
sudo cat > /etc/update-motd.d/10date <<'EOF'
#!/bin/sh
echo
echo echo "Current date and time: $(date)"
EOF
sudo chmod a+x /etc/update-motd.d/*
It will show up in the next login.
For Centos 7 I suggest using dynamic MOTD with my updated PAM package to have update-motd.d feature from Ubuntu.
Install with:
yum install https://github.com/gdubicki/centos-pam-with-update-motd/releases/download/1.1.8-1022.3/pam-1.1.8-1022.el7.x86_64.rpm
Use with:
Delete the default static /etc/motd.
Make SSHD not show the static MOTD with lines PrintMotd no, Banner none, UsePAM yes (and optionally PrintLastLog no) in your /etc/ssh/sshd_config & reload sshd service.
Add this line to your /etc/pam.d/sshd: session optional pam_motd.so motd=/run/motd.dynamic.
Add your script to /etc/update-motd.d:
#!/bin/bash
echo "Current date and time: $(date)"
...as /etc/update-motd.d/10time file for example, with permissions to be read and executed by root.
Please see https://github.com/gdubicki/centos-pam-with-update-motd for the most up to date info.
Related
When I´m running the next bash it work´s in the first time, but if I try to run again it dosent modify the file
oldHS=$HOSTNAME
echo -n "New hostname: "
read NHOST
sed -i "s/$oldHS/$NHOST/g" /etc/hosts
sudo hostnamectl set-hostname $NHOST
read -p "In the following file please validate the change. If it is correct, press control + x otherwise make the change and press control + o. Press enter to continue"
sudo nano /etc/hosts
hostnamectl
read -p "Presione enter para finalizar"
The hostnamectl set-hostname $NHOST always make the change but the script only the first time make the change in the /etc/hosts file
As Shawn said in the comments, the $HOSTNAME doesn`t reflect the chanague until I start a new shell. I made a change in the oldHS variable for this and it works
oldHS=$(hostname)
Im currently logged in as admin and I want to edit the /etc/hosts file which required root access.
I'm not able to make the changes. The script gets executed sucessfully but the changes arent made.
My Script - Runs Sucessfully when executed from terminal
sudo -s
echo "127.0.0.1" >> /etc/hosts
su admin
sudo -s - switches to root without password when executed from terminal
su admin - switches back to admin user when run on terminal
My /etc/hosts file remains empty after running the script
There is no need to actually switch your user within the script.
Also, you can't echo something as root like that because the redirect (>>) is executed by the shell.
A possible workaround is using tee:
echo "127.0.0.1" | sudo tee -a /etc/hosts
Further explanation:
tee basically takes the data from the standard input and writes it either to the standard output, or to a file. For more information see the commands manual ($ man tee)
I have the following bash script to restart the network manager in Debian. The script works as is it should, but not as I would like it to. When the script asks for the sudo password I am able to pass it along using echo, but it displays the password in terminal while the script executes, making it less asthetically pleasing than I would like. Is there anyway to have the script enter the password, but not display the password text while the script calls for the sudo password?
I have tried as many suggestions on Stack Overflow as i could find, well as Stack Exchange before submitting this question.
Script is as follows:
#!/bin/bash
clear
echo "Restarting service Network Manager"
echo""
sleep 1
echo -e "\033[0;31m......................................\033[0m"
echo -e "\033[0;31m......................................\033[0m"
sleep 1
echo""
sudo service network-manager restart
sleep 2
echo <Password>
sleep 2
echo "Service Network Manager Restarted"
sleep 1
echo ""
echo "Relinquishing control of terminal to user..."
sleep 7
clear
Remove the echo <Password> line? I am pretty sure it does nothing other than display the password, as sudo apparently (through an appropriate entry in /etc/sudoers) works without you having to give a password. (What you write to terminal with echo does not get passed to any other process.)
Generally speaking, you can use sudo -S to make sudo expect the password on stdin. But also generally speaking, if you have to hardcode a password in a script, you're doing it wrong in some way.
Is there anyway to have the script enter the password
Putting password in script is not a good idea. First, from security point of view, password may be recovered from script from anyone with access to script. Second, from maintenance view, once you change your password, scripts suddenly stop working and you have to update them all.
Fortunately, as you are already using sudo there is better solution. You can configure sudo to allow running certain command without password, by using NOPASSWD rule in /etc/sudoers.
myuser ALL=(ALL) NOPASSWD: service network-manager restart
See:
How do I run specific sudo commands without a password?
How to run a specific program as root without a password prompt?
Warning: Always edit /etc/sudoers with visudo, never directly. It prevents you from breaking /etc/sudoers. Once you break your /etc/sudoers, you won't be able to use sudo, including using sudo to fix /etc/sudoers.
try this /bin/echo -e "password\n" | sudo apt-get update
or see this Use sudo with password as parameter
I gave everyone in my team a copy of a centos 7 virtual machine. The time can get out of sync. I found that I can manually update the time by using the command below:
[no_sudo#rolling ~]$ ntpdate pool.ntp.org
26 Apr 18:10:11 ntpdate[25928]: bind() fails: Permission denied
[no_sudo#rolling ~]$
However I can only update it as sudo. One of the commands I made for the team runs some automated testing, and uses the date.time as a name.
How can I ether automate the updating of time on the virtual machine, or alter the permissions of that service, so anyone can run it regardless or permissions.
Thanks!
"How can I ether automate the updating of time on the virtual machine"
The ntp daemon, ntpd, should take care of this. Sure it's running?
# start the ntp daemon
/etc/init.d/ntpd start
You could also add a daily or hourly entry in root's crontab that updates the time:
# To edit root's crontab
sudo crontab -e
# Add this line to run the command every day at noon (change as needed):
* 12 * * * /usr/sbin/ntpdate
"alter the permissions of that service, so anyone can run it"
This undermines the security model. However, if the users are allowed to use sudo already, you could allow password-less execution of that single executable so it won't block your script:
# To edit the sudoers file:
sudo visudo
# Then add something like this depending on the location of ntpdate:
username ALL= NOPASSWD: /usr/sbin/ntpdate
You should configure and use ntpd instead of ntpdate.
\#!/bin/sh
\#script to set the time using google - my ISP is blocking access to ntp server! major bummer!! :(
HOST="www.google.co.ke"
ping -q -c 2 $HOST > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "----\`date\`---------------------FAILURE----------------------------------"
echo "Failure: \`date\` -- $HOST is is DOWN! >>> better luck next time!"
else
echo "****\`date\`*********************BEGIN: SUCCESS*********************************"
mydateold="\`date\`"
echo "Success: \`date\` -- $HOST is UP >>> set the time!"
date -s "$(date -d "\`curl http://www.google.co.ke -v 2>&1 | grep "Date: " | awk '{ print $3 " " $5 " " $4 " " $7 " " $6 " GMT"}'\`")"
mynewdate="\`date\`"
echo "Success: \`date\` >>> Date updated: OLD DATETIME: $mydateold and NEW DATETIME:
$mynewdate"
echo "****\`date\`*********************END: SUCCESS***********************************"
fi
\#EOF
\#Powered By Slackware Linux :)
I want to send an email when the system is going to shutdown to an email ID. I have CentOS 6.4. Below is my Script.
cat /ect/init.d/sendshtmail
#!/bin/bash
EMAIL="example#example.com"
SHUTDOWNSUBJECT="["`hostname`"] - System Shutdown"
SHUTDOWNBODY="This is an automated message to notify you that "`hostname`" is shutting down.
LOCKFILE=/var/lock/subsys/SystemEmail
echo "${SHUTDOWNBODY}" | mutt -s "${SHUTDOWNSUBJECT}" ${EMAIL}
It has the appropriate permission. While running it manually it's working perfectly. I have just symlinked it to /etc/rc0.d/ folder. By issuing below command.
ln -s /etc/init.d/sendshtmail /etc/rc0.d/K00sendshtmail
But the script is not sending any email during shutdown. Thanks in Advance.
Place your shell script in /etc/init.d with executable permission and symlink name should start with K##. If you want to execute your script at first place immediately after shut down then name it with K00scriptname. Script started will K will be executed first based on ascending order then script with S.
ln -s /etc/init.d/script /etc/rc0.d/K00scriptname
Shutdown command will send the stop signal to script, your script (K00scriptname) should have stop function like example
stop()
{
echo "executing scriptname"
"Your script logic"
}
case "$1" in
stop)
stop
;;
esac
Most important, K00scriptname will execute only if there would be lock file present in /var/lock/subsys folder, so do "touch /var/lock/subsys/scriptname" then check by doing shutdown.
Try to set executable permissions for your script. Sometimes you need to do that to activate it.
chmod 755 /etc/init.d/sendshtmail
Also try to use absolute paths for your command, while quoting the other variable as well.
echo "${SHUTDOWNBODY}" | /usr/bin/mutt -s "${SHUTDOWNSUBJECT}" "${EMAIL}"
Another attempt is to switch your user to your current user e.g.
echo "${SHUTDOWNBODY}" | su -l -c "/usr/bin/mutt -s \"${SHUTDOWNSUBJECT}\" \"${EMAIL}\"" yourusername
ln -s /etc/init.d/sendshtmail /etc/rc0.d/S01sendshtmail
The symlink name should begin with a S - for Start (K for Kill)
The two-digit specifies the order of execution for your script, the lowest numbered being execute first.