Ubuntu 18.04 - Add new user and initialize it - bash

I'm creating a .sh bash script in Ubuntu 18.04.1 LTS where I need to create a new user using the "adduser" command:
sudo adduser newuser
After that, I need to perfomr other operations like add some files in the newuser's Desktop or Documents folders. The main problem is that, untill I don't reboot the system, the newuser haven't that folders so I need to create them manually
sudo mkdir -p /home/newuser/Desktop
sudo mkdir -p /home/newuser/Documents
sudo mkdir -p /home/newuser/.local/share/applications
I really don't like this solution. Is there a way to initialize that folders after a user creation?
Thanks

You can try using xdg-user-dirs-update tool which generates all required user directories in the $HOME path.
Don't forget to do su newuser first.

Related

How to add the current user to the Docker group on macOS?

I am trying to follow this post, adding the current user to the Docker group:
sudo usermod -aG docker $(whoami)
but of course, there is no usermod command on macOS:
-bash: usermod: command not found
Now I was wondering if there is an equivalent of the above command on macOS? Probably using dscl?
P.S.1. I have used these instructions to set up Docker and docker-machine.
P.S.2. This question is not about Visual Studio Code (VSCode)
in particular, but if I open a new terminal and run eval "$(docker-machine env default)" and then run VSCode with code the problem is solved.
This question, as others have also pointed out, is irrelevant. The process of adding a user to the docker group is only necessary on Linux where sudo privileges are required to run Docker commands, as explained here. On macOS, and using docker-machine, that is unnecessary.
But if one wants to add a user, or more specifically the current user, to the docker user group, for whatever reason, here are the instructions:
List the existing user groups with dscl . list /groups from here
To create a user group, if it doesn't exist use the command sudo dscl . create /Groups/<groupName> from here.
In the context of this discussion the <groupName> could be replaced with docker.
To add a user to a group one can use the command sudo dseditgroup -o edit -a <userName> -t user <groupName>. from here or sudo dscl . append /Groups/<groupName> GroupMembership <userName> from here.
One can replace the <userName> with $USER or $(whoami) to refer to the current user.
To test and see if the expected user has been added o the specific group one can use the command dscl . -read /Groups/<groupName> GroupMembership to list all the remembers. However, it is not guaranteed to deliver the correct result, as explained here.
And the another issue with the Visual Studio Code, also has barely anything to do with the user groups. By running the eval "$(docker-machine env <dockerMachineName>)" in a new terminal, and running the code editor from inside the terminal, the Docker extension works just fine.
I also had to write sudo on mac to run docker commands (I don't know why). Thanks to Foad S. Farimani explanation I fixed it.
I just want to add commands which are ready for using.
First one creates docker group.
Second one adds current user into docker group.
sudo dscl . create /Groups/docker
sudo dseditgroup -o edit -a $USER -t user docker
If you need something different read Foad S. Farimani answer.

WGET seems not to work with user data on AWS EC2 launch

I launch an centos AMI I created, and try to add user data as a file which looks like this:
#!/bin/bash
mkdir /home/centos/testing
cd testing
wget https://validlink
So simply, on launch, the user data creates a folder called testing and downloads this validURL which I will not put as it links to my data - however it is valid and accessible.
When I launch the instance, the folder testing is created successfully, however there is no file inside the directory.
When I ssh into the instance, and run the wget command as a sudo, the file is downloaded successfully inside the testing folder.
Why does the file not get downloaded on the ec2 launch through user data?
You have no way of knowing the current working directory when you execute the cd command. So specify full path:
cd /home/centos/testing
Try this:
#!/bin/bash
mkdir /home/centos/testing
cd /home/centos/testing
wget https://validlink
Run it using the root user.
Try this instead:
#!/bin/bash
sudo su
yum -y install wget
mkdir /home/centos/testing
cd /home/centos/testing
wget https://validlink

osx: How to install a package into user's application support directory?

I need to install 2 audio plugins to the root Audio/Plug-Ins/VST & Components directories. My installer does that fine. But I also need to install a directory of preset files into /Users/$USER/Library/Application Support/MyCompany folder.
I've heard that an installer can't install to / and ~ in the same installer, but I really want it to be 1 install for the user. So it seems like a good idea would be to install the VST and Components first. Then install the preset folder in a temporary location (like /tmp or similar) and then run a post-install script to move the files to the user's Library...but I can't get that to work.
This is the script I'm trying to run:
#!/bin/bash
# movePresets.sh
# I want something like this...but it doesn't work because $USER is root in the installer I believe
/usr/bin/sudo -u $USER mkdir -p "/Users/$USER/Library/Application Support/MyCompany/Presets"
/usr/bin/sudo -u $USER mv -r "/tmp/Presets" "$USER/$USER/Library/Application Support/MyCompany"
exit 0
Obviously, I don't know the proper way to access a user's directory as root. Help please...thank you.
Have you tried saving off the user in a variable first?
#!/bin/bash
realuser=$USER
# or
#realuser=$(whoami)
/usr/bin/sudo -u $realuser mkdir -p "/Users/$realuser/Library/Application Support/MyCompany/Presets"
/usr/bin/sudo -u $realuser mv -r "/tmp/Presets" "$realuser/$realuser/Library/Application Support/MyCompany"

Need to use sudo su - in Unix shell script

I am at beginner level and I need to use sudo su - and pwd in one command line in script for two different users. (I'm using pwd as an example; the specific command is not important.)
And I am using command sudo su - user -c pwd. This command works when switching to one user, but not when switching to another.
For example:-
$ sudo su - ora -c pwd
/oracle/
$ sudo su - adm -c pwd
Sorry, user myuser is not allowed to execute '/usr/bin/su - adm -c pwd' as root on server.
$
How can I make it work for 'adm' user too?
sudo is used to run a command as somebody else.
By default it runs a command as root.
You can also supply the -u option to run a command as another user.
You really shouldn't need to use sudo and su together as they do similar jobs.
Sudo does this job in a much more controlled and configurable fashion.
Sudo can be configured to control:
Who can use it.
What commands they can run.
Who they can run them as.
Whether they need to supply their password when doing so.
You can only run one command at a time, so if you need to do several things together you will need to write a script. Alternatively you can chain them together in a one liner. Or finally you can run a shell as the user you require. e.g.:
sudo bash
I think in your case you probably want to use:
sudo -u adm anycommand

Default user for files and directories created in bash under sudo

I'm writing a bash script that creates directories and copy files under Mac OSX. Some of these directories and files need to be placed in folders owned by the system such as /Library/Audio/Plug-Ins, and so I run the script under sudo. Such script might look like:
copy-plugins.sh:
#!/usr/bin/env bash
mkdir -p /Library/Audio/Plug-Ins/My-Plugins
cp plugin-A.dylib /Library/Audio/Plug-Ins/My-Plugins
cp plugin-B.dylib /Library/Audio/Plug-Ins/My-Plugins
and called:
$ sudo ./copy-plugins.sh
However when running under sudo, all created directories and copied files are owned by root.
I would like to be able to run the script under sudo and have the files be owned by my user.
I could call chown after each file/directory is created or copied
copy-plugins-cumbersome.sh:
#!/usr/bin/env bash
mkdir -p /Library/Audio/Plug-Ins/My-Plugins
chown 501:501 /Library/Audio/Plug-Ins/My-Plugins
cp plugin-A.dylib /Library/Audio/Plug-Ins/My-Plugins
chown 501:501 /Library/Audio/Plug-Ins/My-Plugins/plugin-A.dylib
cp plugin-B.dylib /Library/Audio/Plug-Ins/My-Plugins
chown 501:501 /Library/Audio/Plug-Ins/My-Plugins/plugin-B.dylib
but I'm hoping for a more general solution.
As far as I can tell there is no setuid for bash.
Use cp -p option to preserve file attributes.
Note this will preserve user, group permissions and the modification and access times of the files.
As you need sudo to copy to the directories you are copying to in script, it means you need to be root to copy anything in those directories.
When you do sudo you are root for that particular command or script, so whatever will be created or executed will have root permissions. Till the time you specify.
The possible ways to come out of it without changing anything:
The one you are using, and
Other one to use -p or -a with cp
rsync -go <source file> <destination file>
-g for preserving group and
-o for preserving ownership.
Note If you do a chown out of script, you will have to specifically do sudo chown since files you would be touching belong to root.

Resources