The following are working examples for managing files, directories, users and groups in Ubuntu server environment.
For more information you can take a look at this course on ubuntu linux server:
Work with the console:
By typing "History" without the quotes, you can take a look at your previous commands and with "history | grep name_of_command " you can search for specific command that you already used. If you want to return the previously used command just press the "up" arrow. And if you have problems remembering filenames you can start typing the first letter of the name and then press several times "TAB" button - this will give you autocomplete for the rest of the word.
You can clear the console by using "clear".
Directories:
All the directories are contained into the root directory /. Pay attention: Linux file and directory names are case sensitive, so "Linux.txt" is different than "linux.txt".
If you want to see your current directory just type: "pwd". With "cd .." you go up to the parent directory, with "cd /" you will go to the root directory. Using the command "ls" you can see the current objects(files, directories and symbolic links) residing in the file system.
You can create directories using mkdir new_dir_name
To delete directory use: rm dir_name . Before removing a directory, please make sure that it is empty.
"ls -la" will give you more information like properties, date of creation, size, permissions, owner and grop of the files and directories.
if you go to the root / directory: "cd /" "ls -la |grep bin" will show you not only the /bin directory but any directories that start with "bin" characters. We can also use * to show all files starting with 'linux': "ls linux*" or listing all .html files "ls .html"
If you want to know more about the "ls" command just append "--help" next to it:
"ls --help"
and if you want to list the resulting information in pages you can type:
"ls --help | more"
"Enter" and "Space" keys can be used for navigation
For more information on the command try: "man ls". You can exit the help screen with the key "q"
Files:
You can create an empty file with the command "touch file_name.txt". To see the contents of the file just type: cat file_name.txt
In just one line we can actually filter the contents of a file based on condition (all the lines containing "Example"), and output them in a new file(output.txt): "cat examples.desktop | grep "Example" > output.txt "
To remove the file we type: "rm output.txt"
We can edit files with the "nano" editor: "nano output.txt" Inside we can use crtl + O to write the changes, and ctrl + X to exit the editor
Hint: if there are spaces or special symbols in the filename to work with it, just wrap the name with quotes or just to put / before the special symbol:
“123–!.txt”
123-\’.txt \‘file name!\’
file\ name\!
Hint: a convenient way of displaying text files is with "more /var/log/syslog" - this will give us the content paginated and if we use: watch tail /var/log/syslog we can watch if there are any changes to the last few lines of the file.
"watch -n 5 tail -n 15 logfile.txt" will grab last 15 lines of "logfile.txt" and will watch them every 5 seconds.
Moving and copying files:
to move file we use: "mv source_filename /directory/destination_filename"
same is for copying files: "cp filename /directory/"
We can delete files using: "rm file_name" or just all files within a directory with "rm *"
Permissions
When we run: ls -la we see bunch of information about the objects within a directory. For example:
-rwxr-xr-x 1 nevyan nevyan 4096 nov 25 20:34 file
drwxr-xr-x 2 nevyan nevyan 4096 nov 25 20:34 Public
Lets start with the first row representing a file. We know it is a file because the first character of the first column doesn't start with D.
Then we can see its permissions: They are divided into groups of 3(rwx). (read, write and execute)
First 1 group are for the owner(creator) of the file, then for the group it belongs, and last ones are for every other user.
We use groups in order to be able to set once permissions for multiple users belonging to a group, so they can automatically get those permissions.
For example:
just to be able to "cd" into a directory we need to have (+x) execute rights over the directory.
to be able to list file contents with cat we need (+r)read rights over the file.
Linux applies the principle of least privileges, which means that a user is given not more than the privileges he/she needs for completing a certain task. Let's now take a look of what is inside of /etc/shadow with the cat command. We will see: 13 permission denied
Reason is that only the root user can read this file. You can check this out by issuing: ls -la /etc/shadow and look at the others group(3rd) we see - which means no one else except its owner have rights over this file. And the owner can be seen on the second column (root).
If we run the same command, appending "sudo" in front:
"sudo ls -la /etc/shadow"
(and type the root password)
we will see that we have rights to look at the file. This is because for this particular command we have gained temporary "root" rights
Next we can take a look at /etc/passwd - there we can see all the users registered into the server, their login names, hidden password, user id, group, home directory as well as working shell(command prompt).
Changing permissions:
"chmod ugo+rwx file_name.txt"
this will give maximum privileges(rwx) to all(user,group and others) to file_name.txt
Changing ownership:
to change the ownership we can type:
"chown user1:user1 file_name.txt"
will set user=user1 and group=user1 to the file.
Groups:
Each user can belong to one primary and several secondary groups.
"id" shows the current groups our user belongs to. We can issue: groups and user_name to find out which groups specific user belongs to:
"groups user_name" will list the information in format: (primary, secondary) groups
The same information can be gained from the file: "/etc/group" and we can list information about particular group with: "cat /etc/group | grep mygroup"
"addgroup mygroup" will add new "mygroup" to our currenly existing groups
"usermod -G mygroup nevyan" will remove all secondary groups and will add secondary group "mygroup" to user "nevyan"
With the flag -g we can change the user primary group and if we want just to append another secondary group to our user we can use the -aG flag.
In order to remove user from a group: "deluser nevyan mygroup"
and finally to remove a group we do: "sudo delgroup mygroup"
Notice that we need to "logout" in order to see the effect of those group changes.
Congratulations and enjoy the ubuntu linux server course:
Various tutorials from programming to system administration. Topics include docker, javascript, angular, ubuntu, php and many others.
Youtube channel !
Be sure to visit my
youtube channel
Subscribe To My Channel for updates
Integrating AI code helpers into Visual Studio Code
In this guide, we’ll walk through setting up a local AI-powered coding assistant within Visual Studio Code (VS Code). By leveraging tools s...
-
Here is how to share your Internet connection in a way that other computers to be able to use it. Happy learning and be sure to check ...
-
Here we will be doing an installation of the development/production environment of PHP and MySQL using Docker under Ubuntu. Here is a full v...
-
Laravel installation under Docker seems a painful experience but at the same time, it is a rewarding learning experience. The following are ...