ADC Lab Exp 5
ADC Lab Exp 5
At the same time, it is to be expected that two or more users may need to share access to certain
system resources, such as directories and files. User and group management in Linux allows us
to accomplish both objectives.
Superuser permissions can be gained either by changing to the root user with the su command or
using sudo. The latter approach is used by default in Ubuntu and derivatives, and is preferred
over the former in other distributions as well.
It is also important to note that, as opposed to other Linux flavors, the user that is created when
Ubuntu is first installed has superuser privileges out-of-the-box. You can verify whether sudo is
installed on your machine by running
In Ubuntu or derivatives, this is as easy as doing (you will be required to enter your password to
run sudo):
sudo adduser pluralsight
When a new user is added, a group with the same name is created automatically. This
is called a primary group.
1
Useradd - Adds accounts to the system
2
Usermod - Modifies account attributes
3
Userdel - Deletes accounts from the system
4
Groupadd - Adds groups to the system
5
Groupmod - Modifies group attributes
6
Groupdel - Removes groups from the system
You can use Manpage Help to check complete syntax for each command mentioned here.
Create a Group
We will now understand how to create a group. For this, we need to create groups before
creating any account otherwise, we can make use of the existing groups in our system. We have
all the groups listed in /etc/groups file.
All the default groups are system account specific groups and it is not recommended to use
them for ordinary accounts. So, following is the syntax to create a new group account −
groupadd [-g gid [-o]] [-r] [-f] groupname
The following table lists out the parameters −
1
-g GID - The numerical value of the group's ID
2
-o This option permits to add group with non-unique GID
3
-r This flag instructs groupadd to add a system account
4
-f This option causes to just exit with success status, if the specified group already exists. With
-g, if the specified GID already exists, other (unique) GID is chosen
5
Groupname Actual group name to be created
If you do not specify any parameter, then the system makes use of the default values.
Following example creates a developers group with default values, which is very much
acceptable for most of the administrators.
$ groupadd developers
Modify a Group
To modify a group, use the groupmod syntax −
$ groupmod -n new_modified_group_name old_group_name
To change the developers_2 group name to developer, type −
$ groupmod -n developer developer_2
Here is how you will change the financial GID to 545 −
$ groupmod -g 545 developer
Delete a Group
We will now understand how to delete a group. To delete an existing group, all you need is
the groupdel command and the group name. To delete the financial group, the command is −
$ groupdel developer
This removes only the group, not the files associated with that group. The files are still
accessible by their owners.
Create an Account
Let us see how to create a new account on your Unix system. Following is the syntax to create a
user's account −
useradd -d homedir -g groupname -m -s shell -u userid accountname
The following table lists out the parameters −
1
-d homedir Specifies home directory for the account
2
-g groupname Specifies a group account for this account
3
-m Creates the home directory if it doesn't exist
4
-s shell Specifies the default shell for this account
5
-u userid You can specify a user id for this account
6
Accountname Actual account name to be created
If you do not specify any parameter, then the system makes use of the default values.
The useradd command modifies the /etc/passwd, /etc/shadow, and /etc/group files and creates
a home directory.
Following is the example that creates an account mcmohd, setting its home directory
to /home/mcmohd and the group as developers. This user would have Korn Shell assigned to it.
$ useradd -d /home/mcmohd -g developers -s /bin/ksh mcmohd
Before issuing the above command, make sure you already have the developers group created
using the groupadd command.
Once an account is created you can set its password using the passwd command as follows −
$ passwd mcmohd20
Changing password for user mcmohd20.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
When you type passwd accountname, it gives you an option to change the password, provided
you are a superuser. Otherwise, you can change just your password using the same command
but without specifying your account name.
Modify an Account
The usermod command enables you to make changes to an existing account from the command
line. It uses the same arguments as the useradd command, plus the -l argument, which allows
you to change the account name.
For example, to change the account name mcmohd to mcmohd20 and to change home directory
accordingly, you will need to issue the following command −
$ usermod -d /home/mcmohd20 -m -l mcmohd mcmohd20
Delete an Account
The userdel command can be used to delete an existing user. This is a very dangerous
command if not used with caution.
There is only one argument or option available for the command .r, for removing the account's
home directory and mail file.
For example, to remove account mcmohd20, issue the following command −
$ userdel -r mcmohd20
If you want to keep the home directory for backup purposes, omit the -r option. You can
remove the home directory as needed at a later time.
In this chapter, we will discuss in detail about file permission and access modes in Unix. File
ownership is an important component of Unix that provides a secure method for storing files.
Every file in Unix has the following attributes −
Owner permissions − The owner's permissions determine what actions the owner of the
file can perform on the file.
Group permissions − The group's permissions determine what actions a user, who is a
member of the group that a file belongs to, can perform on the file.
Other (world) permissions − The permissions for others indicate what action all other
users can perform on the file.
Changing Permissions
To change the file or the directory permissions, you use the chmod (change mode) command.
There are two ways to use chmod — the symbolic mode and the absolute mode.
Using chmod in Symbolic Mode
The easiest way for a beginner to modify file or directory permissions is to use the symbolic
mode. With symbolic permissions you can add, delete, or specify the permission set you want
by using the operators in the following table.
1
+ Adds the designated permission(s) to a file or directory.
2
- Removes the designated permission(s) from a file or directory.
3
= Sets the designated permission(s).
Here's an example using testfile. Running ls -1 on the testfile shows that the file's permissions
are as follows −
$ls -l testfile
-rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
Then each example chmod command from the preceding table is run on the testfile, followed
by ls –l, so you can see the permission changes −
$chmod o+wx testfile
$ls -l testfile
-rwxrwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod u-x testfile
$ls -l testfile
-rw-rwxrwx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod g = rx testfile
$ls -l testfile
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile
Here's how you can combine these commands on a single line −
$chmod o+wx,u-x,g = rx testfile
$ls -l testfile
-rw-r-xrwx 1 amrood users 1024 Nov 2 00:10 testfile
0 No permission ---
Here's an example using the testfile. Running ls -1 on the testfile shows that the file's
permissions are as follows −
$ls -l testfile
-rwxrwxr-- 1 amrood users 1024 Nov 2 00:10 testfile
Then each example chmod command from the preceding table is run on the testfile, followed
by ls –l, so you can see the permission changes −
$ chmod 755 testfile
$ls -l testfile
-rwxr-xr-x 1 amrood users 1024 Nov 2 00:10 testfile
$chmod 743 testfile
$ls -l testfile
-rwxr---wx 1 amrood users 1024 Nov 2 00:10 testfile
$chmod 043 testfile
$ls -l testfile
----r---wx 1 amrood users 1024 Nov 2 00:10 testfile
Changing Ownership
The chown command changes the ownership of a file. The basic syntax is as follows −
$ chown user filelist
The value of the user can be either the name of a user on the system or the user id (uid) of a
user on the system.
The following example will help you understand the concept −
$ chown amrood testfile
$
Changes the owner of the given file to the user amrood.
NOTE − The super user, root, has the unrestricted capability to change the ownership of any
file but normal users can change the ownership of only those files that they own.
1
nice/renice Runs a program with modified scheduling priority
2
Netstat Prints network connections, routing tables, interface statistics, masquerade
connections, and multicast memberships
3
Time Helps time a simple command or give resource usage
4
Uptime This is System Load Average
5
Ps Reports a snapshot of the current processes
6
Vmstat Reports virtual memory statistics
7
Gprof Displays call graph profile data
8
Prof Facilitates Process Profiling
9
Top Displays system tasks
5.4. Add Your Printer To Computer
First basic thing is to install or add your printer. Connecting printer in Linux machine is very
simple as almost all Linux distros have printer supports. Open the printer app from your
app menu and look if the printer has been added.
If the printer is already added or configured, it will be listed quickly on the opening printer app.
If not, go to Openprinting and install your driver and again launch your printer option. Your
printer should now be listed under add option, click forward on your printer config. Add the
description and done. Open printer driver page has a vast variety so it is very unlikely that you
don‘t find your printer.
Print anything to check your printer.Now as the printer is added let‘s share it so you can access it
on other systems. It is also simple. Right-click on your printer and select the shared option.
Here it is done. You have successfully shared your printer on a network in Linux. Now you will
need to access it on other systems. Here is how to do it.
I assume you are on LAN or somehow connected to your system from which we have shared our
printer. Now here is what you have to do. Again open the printer tool as we did earlier and hit the
ADD button. Your shared printer will be listed under the network printer. Click the remote
hostname with the preferred printer, forward and fill out the description as we did earlier, apply
and done. Voila! Print now!
If your printer is not listed, click on a find network printer. Enter the IP address of the printer and
hit find. URI would be automatically filled. Again do as the old-time fill description and apply.
Done!
5.4. Share a Printer on Linux
We used Ubuntu 14.04 for this, but the process should be similar on other distributions.
Open Ubuntu‘s System Settings window by clicking the gear icon on the top bar and selecting
System Settings. Click the Printers icon and any printers you‘ve added will appear in the list.
Click the Server menu at the top of the screen and select Server Settings.
Click the ―Publish shared printers connected to this system‖ checkbox to enable network sharing
of connected printers.
Right-click the printer in the list, select Properties, and click Policies. Ensure the Shared box is
checked so the printer will be shared.
Open Ubuntu‘s System Settings window and click the Printers icon. Click the Add button to add
a new printer.
Expand the Network Printer section, select Windows Printer via SAMBA, and click the Browse
button. You‘ll be able to browse available network printers connected to different computers on
the network. Add the printer to your PC, configure its drivers, and it will appear as an available
printer when printing from Linux applications.
Ubuntu can see printers shared via Bonjour from a Mac and will automatically add them.
Modern printers often have built in Wi-Fi, so they can make themselves available to all the
computers, smartphones, and tablets on a network without any complicated printer-sharing. Wi-
Fi printers are ideal if you want to share a printer without the hassle.
5.5. Configuring Network in Linux
Ubuntu GUI Network Tools:
Flags:
Where:
Flags:
host - Give a host name and the command will return IP address. Unlike nslookup,
the host command will use both /etc/hosts as well as DNS.
Example: host domain-name-of-server
nslookup - Give a host name and the command will return IP address. Also see Testing your
DNS (YoLinux Tutorial) Note that nslookup does not use the /etc/hosts file.
ssh your_username@host_ip_address
If the username on your local machine matches the one on the server you are trying to connect
to, you can just type:
ssh host_ip_address
2. Type in your password and hit Enter. Note that you will not get any feedback on the screen
while typing. If you are pasting your password, make sure it is stored safely and not in a text
file.
3. When you are connecting to a server for the very first time, it will ask you if you want to
continue connecting. Just type yes and hit Enter. This message appears only this time since
the remote server is not identified on your local machine.
4. An ECDSA key fingerprint is now added and you are connected to the remote server.
If the computer you are trying to remotely connect to is on the same network, then it is best to
use the private IP address instead of the public IP address. Otherwise, you will have to use the
public IP address only. Additionally, make sure that you know the correct TCP port OpenSSH is
listening to for connection requests and that the port forwarding settings are correct. The default
port is 22 if nobody changed configuration in the sshd_config file. You may also just append the
port number after the host IP address.
Here is the example of a connection request using the OpenSSH client. We will specify the port
number as well:
username@host:~$
You are now able to manage and control a remote machine using your terminal. If you have
trouble connecting to a remote server, make sure that:
ftp 192.168.42.77
2. If the connection is established, a confirmation message will be displayed, and you will
be prompted to enter your FTP username, in this example the FTP username
is linuxize:
3. Once you enter the username you will be prompted to type your password:
Password:
4. If the password is correct, the remote server will display a confirmation message and
the ftp> prompt.
5. 230 OK. Current restricted directory is /
6. Remote system type is UNIX.
7. Using binary mode to transfer files.
ftp>
If the FTP server you are accessing accepts anonymous FTP accounts, and you want to log in as
an anonymous user, use anonymous as username and your email address as a password.
Unix is a multi-user system where the same resources can be shared by different users.
All permissions in Unix are based on restricting access to specific files and folders to specific
users or user groups.
Read permission – If authorized, the user can read the contents of the file.
Execute permission – If authorized, the user can execute the file as a program.
Each file is associated with a set of identifiers that are used to determine who can access the file:
User ID (UID) – Specifies the user that owns the file. By default, this is the creator of the file.
Group ID (GID) – Specifies the user-group that the file belongs to.
Finally, there are three sets of access permissions associated with each file:
User permission – Specifies the level of access given to the user matching the file‘s UID.
Group permission – Specifies the level of access given to users in groups matching the file‘s
GID.
Others permission – Specifies the level of access given to users without a matching UID or GID.
Together, this scheme of access controls makes the Unix system extremely secure while
simultaneously providing the flexibility required of a multi-user system.
The ls -l command can be used to view the permissions associated with each of the files in the
current folder.
Example:
total of 24
In this output, the ‗total 24‘ indicates the total number of blocks occupied by the listed files.
flags – A collection of flags indicating the file mode and the file permissions.
modified-date – The month, date, hour and minute of the last modification to the file.
name – The name of the file or directory.
The flags in the first column specify the file mode and the different sets of permissions:
d: represents a directory
The first of these three indicates whether the user has read permission:
The second character indicates whether the user has to write permission:
The last character indicates whether the user has executed permission:
#3) The next three characters indicate group permissions, similar to the user permissions above.
#4) The final three characters indicate public permissions, similar to the user permissions above.
In case the file is an ordinary file, read permission allows the user to open the file and examine
its contents. Write permission allows the user to modify the contents of the file. And execute
permission allows the user to run the file as a program.
In case the file is a directory, read permission allows the user to list the contents of the directory.
Write permission allows the users to create a new file in the directory, and to remove a file or
directory from it. Execute permission allows the user to run a search on the directory.
Note that only the owner of the file can change the access permissions.
description: This command is used to change the file permissions. These permissions are read,
write and execute permission for the owner, group, and others.
The first optional parameter indicates who – this can be (u)ser, (g)roup, (o)thers or (a)ll
The second optional parameter indicates opcode – this can be for adding (+), removing (-) or
assigning (=) permission.
The third optional parameter indicates the mode – this can be (r)ead, (w)rite, or e(x)ecute.
Example: Add write permission for user, group and others for file1
$ ls -l
$ ls -l
-rw-rw-rw- 1 user staff 39 Jun 21 15:37 file1
$ ls -l
The mode is a combination of three digits – the first digit indicates the permission for the user,
the second digit for the group, and the third digit for others.
Each digit is computed by adding the associated permissions. Read permission is ‗4‘, write
permission is ‗2‘ and execute permission is ‗1‘.
Example: Give read/write/execute permission to the user, read/execute permission to the group,
and execute permission to others.
$ ls -l
$ ls -l