Friday, July 14, 2017

linux random notes

cd - (previous directory). This will take you to the previous directory you were just at.


To find out what kind of file a file is, you can use the file command. It will show you a description of the file’s contents.
$ file banana.jpg


$ less /home/pete/Documents/text1
Use the following command to navigate through less:
q - Used to quit out of less and go back to your shell.
Page up, Page down, Up and Down - Navigate using the arrow keys and page keys.
g - Moves to beginning of the text file.
G - Moves to the end of the text file.
/search - You can search for specific text inside the text document. Prefacing the words you want to search with /
h - If you need a little help about how to use less while you’re in less, use help.


One thing to note, if you copy a file over to a directory that has the same filename, the file will be overwritten with whatever you are copying over. This is no bueno if you have a file that you don’t want to get accidentally overwritten. You can use the -i flag (interactive) to prompt you before overwriting a file.
$ cp -i mycoolfile /home/pete/Pictures


Let’s say you did want to mv a file to overwrite the previous one. You can also make a backup of that file and it will just rename the old version with a ~.
$ mv -b directory1 directory2


find /home -name puppies.jpg
One cool thing to note is that find doesn’t stop at the directory you are searching, it will look inside any subdirectories that directory may have as well


alias
Sometimes typing commands can get really repetitive, or if you need to type a long command many times, it’s best to have an alias you can use for that. To create an alias for a command you simply specify an alias name and set it to the command.
$ alias foobar='ls -la'
Now instead of typing ls -la, you can type foobar and it will execute that command, pretty neat stuff. Keep in mind that this command won't save your alias after reboot, so you'll need to add a permanent alias in:
~/.bashrc
or similar files if you want to have it persist after reboot.
You can remove aliases with the unalias command:
$ unalias foobar


grep
The grep command is quite possibly the most common text processing command you will use. It allows you to search files for characters that match a certain pattern. What if you wanted to know if a file existed in a certain directory or if you wanted to see if a string was found in a file? You certainly wouldn't dig through every line of text, you would use grep!
Let's use our sample.txt file as an example:
$ grep fox sample.txt
You should see that grep found fox in the sample.txt file.
You can also grep patterns that are case insensitive with the -i flag:
$ grep -i somepattern somefile
To get even more flexible with grep you can combine it with other commands with |.
$ env | grep -i User
As you can see grep is pretty versatile. You can even use regular expressions in your pattern:
$ ls /somedir | grep '.txt$'
Should return all files ending with .txt in somedir.


Now that you know what commands to run as the superuser, the question is how do you know who has access to do that? The system doesn't let every single Joe Schmoe run commands as the superuser, so how does it know? There is a file called the /etc/sudoers file, this file lists users who can run sudo. You can edit this file with the visudo command.


/etc/passwd
Remember that usernames aren't really identifications for users. The system uses a user ID (UID) to identify a user. To find out what users are mapped to what ID, look at the /etc/passwd file.
$ cat /etc/passwd
This file shows you a list of users and detailed information about them. For example, the first line in this file most likely looks like this:
root:x:0:0:root:/root:/bin/bash
Each line displays user information for one user, most commonly you'll see the root user as the first line. There are many fields separated by colons that tell you additional information about the user, let's look at them all:
Username
User's password - the password is not really stored in this file, it's usually stored in the /etc/shadow file. We'll discuss more in the next lesson about /etc/shadow, but for now, know that it contains encrypted user passwords. You can see many different symbols that are in this field, if you see an "x" that means the password is stored in the /etc/shadow file, a "*" means the user doesn't have login access and if there is a blank field that means the user doesn't have a password.
The user ID - as you can see root has the UID of 0
The group ID
GECOS field - This is used to generally leave comments about the user or account such as their real name or phone number, it is comma delimited.
User's home directory
User's shell - you'll probably see a lot of user's defaulting to bash for their shell


/etc/shadow
The /etc/shadow file is used to store information about user authentication. It requires superuser read permissions.
$ sudo cat /etc/shadow
root:MyEPTEa$6Nonsense:15000:0:99999:7:::
You'll notice that it looks very similar to the contents of /etc/passwd, however in the password field you'll see an encrypted password. The fields are separated by colons as followed:
Username
Encrypted password
Date of last password changed - expressed as the number of days since Jan 1, 1970. If there is a 0 that means the user should change their password the next time they login
Minimum password age - Days that a user will have to wait before being able to change their password again
Maximum password age - Maximum number of days before a user has to change their password
Password warning period - Number of days before a password is going to expire
Password inactivity period - Number of days after a password has expired to allow login with their password
Account expiration date - date that user will not be able to login
Reserved field for future use


/etc/group
Another file that is used in user management is the /etc/group file. This file allows for different groups with different permissions.
$ cat /etc/group
root:*:0:pete
Very similar to the /etc/password field, the /etc/group fields are as follows:
Group name
Group password - there isn't a need to set a group password, using an elevated privilege like sudo is standard. A "*" will be put in place as the default value.
Group ID (GID)
List of users - you can manually specify users you want in a specific group


No comments:

Post a Comment