How to add the current user to the Docker group on macOS? - 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.

Related

How to delete user via terminal

I'm having some issues installing Postgres and have been trying a few different approaches. I eventually followed this approach which meant I had to run the following command in terminal...
sudo dscl . -create /Users/postgres UserShell /bin/sh
sudo dscl . -create /Users/postgres NFSHomeDirectory /Library/PostgreSQL
However, this did not resolve issue I'm having with installing Postgres.
I then attempted to added postgres users via system preference but ran into the error of "Name already used by another user". However I cannot see this users!
.
How can I effectively delete the postgres user created in the above command? The goal is to add the users via system preferences instead.
I can see the hidden user by using this command:
dscl . -list /Users
I figured it out.
sudo /usr/bin/dscl . -delete "/Users/postgres"

Ubuntu 18.04 - Add new user and initialize it

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.

Adding newuser in docker container with sudo privileges

Im trying to build a docker file and one of the reqt is to create a user with sudo permissions.
Here is the bash script
# quietly add a user without password
adduser --quiet --disabled-password --shell /bin/bash --home /home/newuser --gecos "testuser" newuser
# set password
echo "testuser:testuser" | sudo chpasswd
and the docker compose file.
FROM ros
RUN apt-get update
RUN apt-get install -y sudo
ADD run.sh /usr/local/bin/run.sh
RUN chmod +x /usr/local/bin/run.sh
CMD ["/usr/local/bin/run.sh"]
When I run this build I get the following error.
chpasswd: (user testuser) pam_chauthtok() failed, error:
Authentication token manipulation error
chpasswd: (line 1, user testuser) password not changed
I think you're misinterpreting the requirements. Creating an user with sudo permissions is different from creating an user with the sudo command.
Depending on the distribution, an user may run sudo if it belongs to the wheel or sudo group (the latter is the case with Ubuntu, which is the base image used by ros).
I strongly suggest that you use the useradd command instead of adduser. The latter is different in Debian & RedHat based distributions, unlike the former which is the same across Linux distributions and even *BSD if you don't use the long options. Also, the former lets you specify the supplementary groups in the command line (-G option).
useradd -m -s /bin/bash -G sudo newuser
If you use the -p option you could also supply the password in encrypted form (the term in the manpage should be hashed form) without the need to use chpasswd later. Use the output of mkpasswd -m sha-512. (The mkpasswd command is present in the whois package). If you're going to use chpass, use the -e option to supply the password in encrypted form. Never use plaintext.

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

Detecting if a command is executable by sudo

I am wanting to detect in a shell script if a command I am going to run via sudo can in fact run via sudo. On newer versions of sudo I can do sudo -l "command" and this gives me exactly the result I want.
However, some of the systems have an old version of sudo in which -l "Command" isn't available. Another way I was thinking about doing it was to just try running the command then see if sudo prompted for the password. However, I do not see an easy way to do this as sudo writes the password prompt to the TTY and not via stdout.
Does anyone else know of a straight forward way to do this?
I should also mention "expect" doesn't seem to be available on the systems with the older sudo revisions, either.
Just for reference the "difficult" version of sudo appears to version 1.6.8
On Linux, on (at least) Debian-like systems, you can have a look at /etc/sudoers (and the optional /etc/sudoers.d/* files, if created, and included in the main /etc/sudoers) that give (among others)
the search path to where (which dir) a command can be issued
the sudo user (root) privileges
groups who can use sudo and their privileges
This is the sudoers man page for more information.
if you're only wanting to check that a password is required to run a command then you should be able to run:
$ sudo -n <command>
E.g.
$ sudo -n echo
sudo: sorry, a password is required to run sudo

Resources