InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Ubuntu tutorials

  • How to add a user on Linux with sudo access
  • How to check if a user has password on Linux
  • How to install ViewVC for svn repository on Ubuntu Linux
  • How to setup ssl (https) for your site on Ubuntu Linux
  • Install rabbitmq on Ubuntu Linux
  • LXC (Linux Containers) - quick start tutorial on Ubuntu
  • Linux - command to check swap size
  • Linux - how to add a user using useradd
  • Linux - how to create a large file like 1GB
  • Linux - how to create swap partition
  • Linux - how to remove user from a group
  • Linux - number of cpus (lscpu)
  • Linux du - find disk usage of directories or files
  • NFS client and server handy commands
  • Setup svn repository on AWS Ubuntu with apache auth
  • Ubuntu - check if a service is upstart based
  • Ubuntu - dpkg and apt-get beginner tutorial
  • Ubuntu - find file to package
  • Ubuntu - find where will a package be installed from
  • Ubuntu - how to auto start services on boot using update-rc.d (sysv init)
  • Ubuntu - how to find boot autostart status of a service
  • Ubuntu - reload iptables rules during boot
  • Use x2go to access remote Ubuntu Linux
 
  • Home
  • > Tutorials
  • > Linux

How to add a user on Linux with sudo access

By admin | Last updated on Mar 20, 2016

Sometime we need to add a user on Linux environment from command line or from a shell/bash script and give it sudo access. Linux has command useradd which can be used to this purpose and sudo permission can be given using /etc/sudoers.d directory. Here are the steps for this on Ubuntu Linux (will work on Amazon linux and other linux flavours also):

Add a user with default shell as bash

To add user (say user1) with default shell bash, without password and create home directory if needed, run the following command as root:

$ useradd user1 -m -s /bin/bash

In case you want to create a user with password use -p. This will prompt you for a password.

Authorizing user’s key to ssh

It is a good idea to use user’s key to ssh to machine as that user. Run the following commands for that as root:

$ su - user1
$ mkdir ~/.ssh
$ cd ~/.ssh
$ cat [user1_public_key_file] >> authorized_keys
$ chown user1

Now you can login to the machine using the following command:

ssh -i [user1_private_key_file] user@host

Add password less sudo access

To provide user sudo access we can create a file in /etc/sudoders.d/user and the following line in it:

user1 ALL=(ALL) NOPASSWD: ALL

Please note that this has to be run as root user. Now login as user1 and run any command with sudo to test. e.g. run ls with sudo as shown below:

sudo ls -l

We have used user1 for the purpose of this article. You can use any other user and replace user1 wit it in above instructions.

Suggested posts:

  1. Linux – how to run a command as different user
  2. Mac finder – change search default folder to current folder
  3. Linux – providing sudo access to a users – some best practices
  4. How to locally override website domain (or hostname) to IP mapping
  5. Linux – how to add a user using useradd
  6. How to print javascript object to log
  7. Setup xdebug for remote wordpress debugging
  8. prevent wordpress xmlrpc.php attack
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Linux, Tutorials, Ubuntu Linux
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress